View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.performance.upgrade;
20  
21  import java.awt.Color;
22  import java.awt.Container;
23  import java.awt.Dimension;
24  import java.awt.Font;
25  import java.awt.GridBagConstraints;
26  import java.awt.GridBagLayout;
27  import java.awt.Toolkit;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  import java.io.PrintStream;
31  
32  import javax.swing.BorderFactory;
33  import javax.swing.ButtonGroup;
34  import javax.swing.ImageIcon;
35  import javax.swing.JButton;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JPanel;
39  import javax.swing.JRadioButton;
40  import javax.swing.JSlider;
41  import javax.swing.SwingConstants;
42  import javax.swing.border.TitledBorder;
43  import javax.swing.event.ChangeEvent;
44  import javax.swing.event.ChangeListener;
45  
46  import jgroup.core.ConfigManager;
47  import jgroup.core.registry.DependableRegistry;
48  import jgroup.test.performance.SpeedTest;
49  
50  import org.apache.log4j.Logger;
51  
52  /**
53   *  This class implements a Swing-based GUI for the UpgradeClientTest.
54   *  It supports an interface that allows to control the rate at which
55   *  the client sends requests to the server and one of the predefined
56   *  method that is invoked on the upgrade server. 
57   *
58   *  @author Marcin Solarski
59   *  @since Jgroup 2.1
60   */
61  
62  public class Generator implements ChangeListener, ActionListener {
63  
64    /** Obtain logger for this class */
65    private static Logger log = Logger.getLogger(Generator.class);
66  
67  
68    private UpgradeClientTest test = null;
69  
70    private final int TP_MAX = 1000;
71    private final int TP_TICK = 250;
72    private final int METHOD_MAX = 4;
73    private final long RATE_UPDATE_TIME = 2000;
74    private final long EXPERIMENT_GAP = 2000;
75    private float avRespTime = 0;
76    private int method;
77    private int testTime = 5; //in s
78    private int tp;
79    private int rate;
80    private boolean running, calibrated, exiting = false;
81  
82    private JFrame frame;
83    private JButton calButton;
84    private JButton startButton;
85    private JRadioButton b1, b2, b3, b4;
86    private JLabel rateLabel;
87    private JSlider methodSlider;
88    private JPanel methodPanel;
89    private TitledBorder methodBorder;
90    private JSlider tpSlider;
91    private TitledBorder tpBorder;
92    private Font bigFont;
93  
94    class RateChecker extends Thread {
95  
96      public void run() {
97        setPriority(MIN_PRIORITY);
98        while (running) {
99  	rateLabel.setText(""+avRespTime);
100         try {
101           Thread.sleep(RATE_UPDATE_TIME);
102         }
103         catch (Exception ex) {}
104       }
105     }
106   }
107 
108   class Invocator extends Thread {
109    
110    DependableRegistry reg  = null;
111    SpeedTest server  = null;
112    PrintStream out = null;
113    boolean initOK = false; 
114    long ei = 0;
115 
116 
117     public Invocator() {
118     }
119 
120     private void contactServer() { 
121       while (! initOK ) {
122         try {
123           test = new UpgradeClientTest("UpgradeServer");
124           initOK = true;
125         } catch(Exception e) {
126 	   log.warn("Cannot contact the server. Waiting 5 sec and trying again..." + e.getMessage());
127 	   try{ Thread.sleep(5000); } catch(InterruptedException ex) {}
128 	}
129       }
130    }
131 
132 
133     public void run() {
134       contactServer();
135       calButton.setEnabled(true);
136       //the test invocation handling should have the highest priority
137       setPriority(MAX_PRIORITY);
138       try {
139         while ( true ) { 
140 	  if( running ) {
141 	     log.debug("Experiment " + ei + " with rate = " + tp + " test time = " + testTime );
142 	     avRespTime = test.generate_traffic(tp, testTime); 
143 	     rateLabel.setText(""+avRespTime);
144 	     ei++;
145 	  }
146 	  if( exiting ) break;
147 	  try {Thread.sleep(EXPERIMENT_GAP); } catch(InterruptedException e) {};
148 	}
149       }
150       catch(Exception e) {
151         log.info("While sending request, an exception caught: ", e);
152       }
153     }
154   }
155 
156   public Generator(int method, int tp) {
157     this.method = method;
158     this.tp = tp;
159 
160 
161    if (this.method < 0) this.method = 0;
162    if (this.method > METHOD_MAX) this.method = METHOD_MAX;
163    if (this.tp < 1) this.tp = 1;
164    if (this.tp > TP_MAX) this.tp = TP_MAX;
165 
166   }
167 
168   
169     public void actionPerformed(ActionEvent e) {
170       Object src = e.getSource(); 
171       if (src == calButton) {
172 	    log.debug("Calibration is started");
173 	    rateLabel.setText(""+test.calibrate(method));
174 	    calibrated = true;
175             startButton.setEnabled(true);
176 	    log.debug("Calibration done");
177        } else if (src == startButton ) {
178          if (running ) {
179 	         running = false;
180 	        log.debug("Experiment will be stoped");
181 		startButton.setText("Start");
182 	 } else {
183 	        running = true;
184 	        log.debug("Experiment will be started");
185 		startButton.setText("Stop");
186          }
187        } else if (src == b1) {
188 	       log.debug("Changing method to 1");
189         } else if (src == b2) {
190 	       log.debug("Changing method to 2");
191         } else if (src == b3) {
192 	       log.debug("Changing method to 3");
193         } else if (src == b4) {
194 	       log.debug("Changing method to 4");
195       }
196 
197     }
198 
199 
200   public void stateChanged(ChangeEvent evt) {
201     Object src = evt.getSource();
202     //log.debug(evt);
203     if (src == methodSlider) {
204       method = methodSlider.getValue();
205       method = (method + 1) % 4;
206       methodBorder.setTitle(" Method  " + method );
207     } else if (src == tpSlider) {
208       tp = tpSlider.getValue();
209       tpBorder.setTitle(" troughput " + tp + " rps ");
210       tp++;
211     } 
212   }
213 
214   public void init() {
215 
216     bigFont = new Font( null, Font.BOLD, 
217 	 new Integer( System.getProperty("jgroup.test.font.big", "22")).intValue());
218     
219     frame = new JFrame("Generator" );
220 
221     GridBagLayout gridbag = new GridBagLayout();
222     GridBagConstraints constr = new GridBagConstraints();
223     Container cpane = frame.getContentPane();
224     cpane.setLayout(gridbag);
225 
226     calButton = new JButton("Calibrate", new ImageIcon("resources/configure.jpg", "Calibrate"));
227     calButton.setDefaultCapable(true); 
228     calButton.addActionListener(this);
229     calButton.setEnabled(false);
230     constr.fill = GridBagConstraints.BOTH;
231     constr.weightx = 0.5;
232     constr.weighty = 0.5;
233     constr.gridx = 0;
234     constr.gridy = 2;
235     constr.gridwidth = 1;
236     gridbag.setConstraints(calButton, constr);
237     cpane.add(calButton);
238 
239    startButton = new JButton("Start", new ImageIcon("resources/start.jpg", "Start"));
240     startButton.setDefaultCapable(true); 
241     startButton.addActionListener(this);
242     startButton.setEnabled(false);
243     constr.fill = GridBagConstraints.BOTH;
244     constr.weightx = 0.5;
245     constr.weighty = 0.5;
246     constr.gridx = 1;
247     constr.gridy = 2;
248     constr.gridwidth = 1;
249     gridbag.setConstraints(startButton, constr);
250     cpane.add(startButton);
251 
252      
253     rateLabel = new JLabel(rate + " ");
254     rateLabel.setFont(bigFont);
255     rateLabel.setForeground(Color.black);
256     rateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
257     rateLabel.setBorder(BorderFactory.createTitledBorder(" Response Time  "));
258     constr.fill = GridBagConstraints.BOTH;
259     constr.weightx = 1.0;
260     constr.weighty = 0.5;
261     constr.gridx = 2;
262     constr.gridy = 2;
263     constr.gridwidth = 1;
264     gridbag.setConstraints(rateLabel, constr);
265     cpane.add(rateLabel);
266 
267     JLabel logo = new JLabel(new ImageIcon("resources/logo.jpg", "Logo"));
268     constr.fill = GridBagConstraints.BOTH;
269     constr.weightx = 1.0;
270     constr.weighty = 0.5;
271     constr.gridx = 4;
272     constr.gridy = 2;
273     constr.gridwidth = 1;
274     gridbag.setConstraints(logo, constr);
275     cpane.add(logo);
276 
277     methodPanel = new JPanel();
278     methodPanel.setBorder( BorderFactory.createTitledBorder(" method to invoke"  ));
279     ButtonGroup methodGroup = new ButtonGroup();
280     b1 = new JRadioButton("test()", true);
281     methodGroup.add(b1);
282     methodPanel.add(b1);
283     b1.addActionListener(this);
284     b2 = new JRadioButton("mtest()", false);
285     methodGroup.add(b2);
286     methodPanel.add(b2);
287     b2.addActionListener(this);
288     b3 = new JRadioButton("test(byte[])", false);
289     methodGroup.add(b3);
290     methodPanel.add(b3);
291     b3.addActionListener(this);
292     b4 = new JRadioButton("mtest(byte[])", false);
293     methodGroup.add(b4);
294     methodPanel.add(b4);
295     b4.addActionListener(this);
296     constr.fill = GridBagConstraints.BOTH;
297     constr.weightx = 1.0;
298     constr.weighty = 0.5;
299     constr.gridx = 0;
300     constr.gridy = 0;
301     constr.gridwidth = 3;
302     gridbag.setConstraints(methodPanel, constr);
303     cpane.add(methodPanel);
304 
305 
306    tpBorder =
307       BorderFactory.createTitledBorder(" throughput " + tp + " rps ");
308 
309     tpSlider = new JSlider(SwingConstants.HORIZONTAL, 0, TP_MAX, tp);
310     tpSlider.setBorder(tpBorder);
311     tpSlider.setMajorTickSpacing(TP_TICK);
312     tpSlider.setMinorTickSpacing(TP_TICK / 2);
313     tpSlider.setPaintTicks(true);
314     tpSlider.setPaintLabels(true);
315     tpSlider.addChangeListener(this);
316     constr.fill = GridBagConstraints.BOTH;
317     constr.weightx = 1.0;
318     constr.weighty = 0.5;
319     constr.gridx = 0;
320     constr.gridy = 1;
321     constr.gridwidth = 3;
322     gridbag.setConstraints(tpSlider, constr);
323     cpane.add(tpSlider);
324 
325     
326     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
327     frame.setSize(400, 220);
328     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
329     frame.setLocation(
330       (d.width - frame.getSize().width) / 2,
331       (d.height - frame.getSize().height) / 2);
332     frame.setVisible(true);
333 
334     calibrated = false;
335     running = false;
336 
337     //RateChecker checker = new RateChecker();
338     //checker.start();
339     Invocator invocator = new Invocator();
340     invocator.start();
341   }
342 
343   public static void main(String args[])
344   {
345     int method = 0;
346     int tp = 1;
347     try {
348       ConfigManager.init();
349       Generator gen = new Generator(method, tp);
350       gen.init();
351     } catch (Exception ex) {
352       ex.printStackTrace();
353     }
354   }
355 }