1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.socketadapter;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36 import org.perfidix.AbstractConfig;
37 import org.perfidix.Benchmark;
38 import org.perfidix.Perfidix;
39 import org.perfidix.element.BenchmarkMethod;
40 import org.perfidix.exceptions.SocketViewException;
41 import org.perfidix.meter.AbstractMeter;
42 import org.perfidix.ouput.AbstractOutput;
43 import org.perfidix.ouput.TabularSummaryOutput;
44 import org.perfidix.result.BenchmarkResult;
45
46
47
48
49
50
51
52
53 public final class SocketAdapter {
54
55
56 private transient Benchmark benchmark;
57
58
59 private transient final IUpdater view;
60
61
62
63
64
65
66
67
68
69 public SocketAdapter(final IUpdater update, final String... classes) throws SocketViewException,
70 ClassNotFoundException, InstantiationException, IllegalAccessException {
71 view = update;
72
73
74 final AbstractConfig oldConf = Perfidix.getConfiguration(classes);
75 final AbstractOutput[] outputs = new AbstractOutput[oldConf.getListener().length + 1];
76 System.arraycopy(oldConf.getListener(), 0, outputs, 0, oldConf.getListener().length);
77 outputs[outputs.length - 1] = new SocketListener(view);
78
79 Set<AbstractMeter> meters = new HashSet<AbstractMeter>();
80 meters.addAll(Arrays.asList(oldConf.getMeters()));
81
82 Set<AbstractOutput> listeners = new HashSet<AbstractOutput>();
83 listeners.addAll(Arrays.asList(outputs));
84
85
86 final AbstractConfig newConf =
87 new AbstractConfig(oldConf.getRuns(), meters, listeners, oldConf.getArrangement(), oldConf
88 .getGcProb()) {
89 };
90 benchmark = new Benchmark(newConf);
91
92 }
93
94
95
96
97
98
99
100
101 public boolean registerClasses(final String... classNames) throws SocketViewException {
102 try {
103 benchmark = Perfidix.setUpBenchmark(classNames, benchmark);
104
105 final Map<BenchmarkMethod, Integer> vals = benchmark.getNumberOfMethodsAndRuns();
106 return view.initProgressView(vals);
107 } catch (final ClassNotFoundException e2) {
108 return view.updateErrorInElement(e2.toString(), e2);
109 }
110 }
111
112
113
114
115
116
117 public boolean runBenchmark() throws SocketViewException {
118 final BenchmarkResult res = benchmark.run();
119 new TabularSummaryOutput().visitBenchmark(res);
120 view.finished();
121 return true;
122 }
123
124
125
126
127
128
129
130 public static void main(final String[] args) {
131
132 int viewPort = 0;
133 final List<String> classList = new ArrayList<String>();
134 for (int i = 0; i < args.length; i++) {
135 if (args[i].equals("-Port")) {
136 if (args[i + 1] != null) {
137 viewPort = Integer.parseInt(args[i + 1]);
138 }
139 break;
140 } else {
141 classList.add(args[i]);
142 }
143 }
144 try {
145
146 final IUpdater updater = new SocketViewProgressUpdater(null, viewPort);
147
148 final SocketAdapter adapter =
149 new SocketAdapter(updater, classList.toArray(new String[classList.size()]));
150
151 adapter.registerClasses(classList.toArray(new String[classList.size()]));
152 adapter.runBenchmark();
153 } catch (final SocketViewException e) {
154 throw new IllegalStateException(e);
155 } catch (ClassNotFoundException e) {
156 throw new IllegalStateException(e);
157 } catch (InstantiationException e) {
158 throw new IllegalStateException(e);
159 } catch (IllegalAccessException e) {
160 throw new IllegalStateException(e);
161 }
162 }
163
164 }