KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > http > load > webapp > ValidateHttpFieldReplicationTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.http.load.webapp;
5
6 import org.apache.commons.lang.SerializationUtils;
7
8 import com.tctest.performance.http.load.AbstractHttpLoadTest;
9 import com.tctest.performance.sampledata.OrganicObjectGraph;
10 import com.tctest.performance.sampledata.OrganicObjectGraphManager;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FilenameFilter JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 public abstract class ValidateHttpFieldReplicationTest extends AbstractHttpLoadTest {
23
24   private static final String JavaDoc VALIDATE = "validate";
25   private boolean validate;
26
27   protected ValidateHttpFieldReplicationTest(String JavaDoc[] args) {
28     super(setArgs(args));
29     if (args.length == 2 && args[0].equals(VALIDATE)) validate = true;
30   }
31
32   protected abstract int changes();
33
34   protected abstract int graphSize();
35
36   protected final void execute() throws Exception JavaDoc {
37     if (!validate) super.execute();
38     else validateObjectGraphs(resultsDir());
39   }
40
41   private static String JavaDoc[] setArgs(String JavaDoc[] args) {
42     if (args.length == 2 && args[0].equals(VALIDATE)) return new String JavaDoc[] { "0", args[1] };
43     return args;
44   }
45
46   protected final void validateArgs(String JavaDoc[] args) {
47     if (args.length < 2) {
48       System.out.println("Usage:");
49       System.out.println(" [<duration in seconds> | validate] <working dir path> [report]");
50       System.exit(0);
51     }
52   }
53
54   private final void validateObjectGraphs(File JavaDoc resultsDir) throws Exception JavaDoc, FileNotFoundException JavaDoc {
55     if (!resultsDir.exists()) {
56       System.err.println("No Data Available to Validate");
57       return;
58     }
59     String JavaDoc[] files = resultsDir.list(new FilenameFilter JavaDoc() {
60       public boolean accept(File JavaDoc dir, String JavaDoc name) {
61         return name.endsWith("obj");
62       }
63     });
64     OrganicObjectGraph[] graphArray;
65     OrganicObjectGraph graph;
66     String JavaDoc hostKey;
67     Map JavaDoc hostGraphs = new HashMap JavaDoc();
68     List JavaDoc graphs;
69
70     // deserialize map of graphs keyed by host
71
for (int i = 0; i < files.length; i++) {
72       String JavaDoc[] parts = files[i].split("_");
73       if ((parts.length == 3) && parts[1].equals("graph")) {
74         System.out.println(" loading: " + files[i]);
75         FileInputStream JavaDoc in = new FileInputStream JavaDoc(resultsDir() + File.separator + files[i]);
76         graph = (OrganicObjectGraph) SerializationUtils.deserialize(in);
77         System.out.println(" sequence #: " + graph.sequenceNumber());
78         System.out.println(" change iteration count #: " + graph.changeIterationCount());
79         System.out.println(" envkey: " + graph.envKey());
80         System.out.println("--");
81         hostKey = graph.envKey();
82         if (!hostGraphs.containsKey(hostKey)) {
83           graphs = new ArrayList JavaDoc();
84           graphs.add(graph);
85           hostGraphs.put(hostKey, graphs);
86         } else {
87           graphs = (List JavaDoc) hostGraphs.get(hostKey);
88           graphs.add(graph);
89         }
90       }
91     }
92     System.out.println("");
93     
94     Iterator JavaDoc iter = hostGraphs.entrySet().iterator();
95     while (iter.hasNext()) {
96       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
97       graphs = (ArrayList JavaDoc) entry.getValue();
98       graphArray = new OrganicObjectGraph[graphs.size()];
99
100       // sort graphs by sequence number
101
for (int i = 0; i < graphArray.length; i++) {
102         graph = (OrganicObjectGraph) graphs.get(i);
103         graphArray[graph.sequenceNumber()] = graph;
104       }
105       // validate graphs per host
106
if (!OrganicObjectGraphManager.validate(graphArray, graphSize(), changes())) {
107         System.out.println("\nGraph Validation: Failed");
108         return;
109       }
110     }
111     System.out.println("\nGraph Validation: Passed");
112   }
113 }
114
Popular Tags