Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/util/SystemWatch.java


1   //
2   // Copyright(c) 2002, Chris Leung
3   //
4   
5   package com.port80.util;
6   
7   /** Watch for system status (time, memory footprint, ... etc).
8    *
9    *  Usage: 
10   *    SystemWatch t1=new SystemWatch().start();
11   *    t1.stop();
12   *    msg.println(t1.toString());
13   *    // Cumulate elapsed time, recalculate used memory size.
14   *    t1.start();
15   *    t1.stop();
16   *    // Reset elapsed time, recalculate used memory size.
17   *    t1.restart();
18   *    t1.stop();
19   */
20  public class SystemWatch {
21  
22      // Instance fields /////////////////////////////////////////////////////
23      //
24  
25      private String format=null;
26      private float elapsed=0f;
27      private long usedSize=0;
28      private long totalSize=0;
29      //
30      private long startTime=-1;
31      private long startSize=-1;
32      private Runtime runtime=null;
33  
34      // Constructors ////////////////////////////////////////////////////////
35      //
36  
37      public SystemWatch() {
38    runtime=Runtime.getRuntime();
39      }
40      public SystemWatch(String format) {
41    runtime=Runtime.getRuntime();
42    this.format=format;
43      }
44      
45      // Instance methods ////////////////////////////////////////////////////
46      //
47  
48      public SystemWatch start() { 
49    startTime=System.currentTimeMillis();
50    startSize=runtime.totalMemory()-runtime.freeMemory();
51    return this;
52      }
53      public SystemWatch stop() { 
54    if(startTime>0) elapsed+=(System.currentTimeMillis()-startTime)/1000f;
55    else msg.warn("SystemWatch.stop(): watch is not running. startTime="+startTime);
56    totalSize=runtime.totalMemory()-runtime.freeMemory();
57    usedSize=totalSize-startSize;
58    startTime=-1;
59    startSize=-1;
60    return this;
61      }
62      public void restart() {
63    elapsed=0f;
64    usedSize=0;
65    totalSize=0;
66    startTime=System.currentTimeMillis();
67    startSize=runtime.totalMemory()-runtime.freeMemory();
68      }
69      public void reset() {
70    elapsed=0f;
71    usedSize=0;
72    totalSize=0;
73    startTime=-1;
74    startSize=-1;
75      }
76  
77      public float elapsed() {
78    if(startTime<0) return elapsed;
79    return (System.currentTimeMillis()-startTime)/1000f;
80      }
81      public long size() { 
82    if(startSize<0) return usedSize;
83    return runtime.totalMemory()-runtime.freeMemory()-startSize;
84      }
85  
86      /** @return current used memory or when watch is stopped. */
87      public long totalSize() {
88    if(startSize<0) return totalSize;
89    else return runtime.totalMemory()-runtime.freeMemory();
90      }
91  
92      public String toString() {
93    if(format==null) return sprint.f("%-.3f (sec), %-.2f (M)").a(elapsed).a(usedSize/1e6).end();
94    else return sprint.f(format).a(elapsed).a(usedSize/1e6).end();
95      }
96  
97      // Test ////////////////////////////////////////////////////////////////
98      //
99  
100     public static void main(String[] args) {
101   test();
102     }
103 
104     private static void test() {
105   SystemWatch t1=new SystemWatch().start();
106   SystemWatch t2=new SystemWatch("%10.4f(sec),%.3f(M)").start();
107   System.out.println("### SystemWatch started, any key to stop ... ");
108   try {
109       int c=System.in.read();
110   } catch(Exception e) {msg.err(e);}
111   t1.stop();
112   Runtime.getRuntime().gc();
113   t2.stop();
114   System.out.println(t1.toString());
115   System.out.println(t2.toString());
116     }
117     
118     ////////////////////////////////////////////////////////////////////////
119     
120 }