View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * The SocketAdapter is the main-class for registration of the classes that will
48   * be benched and creation of the socket stub to the ide view.
49   * 
50   * @author Lukas Lewandowski, University of Konstanz
51   * @author Sebastian Graf, University of Konstanz
52   */
53  public final class SocketAdapter {
54  
55      /** Instance for this run of the adapter */
56      private transient Benchmark benchmark;
57  
58      /** View instance for communicating with the perclipse plugin */
59      private transient final IUpdater view;
60  
61      /**
62       * public constructor.
63       * 
64       * @throws IllegalAccessException
65       * @throws InstantiationException
66       * @throws ClassNotFoundException
67       * @throws SocketViewException
68       */
69      public SocketAdapter(final IUpdater update, final String... classes) throws SocketViewException,
70          ClassNotFoundException, InstantiationException, IllegalAccessException {
71          view = update;
72  
73          // config adaptions for including the view
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          // Building up the benchmark object
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       * Registering all classes and getting a mapping with the Methods and the
96       * corresponding overall runs
97       * 
98       * @param classNames
99       *            the names of the classes to be benched
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      * This method starts the bench progress with the registered classes.
114      * 
115      * @throws SocketViewException
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      * Main method for invoking benchs with classes as strings.
126      * 
127      * @param args
128      *            the classes
129      */
130     public static void main(final String[] args) {
131         // init of the connection to the plugin
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 }