1 18 19 package org.apache.jmeter.reporters; 20 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Graphics ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.io.BufferedReader ; 29 import java.io.File ; 30 import java.io.FileReader ; 31 import java.io.IOException ; 32 import java.text.DecimalFormat ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Vector ; 36 37 import javax.swing.JFrame ; 38 import javax.swing.JLabel ; 39 import javax.swing.JOptionPane ; 40 import javax.swing.JPanel ; 41 42 import org.apache.jorphan.logging.LoggingManager; 43 import org.apache.log.Logger; 44 45 46 54 public class FileReporter extends JPanel 55 { 56 transient private static Logger log 57 = LoggingManager.getLoggerForClass(); 58 Hashtable data = new Hashtable (); 59 60 61 public void init(String file) throws IOException 62 { 63 File datafile = new File (file); 64 BufferedReader reader = null; 65 66 try 67 { 68 if (datafile.canRead()) 69 { 70 reader = new BufferedReader (new FileReader (datafile)); 71 } 72 else 73 { 74 JOptionPane.showMessageDialog( 75 null, "The file you specified cannot be read.", 76 "Information", JOptionPane.INFORMATION_MESSAGE); 77 return; 78 } 79 String line; 80 81 while ((line = reader.readLine()) != null) 82 { 83 try 84 { 85 line = line.trim(); 86 if (line.startsWith("#") || line.length() == 0) 87 { 88 continue; 89 } 90 int splitter = line.lastIndexOf(' '); 91 String key = line.substring(0, splitter); 92 int len = line.length() - 1; 93 Integer value = null; 94 95 if (line.charAt(len) == ',') 96 { 97 value = new Integer ( 98 line.substring(splitter + 1, len)); 99 } 100 else 101 { 102 value = new Integer ( 103 line.substring(splitter + 1)); 104 } 105 Vector v = getData(key); 106 107 if (v == null) 108 { 109 v = new Vector (); 110 this.data.put(key, v); 111 } 112 v.addElement(value); 113 } 114 catch (NumberFormatException nfe) 115 { 116 log.error("This line could not be parsed: " + line, nfe); 117 } 118 catch (Exception e) 119 { 120 log.error("This line caused a problem: " + line, e); 121 } 122 } 123 } 124 finally 125 { 126 if (reader != null) reader.close(); 127 } 128 showPanel(); 129 } 130 131 public Vector getData(String key) 132 { 133 return (Vector ) data.get(key); 134 } 135 136 139 public void showPanel() 140 { 141 JFrame f = new JFrame ("Data File Report"); 142 143 setLayout(new BorderLayout ()); 144 graphPanel gp = new graphPanel(data); 145 146 add(gp, "Center"); 147 add(gp.getStats(), BorderLayout.EAST); 148 add(gp.getLegend(), BorderLayout.NORTH); 149 f.setSize(500, 300); 150 f.getContentPane().add(this); 151 f.show(); 152 } 153 } 154 155 156 161 class graphPanel extends JPanel 162 { 163 Hashtable data; 165 Vector keys = new Vector (); 166 Vector colorList = new Vector (); 167 public graphPanel() 168 {} 169 170 public graphPanel(Hashtable data) 171 { 172 this.data = data; 173 Enumeration e = data.keys(); 174 175 while (e.hasMoreElements()) 176 { 177 String key = (String ) e.nextElement(); 178 179 keys.addElement(key); 180 } 181 for (int a = 0x33; a < 0xFF; a += 0x66) 182 { 183 for (int b = 0x33; b < 0xFF; b += 0x66) 184 { 185 for (int c = 0x33; c < 0xFF; c += 0x66) 186 { 187 colorList.addElement(new Color (a, b, c)); 188 } 189 } 190 } 191 } 192 193 196 public float getMax() 197 { 198 float maxValue = 0; 199 200 for (int t = 0; t < keys.size(); t++) 201 { 202 String key = (String ) keys.elementAt(t); 203 Vector temp = (Vector ) data.get(key); 204 205 for (int j = 0; j < temp.size(); j++) 206 { 207 float f = ((Integer ) temp.elementAt(j)).intValue(); 208 209 maxValue = Math.max(f, maxValue); 210 } 211 } 212 return (float) (maxValue + maxValue * 0.1); 213 } 214 215 218 public float getMin() 219 { 220 float minValue = 9999999; 221 222 for (int t = 0; t < keys.size(); t++) 223 { 224 String key = (String ) keys.elementAt(t); 225 Vector temp = (Vector ) data.get(key); 226 227 for (int j = 0; j < temp.size(); j++) 228 { 229 float f = ((Integer ) temp.elementAt(j)).intValue(); 230 231 minValue = Math.min(f, minValue); 232 } 233 } 234 return (float) (minValue - minValue * 0.1); 235 } 236 237 240 public JPanel getLegend() 241 { 242 JPanel main = new JPanel (); 243 GridBagLayout g = new GridBagLayout (); 244 245 main.setLayout(g); 246 GridBagConstraints c = new GridBagConstraints (); 247 248 c.insets = new Insets (3, 3, 3, 3); 249 c.fill = GridBagConstraints.BOTH; 250 c.gridwidth = 1; 251 c.gridheight = 1; 252 for (int t = 0; t < keys.size(); t++) 253 { 254 String key = (String ) keys.elementAt(t); 255 JLabel colorSwatch = new JLabel (" "); 256 257 colorSwatch.setBackground( 258 (Color ) colorList.elementAt(t % colorList.size())); 259 colorSwatch.setOpaque(true); 260 c.gridx = 1; 261 c.gridy = t; 262 g.setConstraints(colorSwatch, c); 263 main.add(colorSwatch); 264 JLabel name = new JLabel (key); 265 266 c.gridx = 2; 267 c.gridy = t; 268 g.setConstraints(name, c); 269 main.add(name); 270 } 271 return main; 272 } 273 274 277 public JPanel getStats() 278 { 279 int total = 0; 280 float totalValue = 0; 281 float maxValue = 0; 282 float minValue = 999999; 283 284 for (int t = 0; t < keys.size(); t++) 285 { 286 String key = (String ) keys.elementAt(t); 287 Vector temp = (Vector ) data.get(key); 288 289 for (int j = 0; j < temp.size(); j++) 290 { 291 float f = ((Integer ) temp.elementAt(j)).intValue(); 292 293 minValue = Math.min(f, minValue); 294 maxValue = Math.max(f, maxValue); 295 totalValue += f; 296 total++; 297 } 298 } 299 float averageValue = totalValue / total; 300 JPanel main = new JPanel (); 301 GridBagLayout g = new GridBagLayout (); 302 303 main.setLayout(g); 304 DecimalFormat df = new DecimalFormat ("#0.0"); 305 GridBagConstraints c = new GridBagConstraints (); 306 307 c.insets = new Insets (3, 6, 3, 6); 308 c.fill = GridBagConstraints.BOTH; 309 c.gridwidth = 1; 310 c.gridheight = 1; 311 JLabel count = new JLabel ("Count: " + total); 312 313 c.gridx = 1; 314 c.gridy = 1; 315 g.setConstraints(count, c); 316 JLabel min = new JLabel ("Min: " + df.format(new Float (minValue))); 317 318 c.gridx = 1; 319 c.gridy = 2; 320 g.setConstraints(min, c); 321 JLabel max = new JLabel ("Max: " + df.format(new Float (maxValue))); 322 323 c.gridx = 1; 324 c.gridy = 3; 325 g.setConstraints(max, c); 326 JLabel average = 327 new JLabel ("Average: " + df.format(new Float (averageValue))); 328 329 c.gridx = 1; 330 c.gridy = 4; 331 g.setConstraints(average, c); 332 main.add(count); 333 main.add(min); 334 main.add(max); 335 main.add(average); 336 return main; 337 } 338 339 342 public int getDataWidth() 343 { 344 int size = 0; 345 346 for (int t = 0; t < keys.size(); t++) 347 { 348 String key = (String ) keys.elementAt(t); 349 Vector v = (Vector ) data.get(key); 350 351 size = Math.max(size, v.size()); 352 } 353 return size; 354 } 355 356 359 public void update(Graphics g) 360 { 361 int base = 10; 363 364 g.setColor(Color.white); 365 g.fillRect(0, 0, getSize().width, getSize().height); 366 int width = getSize().width; 367 int height = getSize().height; 368 float maxValue = getMax(); 369 float minValue = getMin(); 370 371 g.setColor(Color.gray); 373 int dataWidth = getDataWidth(); 374 int increment = Math.round((width - 1) / (dataWidth - 1)); 375 376 379 int yIncrement = Math.round(((float) height - (1 + base)) / (10 - 1)); 380 381 384 for (int t = 1; t < dataWidth; t += (dataWidth / 25 + 1)) 386 { 387 g.drawString( 388 (new Integer (t)).toString(), 389 t * increment + 2, 390 height - 2); 391 } 392 float incrementValue = (maxValue - minValue) / (10 - 1); 393 394 for (int t = 0; t < 10; t++) 395 { 396 g.drawString( 397 new Integer (Math.round(minValue + (t * incrementValue))) 398 .toString(), 399 2, 400 height - t * yIncrement - 2 - base); 401 } 402 int start = 0; 404 405 for (int t = 0; t < keys.size(); t++) 406 { 407 String key = (String ) keys.elementAt(t); 408 Vector v = (Vector ) data.get(key); 409 410 start = 0; 411 g.setColor((Color ) colorList.elementAt(t % colorList.size())); 412 for (int i = 0; i < v.size() - 1; i++) 413 { 414 float y1 = (float) ((Integer ) v.elementAt(i)).intValue(); 415 float y2 = (float) ((Integer ) v.elementAt(i + 1)).intValue(); 416 417 y1 = y1 - minValue; 418 y2 = y2 - minValue; 419 int Y1 = Math.round((height * y1) / (maxValue - minValue)); 420 int Y2 = Math.round((height * y2) / (maxValue - minValue)); 421 422 Y1 = height - Y1 - base; 423 Y2 = height - Y2 - base; 424 g.drawLine(start, Y1, start + increment, Y2); 425 426 start += increment; 427 } 428 } 429 } 430 431 public void paint(Graphics g) 432 { 433 update(g); 434 } 435 } 436 | Popular Tags |