1 package org.enhydra.barracuda.testbed.workbench.stress; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.io.*; 6 import java.net.*; 7 import java.util.*; 8 import javax.swing.*; 9 import javax.swing.event.*; 10 11 import org.enhydra.barracuda.plankton.data.*; 12 import org.enhydra.barracuda.plankton.http.*; 13 14 import org.enhydra.barracuda.testbed.workbench.util.gui.Containers; 15 import org.enhydra.barracuda.testbed.workbench.util.gui.Cursors; 16 import org.enhydra.barracuda.testbed.workbench.util.gui.DefaultComponentListener; 17 18 21 public class URLStressTester extends JPanel { 22 23 public static String DEFAULT_URL = "http://localhost:8080/Barracuda/Level5Event.event"; 25 public static String DEFAULT_NUMBER_OF_THREADS = "5"; 26 27 public static int showDebug = 0; 29 30 private int activeThreads = 0; 32 private int threadCntr = 0; 33 private long startTime = 0; 34 private long endTime = 0; 35 private long totalTime = 0; 36 private boolean readySetGo = false; 37 private boolean logToConsole = true; 38 private int wrapOutput = 60; 39 private boolean showDetails = true; 40 private boolean doGet = true; 41 42 private boolean isInitialized = false; 44 45 private JTextField jtfURL = null; 48 private JButton jbRunTest = null; 49 private JButton jbAvgTest = null; 50 private JButton jbAddToReq = null; 51 private JSplitPane jspMaster = null; 53 private JPanel jpRequests = null; 55 private JPanel jpOutput = null; 57 private JTextField jtfNoOfThreads = null; 59 private JRadioButton jrbGet = null; 60 private JRadioButton jrbPost = null; 61 private JTextField jtfLatency = null; 62 private JTextField jtfWrapOutput = null; 63 private JCheckBox jcbShowDetails = null; 64 65 66 69 public URLStressTester() { 70 this.addComponentListener(new LocalComponentListener()); 72 } 73 74 83 private void initViews() throws Exception { 84 85 this.setLayout(new GridBagLayout()); 87 LocalActionAdapter actionAdapter = new LocalActionAdapter(); 88 Insets buttonInsets = new Insets(0, 5, 0, 5); 89 90 JPanel jpURL = new JPanel(); 92 jpURL.setLayout(new GridBagLayout()); 93 this.add(jpURL, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0, 94 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); 95 JLabel jlbURL = new JLabel("URL:"); 97 jpURL.add(jlbURL, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 98 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 99 jtfURL = new JTextField(DEFAULT_URL); 101 jpURL.add(jtfURL, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 1.0, 0.0, 102 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0)); 103 jbRunTest = new JButton("Run Test"); 105 jbRunTest.setToolTipText("Run the test"); 106 jbRunTest.setMargin(buttonInsets); 107 jbRunTest.addActionListener(actionAdapter); 108 jpURL.add(jbRunTest, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 109 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0)); 110 jbAvgTest = new JButton("Avg Test"); 112 jbAvgTest.setToolTipText("Run the test 10x and average the results"); 113 jbAvgTest.setMargin(buttonInsets); 114 jbAvgTest.addActionListener(actionAdapter); 115 jpURL.add(jbAvgTest, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 116 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0)); 117 jbAddToReq = new JButton("+List"); 119 jbAddToReq.setMargin(buttonInsets); 120 jbAddToReq.addActionListener(actionAdapter); 121 jpURL.add(jbAddToReq, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 122 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 123 124 125 126 jspMaster = new JSplitPane(); 128 jspMaster.setOneTouchExpandable(true); 129 this.add(jspMaster, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 1.0, 130 GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0)); 131 132 jpRequests = new JPanel(); 134 jpRequests.setLayout(new GridBagLayout()); 135 jspMaster.add(jpRequests, JSplitPane.LEFT); 136 jpRequests.add(new JLabel("todo"), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 1.0, 138 GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); 139 140 jpOutput = new JPanel(); 142 jpOutput.setLayout(new GridBagLayout()); 143 JScrollPane jspOutput = new JScrollPane(jpOutput); 144 jspMaster.add(jspOutput, JSplitPane.RIGHT); 145 149 jspMaster.setDividerLocation(0); 150 151 152 JPanel jpFooter = new JPanel(); 154 jpFooter.setLayout(new GridBagLayout()); 155 this.add(jpFooter, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0, 156 GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 2, 5), 0, 0)); 157 jpFooter.add(new JLabel(" "), new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 1.0, 0.0, 159 GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); 160 jpFooter.add(new JLabel("Http Method:"), new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 162 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0)); 163 ButtonGroup bg = new ButtonGroup(); 164 jrbGet = new JRadioButton("Get"); 165 bg.add(jrbGet); 166 jrbGet.setSelected(true); 167 jpFooter.add(jrbGet, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 168 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0)); 169 jrbPost = new JRadioButton("Post"); 170 bg.add(jrbPost); 171 jpFooter.add(jrbPost, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 172 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 173 jpFooter.add(new JLabel("No. Threads:"), new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 175 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0)); 176 jtfNoOfThreads = new JTextField(DEFAULT_NUMBER_OF_THREADS, 3); 177 jpFooter.add(jtfNoOfThreads, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 178 GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0)); 179 jpFooter.add(new JLabel("Latency:"), new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 181 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0)); 182 jtfLatency = new JTextField("0", 3); 183 jpFooter.add(jtfLatency, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 184 GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0)); 185 jpFooter.add(new JLabel("Wrap:"), new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 187 GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0)); 188 jtfWrapOutput = new JTextField("60", 2); 189 jpFooter.add(jtfWrapOutput, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 190 GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0)); 191 jcbShowDetails = new JCheckBox("Show Details", true); 193 jpFooter.add(jcbShowDetails, new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0.0, 0.0, 194 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0)); 195 } 196 197 198 public void runTest(int numberOfTimesToRun) { 199 try { 200 Cursors.setWorking(Containers.getRootContainer(URLStressTester.this), true); 202 203 jbRunTest.setEnabled(false); 205 jbAddToReq.setEnabled(false); 206 207 String surl = jtfURL.getText(); 209 int threadCount = 1; 210 try {threadCount = Integer.parseInt(jtfNoOfThreads.getText());} 211 catch (NumberFormatException e) {} 212 int latency = 0; 213 try {latency = Integer.parseInt(jtfLatency.getText());} 214 catch (NumberFormatException e) {} 215 wrapOutput = 60; 216 try {wrapOutput = Integer.parseInt(jtfWrapOutput.getText());} 217 catch (NumberFormatException e) {} 218 doGet = jrbGet.isSelected(); 219 showDetails = jcbShowDetails.isSelected(); 220 int outputCount = (showDetails ? threadCount : 1); 221 222 jpOutput.removeAll(); 224 jpOutput.setLayout(new GridLayout(1,outputCount,0,0)); 225 JTextArea jtxa[] = new JTextArea[outputCount]; 226 for (int i=0; i<outputCount; i++) { 227 jtxa[i] = new JTextArea(""); 228 jtxa[i].setCursor(new Cursor(Cursor.WAIT_CURSOR)); 229 jtxa[i].setMinimumSize(new Dimension(20,100)); 230 jtxa[i].invalidate(); 232 JScrollPane jsp = new JScrollPane(jtxa[i]); 233 jpOutput.add(jsp); 234 } 235 Cursors.setWorking(jpOutput, true); 236 jspMaster.validate(); 238 jspMaster.paintAll(jspMaster.getGraphics()); 239 Thread.yield(); 240 241 long totalMillis = 0; 243 for (int loopCntr=0; loopCntr<numberOfTimesToRun; loopCntr++) { 244 activeThreads=0; 246 threadCntr=0; 247 readySetGo = false; 248 URLRequestor threads[] = new URLRequestor[threadCount]; 249 for (int i=0; i<threadCount; i++) { 250 int ta = 0; 251 if (i<outputCount) ta = i; 252 threads[i] = new URLRequestor(surl, i, jtxa[ta], latency*i); 253 threads[i].start(); 254 } 255 256 for (int i=0; i<threadCount; i++) { 258 if (!threads[i].isRunning) { 259 --i; 260 try { 261 Thread.sleep(50); 262 Thread.yield(); 263 } catch (InterruptedException ie) {} 264 } 265 } 266 267 startTime = System.currentTimeMillis(); 269 totalTime = 0; 270 readySetGo = true; 271 272 for (int i=0; i<threadCount; i++) { 274 try {threads[i].join();} 275 catch (InterruptedException ie) {} 276 } 277 endTime = System.currentTimeMillis(); 278 println (jtxa[0], " "); 279 println (jtxa[0], "Start:"+startTime); 280 println (jtxa[0], "End:"+endTime, true); 281 println (jtxa[0], "Elapsed:"+(endTime-startTime)+" millisecs", true); 282 println (jtxa[0], "Average:"+((totalTime)/threadCount)+" millisecs", true); 283 totalMillis += (endTime-startTime); 284 } 285 println (jtxa[0], " "); 286 long avg = (totalMillis/numberOfTimesToRun); 287 if (numberOfTimesToRun>1) println (jtxa[0], "Average time/run over "+numberOfTimesToRun+" iterations:"+avg+" millisecs", true); 288 println (jtxa[0], "Throughput:"+(threadCount*numberOfTimesToRun*1000/totalMillis)+" reqs/1000 millisecs", true); 289 } finally { 290 jbRunTest.setEnabled(true); 292 jbAddToReq.setEnabled(true); 293 294 Cursors.setWorking(Containers.getRootContainer(URLStressTester.this), false); 296 } 297 } 298 299 303 class URLRequestor extends Thread { 304 305 String surl = null; 306 int threadNo = -1; 307 JTextArea jtxa = null; 308 int latency; 309 boolean isRunning = false; 310 311 public URLRequestor (String iurl, int ithreadNo, JTextArea ijtxa, int ilatency) { 312 surl = iurl; 313 threadNo = ithreadNo; 314 jtxa = ijtxa; 315 jtxa.setText(""); 316 latency = ilatency; 317 if (showDebug>0) println (jtxa, "Creating Thread..."); 318 } 319 320 public void run() { 321 HttpRequester hr = new HttpRequester(); 323 String urlStr = surl; 324 String method = (doGet ? HttpRequester.GET : HttpRequester.POST); 325 Map props = null; 326 int spos = surl.indexOf("?"); 327 if (spos>0) { 328 String paramStr = surl.substring(spos+1); 329 urlStr = surl.substring(0,spos); 330 if (showDebug>0) println (jtxa, "URL Str:"+urlStr); 331 if (showDebug>0) println (jtxa, "Param Str:"+paramStr); 332 props = HttpConverter.cvtURLStringToMap (paramStr, "&"); 333 } 334 try {hr.setRequest (urlStr, method, props);} 335 catch (MalformedURLException e) { 336 println (jtxa, "Bad URL:"+e); 337 return; 338 } 339 340 isRunning = true; 342 343 344 while (!readySetGo) { 346 try { 347 Thread.sleep(10); 348 Thread.yield(); 349 } catch (InterruptedException ie) {} 350 } 351 352 if (latency>0) try { 354 Thread.sleep(latency); 355 Thread.yield(); 356 } catch (InterruptedException ie) {} 357 358 long startTime = System.currentTimeMillis(); 360 activeThreads++; 362 if (showDetails) println (jtxa, "Starting Thread "+threadNo+(latency>0 ? " (lat="+latency+")" : "")); 363 364 boolean success = false; 366 try { 367 376 hr.connect(); 378 379 if (showDetails) println (jtxa, "Headers..."); 382 Map hdrs = hr.getURLConnection().getHeaderFields(); 383 Iterator it = hdrs.entrySet().iterator(); 384 while (it.hasNext()) { 385 Map.Entry me = (Map.Entry) it.next(); 386 if (showDetails) println (jtxa, "key:"+me.getKey()+" val:"+me.getValue()); 387 } 388 389 if (showDetails) println (jtxa, "Data..."); 391 String inputLine = null; 392 while ((inputLine = hr.readLine())!=null) { 393 if (showDetails) println (jtxa, inputLine); 394 Thread.yield(); 395 } 396 hr.disconnect(); 397 success = true; 398 } catch (Exception e) { 399 println (jtxa, "Aborted Thread "+threadNo+" "+e); 400 e.printStackTrace(); 401 } 402 long endTime = System.currentTimeMillis(); 403 incrTotalTime(endTime-startTime); 404 if (success && showDetails) println (jtxa, "Finished Thread "+threadNo+" ("+(endTime-startTime)+" millis)"); 405 } 406 } 407 408 public void println(JTextArea jta, String s) { 409 println (jta, s, logToConsole); 410 } 411 412 public void println(JTextArea jta, String s, boolean logToConsole) { 413 for (int i=0, max=s.length(); i<max; i+=wrapOutput) { 414 int epos = i+wrapOutput; 415 if (epos>max) epos = max; 416 if (i==0) jta.append(s.substring(i,epos)+System.getProperty("line.separator")); 417 else jta.append(" "+s.substring(i,epos)+System.getProperty("line.separator")); 418 } 419 jta.invalidate(); 420 jta.paintAll(jta.getGraphics()); 421 if (logToConsole) System.out.println (s); 422 } 423 424 public synchronized void incrTotalTime(long incr) { 425 totalTime += incr; 426 } 427 428 429 430 434 class LocalComponentListener extends DefaultComponentListener { 435 436 439 public void componentShown(ComponentEvent e) { 440 if (showDebug>1) System.out.println ("Shown:"+e); 441 442 if (!isInitialized) try { 444 initViews(); 445 isInitialized = true; 446 } catch (Exception be) { 447 System.out.println ("Error initializing "+this); 448 be.printStackTrace(); 449 } 450 } 451 } 452 453 457 class LocalActionAdapter implements java.awt.event.ActionListener { 458 459 462 public void actionPerformed(ActionEvent e) { 463 Object o = e.getSource(); 465 if (o==jbAddToReq) { 466 java.awt.Toolkit.getDefaultToolkit().beep(); 467 JOptionPane.showMessageDialog(null, "Not implemented yet", "To Do", JOptionPane.INFORMATION_MESSAGE); 468 } else if (o==jbRunTest) { 469 runTest(1); 470 } else if (o==jbAvgTest) { 471 runTest(10); 472 } 473 } 474 } 475 476 } 477 | Popular Tags |