KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > http > load > StatsCollector


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
13 import java.io.Serializable JavaDoc;
14 import java.text.NumberFormat JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.Date JavaDoc;
17
18 final class StatsCollector implements Serializable JavaDoc {
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 JavaDoc END = new Object JavaDoc();
27   private long lastStartTime = -1;
28   private long firstStat = -1;
29   private final NumberFormat JavaDoc format = NumberFormat.getInstance();
30   private final SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("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 JavaDoc()) + ": 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 JavaDoc e) {
75       e.printStackTrace();
76       throw new RuntimeException JavaDoc(e);
77     }
78   }
79
80   public static StatsCollector read(InputStream JavaDoc in) {
81     return (StatsCollector) SerializationUtils.deserialize(in);
82   }
83
84   public ResponseStatistic takeStat() {
85     try {
86       Object JavaDoc o = stats.take();
87       if (o == END) { return null; }
88       return (ResponseStatistic) o;
89     } catch (InterruptedException JavaDoc e) {
90       e.printStackTrace();
91       throw new RuntimeException JavaDoc(e);
92     }
93   }
94
95   public void finalStat() {
96     try {
97       stats.put(END);
98     } catch (InterruptedException JavaDoc e) {
99       e.printStackTrace();
100       throw new RuntimeException JavaDoc(e);
101     }
102   }
103
104 }
105
Popular Tags