KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > longrunning > LongrunningGCTestAppCLI


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.longrunning;
5
6 import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
8
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.distrunner.ArgException;
11 import com.tc.simulator.distrunner.ArgParser;
12 import com.tc.simulator.distrunner.SpecFactoryImpl;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tc.simulator.listener.OutputListener;
15 import com.tc.simulator.listener.ResultsListener;
16 import com.tc.simulator.listener.StatsListener;
17 import com.tcsimulator.ApplicationConfigImpl;
18 import com.tcsimulator.listener.OutputListenerObject;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Map.Entry;
24
25 public class LongrunningGCTestAppCLI {
26   private static final String JavaDoc PARTICIPANT_COUNT_ARG = "participantcount";
27   private static int globalParticipantCount;
28   private static int intensity;
29   private static CyclicBarrier barrier;
30
31   public static void main(String JavaDoc[] args) throws ArgException, BrokenBarrierException, InterruptedException JavaDoc {
32     parseArgs(args);
33     barrier = new CyclicBarrier(globalParticipantCount);
34     ArgParser parser = new ArgParser(args, new SpecFactoryImpl(), false, false);
35     intensity = parser.getIntensity();
36
37     String JavaDoc appId = "1";
38     String JavaDoc applicatonClassname = LongrunningGCTestApp.class.getName();
39     ApplicationConfig cfg = new ApplicationConfigImpl(applicatonClassname, intensity, globalParticipantCount);
40     ListenerProvider provider = new TestAppApplicationListenerProvider(new OutputListenerObject(), null);
41     LongrunningGCTestApp testApp = new LongrunningGCTestApp(appId, cfg, provider);
42
43     System.out.println("Waiting for other clients to start...");
44     barrier.barrier();
45     System.out.println("Done waiting for all clients to start.");
46     testApp.run();
47   }
48
49   private static void parseArgs(String JavaDoc[] args) throws ArgException {
50     for (int i = 0; i < args.length; i++) {
51       if (args[i].startsWith(PARTICIPANT_COUNT_ARG)) {
52         String JavaDoc[] nvPair = args[i].split("=");
53         if (nvPair.length != 2) { throw new ArgException("Malformed argument: " + args[i]); }
54         globalParticipantCount = Integer.parseInt(nvPair[1]);
55       }
56     }
57     if (globalParticipantCount == 0) { throw new AssertionError JavaDoc("Participant count must be specified."); }
58   }
59
60   /* ======================================================================= */
61
62   private static class TestAppApplicationListenerProvider implements ListenerProvider {
63     private final OutputListener outputListener;
64     private final ResultsListener resultsListener;
65
66     public TestAppApplicationListenerProvider(OutputListener ol, ResultsListener rl) {
67       this.outputListener = ol;
68       this.resultsListener = rl;
69     }
70
71     public OutputListener getOutputListener() {
72       return outputListener;
73     }
74
75     public ResultsListener getResultsListener() {
76       return resultsListener;
77     }
78
79     public StatsListener newStatsListener(Properties JavaDoc properties) {
80       return new TestAppStatsListenerObject(properties);
81     }
82
83   }
84
85   /* ======================================================================= */
86
87   private static class TestAppStatsListenerObject implements StatsListener {
88     private final String JavaDoc label;
89     private static final String JavaDoc TOKEN = "<app-perf>";
90
91     public TestAppStatsListenerObject(Properties JavaDoc properties) {
92       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93       for (Iterator JavaDoc i = properties.entrySet().iterator(); i.hasNext();) {
94         Map.Entry JavaDoc entry = (Entry) i.next();
95         buf.append(entry.getKey() + "=" + entry.getValue());
96         if (i.hasNext()) {
97           buf.append(",");
98         }
99       }
100       this.label = buf.toString();
101     }
102
103     public void sample(long sampleValue, String JavaDoc desc) {
104       System.out.println(TOKEN + label + ": " + sampleValue);
105     }
106
107   }
108
109 }
110
Popular Tags