|
1 | | -/* |
2 | | - * To change this license header, choose License Headers in Project Properties. |
3 | | - * To change this template file, choose Tools | Templates |
4 | | - * and open the template in the editor. |
5 | | - */ |
6 | | - |
7 | 1 | package org.jlab.utils.benchmark; |
8 | 2 |
|
9 | | -import java.util.ArrayList; |
10 | 3 | import java.util.Arrays; |
11 | | -import java.util.HashMap; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.LinkedHashMap; |
12 | 6 | import java.util.Map; |
13 | 7 | import java.util.Timer; |
14 | 8 | import java.util.TimerTask; |
15 | | -import java.util.TreeMap; |
16 | 9 |
|
17 | 10 | /** |
18 | 11 | * |
19 | 12 | * @author gavalian |
20 | 13 | */ |
21 | 14 | public class Benchmark { |
22 | 15 |
|
23 | | - private static Benchmark benchmarkInstance = new Benchmark(); |
24 | | - |
25 | | - private final Map<String,BenchmarkTimer> timerStore = new HashMap<String,BenchmarkTimer>(); |
| 16 | + private static final Benchmark benchmarkInstance = new Benchmark(); |
| 17 | + private final Map<String,BenchmarkTimer> timerStore = new LinkedHashMap<>(); |
26 | 18 | private Timer updateTimer = null; |
| 19 | + |
| 20 | + public Benchmark(){} |
27 | 21 |
|
28 | | - |
29 | | - public Benchmark(){ |
30 | | - |
| 22 | + public static Benchmark getInstance(){ |
| 23 | + return benchmarkInstance; |
31 | 24 | } |
32 | 25 |
|
33 | | - public void printTimer(int interval){ |
34 | | - TimerTask timerTask = new TimerTask() |
35 | | - { |
36 | | - public void run() |
37 | | - { |
38 | | - //what to do at each excecution |
39 | | - System.out.println(benchmarkStringValue()); |
40 | | - } |
41 | | - }; |
| 26 | + public void printTimer(int seconds){ |
| 27 | + TimerTask timerTask = new TimerTask() { |
| 28 | + @Override |
| 29 | + public void run() { System.out.println(getInstance()); } |
| 30 | + }; |
42 | 31 | updateTimer = new Timer("Benchmark"); |
43 | | - updateTimer.scheduleAtFixedRate(timerTask, 0, interval); |
| 32 | + updateTimer.scheduleAtFixedRate(timerTask, 0, 1000*seconds); |
44 | 33 | } |
45 | 34 |
|
46 | | - |
47 | 35 | public void reset(){ |
48 | | - for(Map.Entry<String,BenchmarkTimer> entry : this.timerStore.entrySet()){ |
49 | | - entry.getValue().reset(); |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - public static Benchmark getInstance(){ |
54 | | - return benchmarkInstance; |
| 36 | + for (BenchmarkTimer bt : timerStore.values()) |
| 37 | + bt.reset(); |
55 | 38 | } |
56 | 39 |
|
57 | 40 | public void addTimer(String name){ |
58 | | - if(timerStore.containsKey(name)==true){ |
59 | | - System.err.println("[Benchmark] -----> error. timer with name (" |
60 | | - + name + ") already exists"); |
61 | | - } else { |
62 | | - BenchmarkTimer timer = new BenchmarkTimer(name); |
63 | | - timerStore.put(timer.getName(), timer); |
64 | | - } |
| 41 | + if (!timerStore.containsKey(name)) |
| 42 | + timerStore.put(name, new BenchmarkTimer(name)); |
| 43 | + else |
| 44 | + System.err.println("[Benchmark] -----> error. timer with name ("+ name + ") already exists"); |
65 | 45 | } |
66 | 46 |
|
67 | 47 | public void pause(String name){ |
68 | | - if(timerStore.containsKey(name)==false){ |
69 | | - addTimer(name); |
70 | | - } else { |
| 48 | + if (!timerStore.containsKey(name)) |
| 49 | + timerStore.put(name, new BenchmarkTimer(name)); |
| 50 | + else |
71 | 51 | timerStore.get(name).pause(); |
72 | | - } |
73 | 52 | } |
74 | 53 |
|
75 | 54 | public void resume(String name){ |
76 | | - if(timerStore.containsKey(name)==false){ |
77 | | - //System.err.println("[Benchmark] -----> error. no timer defined with name (" |
78 | | - //+ name + ")"); |
79 | | - addTimer(name); |
80 | | - timerStore.get(name).resume(); |
81 | | - } else { |
82 | | - timerStore.get(name).resume(); |
83 | | - } |
| 55 | + if (!timerStore.containsKey(name)) |
| 56 | + timerStore.put(name, new BenchmarkTimer(name)); |
| 57 | + timerStore.get(name).resume(); |
84 | 58 | } |
85 | 59 |
|
86 | | - public BenchmarkTimer getTimer(String name){ |
87 | | - if(timerStore.containsKey(name)==true){ |
88 | | - return timerStore.get(name); |
89 | | - } |
90 | | - return null; |
| 60 | + public BenchmarkTimer getTimer(String name){ |
| 61 | + return timerStore.getOrDefault(name, null); |
91 | 62 | } |
92 | | - |
93 | | - |
94 | | - public String benchmarkStringValue(){ |
95 | | - StringBuilder str = new StringBuilder(); |
96 | | - ArrayList<String> timerStrings = new ArrayList<String>(); |
97 | | - for(Map.Entry<String,BenchmarkTimer> timer : timerStore.entrySet()){ |
98 | | - timerStrings.add(timer.getValue().toString()); |
99 | | - //str.append(timer.getValue().toString()); |
100 | | - //str.append("\n"); |
101 | | - } |
102 | | - |
103 | | - if(timerStrings.size()>0){ |
104 | | - int len = timerStrings.get(0).length(); |
105 | | - char[] asterix = new char[len+8]; |
106 | | - Arrays.fill(asterix,'*'); |
107 | | - String margins = new String(asterix); |
108 | | - str.append(margins); |
109 | | - str.append("\n"); |
110 | | - str.append("* BENCHMARK RESULTS \n"); |
111 | | - str.append(margins); |
112 | | - str.append("\n"); |
113 | | - for(String lines : timerStrings){ |
114 | | - str.append("* "); |
115 | | - str.append(lines); |
116 | | - str.append(" *\n"); |
117 | | - } |
118 | | - str.append(margins); |
119 | | - str.append("\n"); |
120 | | - } |
121 | | - |
122 | | - return str.toString(); |
| 63 | + |
| 64 | + public BenchmarkTimer getTotal(String name) { |
| 65 | + BenchmarkTimer total = new BenchmarkTimer(name); |
| 66 | + for (BenchmarkTimer b : timerStore.values()) |
| 67 | + total.add(b); |
| 68 | + return total; |
123 | 69 | } |
| 70 | + |
124 | 71 | @Override |
125 | 72 | public String toString(){ |
126 | | - StringBuilder str = new StringBuilder(); |
127 | | - ArrayList<String> timerStrings = new ArrayList<String>(); |
128 | | - for(Map.Entry<String,BenchmarkTimer> timer : timerStore.entrySet()){ |
129 | | - timerStrings.add(timer.getValue().toString()); |
130 | | - //str.append(timer.getValue().toString()); |
131 | | - //str.append("\n"); |
132 | | - } |
133 | | - |
134 | | - if(timerStrings.size()>0){ |
135 | | - int len = timerStrings.get(0).length(); |
136 | | - char[] asterix = new char[len+8]; |
| 73 | + StringBuilder s = new StringBuilder(); |
| 74 | + Collection<BenchmarkTimer> timers = timerStore.values(); |
| 75 | + if (!timers.isEmpty()) { |
| 76 | + int len = timers.iterator().next().toString().length(); |
| 77 | + char[] asterix = new char[len+8]; |
137 | 78 | Arrays.fill(asterix,'*'); |
138 | 79 | String margins = new String(asterix); |
139 | | - str.append(margins); |
140 | | - str.append("\n"); |
141 | | - str.append("* BENCHMARK RESULTS \n"); |
142 | | - str.append(margins); |
143 | | - str.append("\n"); |
144 | | - for(String lines : timerStrings){ |
145 | | - str.append("* "); |
146 | | - str.append(lines); |
147 | | - str.append(" *\n"); |
| 80 | + s.append(margins); |
| 81 | + s.append("\n"); |
| 82 | + s.append("* BENCHMARK RESULTS \n"); |
| 83 | + s.append(margins); |
| 84 | + s.append("\n"); |
| 85 | + for (BenchmarkTimer b : timers) { |
| 86 | + s.append("* "); |
| 87 | + s.append(b); |
| 88 | + s.append(" *\n"); |
148 | 89 | } |
149 | | - str.append(margins); |
150 | | - str.append("\n"); |
| 90 | + s.append("* "); |
| 91 | + s.append(getTotal("")); |
| 92 | + s.append(" *\n"); |
| 93 | + s.append(margins); |
| 94 | + s.append("\n"); |
| 95 | + } |
| 96 | + return s.toString(); |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String[] args){ |
| 100 | + Benchmark b = getInstance(); |
| 101 | + b.printTimer(10); |
| 102 | + int loop = 0; |
| 103 | + while(true){ |
| 104 | + b.resume("COUNT"); |
| 105 | + loop++; |
| 106 | + b.pause("COUNT"); |
| 107 | + try { Thread.sleep(2000); } |
| 108 | + catch (InterruptedException ex) {} |
151 | 109 | } |
152 | | - |
153 | | - return str.toString(); |
154 | 110 | } |
155 | 111 | } |
0 commit comments