1 5 package com.tctest.performance.http.load; 6 7 import org.apache.commons.httpclient.HttpStatus; 8 import org.apache.commons.lang.SerializationUtils; 9 10 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 11 12 import java.io.InputStream ; 13 import java.io.Serializable ; 14 import java.text.NumberFormat ; 15 import java.text.SimpleDateFormat ; 16 import java.util.Date ; 17 18 final class StatsCollector implements Serializable { 19 20 private static final long REPORT = 500; 21 22 private int count; 23 private int success = 0; 24 private int errors = 0; 25 private final LinkedQueue stats = new LinkedQueue(); 26 private final Object END = new Object (); 27 private long lastStartTime = -1; 28 private long firstStat = -1; 29 private final NumberFormat format = NumberFormat.getInstance(); 30 private final SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss,SSS"); 31 32 public StatsCollector() { 33 format.setMaximumFractionDigits(2); 34 } 35 36 void addStat(ResponseStatistic stat) { 37 38 synchronized (this) { 39 if (firstStat == -1) { 40 firstStat = System.currentTimeMillis(); 41 } 42 43 count++; 44 45 boolean isSuccess = stat.statusCode() == HttpStatus.SC_OK; 46 if (isSuccess) { 47 success++; 48 } else { 49 errors++; 50 } 51 52 if ((count % REPORT) == 0) { 53 final long start = lastStartTime == -1 ? firstStat : lastStartTime; 54 final long end = System.currentTimeMillis(); 55 56 if (end < start) { 57 System.out.println("\n**********************\nWARN: backward clock movement detected (" + end + " < " + start 58 + ")\n**********************\n"); 59 } 60 61 final double rate = ((double) (REPORT * 1000L)) / ((double) (end - start)); 62 final double overallRate = ((double) (count * 1000L)) / ((double) (end - firstStat)); 63 64 System.out.println(dateFormat.format(new Date ()) + ": Completed " + count + " requests (" + success + " OK, " 65 + errors + " errors), " + format.format(rate) + " tps, overall " 66 + format.format(overallRate) + " tps"); 67 68 lastStartTime = end; 69 } 70 } 71 72 try { 73 stats.put(stat); 74 } catch (InterruptedException e) { 75 e.printStackTrace(); 76 throw new RuntimeException (e); 77 } 78 } 79 80 public static StatsCollector read(InputStream in) { 81 return (StatsCollector) SerializationUtils.deserialize(in); 82 } 83 84 public ResponseStatistic takeStat() { 85 try { 86 Object o = stats.take(); 87 if (o == END) { return null; } 88 return (ResponseStatistic) o; 89 } catch (InterruptedException e) { 90 e.printStackTrace(); 91 throw new RuntimeException (e); 92 } 93 } 94 95 public void finalStat() { 96 try { 97 stats.put(END); 98 } catch (InterruptedException e) { 99 e.printStackTrace(); 100 throw new RuntimeException (e); 101 } 102 } 103 104 } 105 | Popular Tags |