001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.longrunning;
006:
007: import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
008: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
009:
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.distrunner.ArgException;
012: import com.tc.simulator.distrunner.ArgParser;
013: import com.tc.simulator.distrunner.SpecFactoryImpl;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.simulator.listener.MutationCompletionListener;
016: import com.tc.simulator.listener.OutputListener;
017: import com.tc.simulator.listener.ResultsListener;
018: import com.tc.simulator.listener.StatsListener;
019: import com.tcsimulator.ApplicationConfigImpl;
020: import com.tcsimulator.listener.OutputListenerObject;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.Map.Entry;
026:
027: public class LongrunningGCTestAppCLI {
028: private static final String PARTICIPANT_COUNT_ARG = "participantcount";
029: private static int globalParticipantCount;
030: private static int intensity;
031: private static CyclicBarrier barrier;
032:
033: public static void main(String[] args) throws ArgException,
034: BrokenBarrierException, InterruptedException {
035: parseArgs(args);
036: barrier = new CyclicBarrier(globalParticipantCount);
037: ArgParser parser = new ArgParser(args, new SpecFactoryImpl(),
038: false, false);
039: intensity = parser.getIntensity();
040:
041: String appId = "1";
042: String applicatonClassname = LongrunningGCTestApp.class
043: .getName();
044: ApplicationConfig cfg = new ApplicationConfigImpl(
045: applicatonClassname, intensity, globalParticipantCount);
046: ListenerProvider provider = new TestAppApplicationListenerProvider(
047: new OutputListenerObject(), null);
048: LongrunningGCTestApp testApp = new LongrunningGCTestApp(appId,
049: cfg, provider);
050:
051: System.out.println("Waiting for other clients to start...");
052: barrier.barrier();
053: System.out.println("Done waiting for all clients to start.");
054: testApp.run();
055: }
056:
057: private static void parseArgs(String[] args) throws ArgException {
058: for (int i = 0; i < args.length; i++) {
059: if (args[i].startsWith(PARTICIPANT_COUNT_ARG)) {
060: String[] nvPair = args[i].split("=");
061: if (nvPair.length != 2) {
062: throw new ArgException("Malformed argument: "
063: + args[i]);
064: }
065: globalParticipantCount = Integer.parseInt(nvPair[1]);
066: }
067: }
068: if (globalParticipantCount == 0) {
069: throw new AssertionError(
070: "Participant count must be specified.");
071: }
072: }
073:
074: /* ======================================================================= */
075:
076: private static class TestAppApplicationListenerProvider implements
077: ListenerProvider {
078: private final OutputListener outputListener;
079: private final ResultsListener resultsListener;
080:
081: public TestAppApplicationListenerProvider(OutputListener ol,
082: ResultsListener rl) {
083: this .outputListener = ol;
084: this .resultsListener = rl;
085: }
086:
087: public OutputListener getOutputListener() {
088: return outputListener;
089: }
090:
091: public ResultsListener getResultsListener() {
092: return resultsListener;
093: }
094:
095: public StatsListener newStatsListener(Properties properties) {
096: return new TestAppStatsListenerObject(properties);
097: }
098:
099: public MutationCompletionListener getMutationCompletionListener() {
100: throw new AssertionError(
101: "This method needs to be implemented");
102: }
103:
104: }
105:
106: /* ======================================================================= */
107:
108: private static class TestAppStatsListenerObject implements
109: StatsListener {
110: private final String label;
111: private static final String TOKEN = "<app-perf>";
112:
113: public TestAppStatsListenerObject(Properties properties) {
114: StringBuffer buf = new StringBuffer();
115: for (Iterator i = properties.entrySet().iterator(); i
116: .hasNext();) {
117: Map.Entry entry = (Entry) i.next();
118: buf.append(entry.getKey() + "=" + entry.getValue());
119: if (i.hasNext()) {
120: buf.append(",");
121: }
122: }
123: this .label = buf.toString();
124: }
125:
126: public void sample(long sampleValue, String desc) {
127: System.out.println(TOKEN + label + ": " + sampleValue);
128: }
129:
130: }
131:
132: }
|