1 19 20 package org.netbeans.core.startup.preferences; 21 22 26 public final class Statistics { 27 28 29 public static final Statistics.TimeConsumer FLUSH = 30 new Statistics.TimeConsumer("Flush"); public static final Statistics.TimeConsumer LOAD = 32 new Statistics.TimeConsumer("Load"); public static final Statistics.TimeConsumer REMOVE_NODE = 34 new Statistics.TimeConsumer("Remove node"); public static final Statistics.TimeConsumer CHILDREN_NAMES = 36 new Statistics.TimeConsumer("Children names"); 38 39 private Statistics() {} 40 41 42 public static Statistics.StopWatch getStopWatch(Statistics.TimeConsumer consumer) { 43 return new Statistics.StopWatch(consumer); 44 } 45 46 public static Statistics.StopWatch getStopWatch(Statistics.TimeConsumer consumer, boolean start) { 47 Statistics.StopWatch retval = new Statistics.StopWatch(consumer); 48 if (start) { 49 retval.start(); 50 } 51 return retval; 52 } 53 54 public static final class TimeConsumer { 55 private int elapsedTime; 56 private int numberOfCalls; 57 private final String description; 58 59 private TimeConsumer(final String description) { 60 this.description = description; 61 } 62 63 public int getConsumedTime() { 64 return elapsedTime; 65 } 66 67 public int getNumberOfCalls() { 68 return numberOfCalls; 69 } 70 71 public void reset() { 72 elapsedTime = 0; 73 numberOfCalls = 0; 74 } 75 76 public String toString() { 77 return description + ": " + numberOfCalls + " calls in " + elapsedTime + "ms"; } 79 80 private void incrementNumerOfCalls() { 81 numberOfCalls++; 82 83 } 84 } 85 86 public static final class StopWatch { 87 private long startTime = 0; 88 private final Statistics.TimeConsumer activity; 89 90 91 92 private StopWatch(Statistics.TimeConsumer activity) { 93 this.activity = activity; 94 } 95 96 97 public void start() { 98 startTime = System.currentTimeMillis(); 99 } 100 101 public void stop() { 102 assert startTime != 0; 103 activity.elapsedTime += (System.currentTimeMillis() - startTime); 104 activity.incrementNumerOfCalls(); 105 startTime = 0; 106 } 107 } 108 } 109 110 | Popular Tags |