1 /*
2  * @(#) $Id: JSTKPerfData.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk;
11
12import java.io.OutputStream;
13import java.io.PrintWriter;
14
15public class JSTKPerfData{
16    private long noOfInvocations;
17    private long totalBytes;
18    private long minBytes;
19    private long maxBytes;
20    private long curBytes;
21    private long totalTime;
22    private long minTime;
23    private long maxTime;
24    private long curTime;
25
26    private long startTime;
27
28    private boolean collectPerfData;
29
30    public JSTKPerfData(){
31        reset();
32    }
33
34    public void reset(){
35        collectPerfData = true;;
36        noOfInvocations = 0;
37        totalBytes = totalTime = 0;
38        minBytes = minTime = 999999999;
39        maxBytes = maxTime = 0;
40    }
41
42    public void updateBegin(){
43        if (!collectPerfData)
44            return;
45        this.startTime = System.currentTimeMillis();
46    }
47
48    public void updateEnd(long bytes){
49        if (!collectPerfData)
50            return;
51
52        long duration = System.currentTimeMillis() - startTime;
53        ++noOfInvocations;
54
55        curBytes = bytes;
56        totalBytes += bytes;
57        if (minBytes > curBytes)
58            minBytes = curBytes;
59        if (maxBytes < curBytes)
60            maxBytes = curBytes;
61
62        curTime = duration;
63        totalTime += curTime;
64        if (minTime > curTime)
65            minTime = curTime;
66        if (maxTime < curTime)
67            maxTime = curTime;
68    }
69
70    public boolean getEnabled(){
71        return collectPerfData;
72    }
73    public void setEnabled(boolean state){
74        collectPerfData = state;
75    }
76    public long getNoOfInvocations(){
77        return noOfInvocations;
78    }
79    public long getTotalTime(){
80        return totalTime;
81    }
82    public long getTotalBytes(){
83        return totalBytes;
84    }
85    public void store(OutputStream os){
86        PrintWriter pw = new PrintWriter(os);
87        double procRate = 0;
88        if (totalTime != 0)
89            procRate =  (totalBytes*1000)/(totalTime*1024);
90        pw.println("(N=" + noOfInvocations + ", TT=" + totalTime +
91            " ms, TB=" + totalBytes + " bytes, TB/TT=" + procRate + " KB/s)");
92        pw.flush();
93    }
94}