Skip to content

Commit 4e8227e

Browse files
authored
Cleanup benchmark/progress utility (#942)
* cleanup * add benchmark addition * cleanup * cleanup * use seconds
1 parent 87dc69f commit 4e8227e

3 files changed

Lines changed: 82 additions & 142 deletions

File tree

Lines changed: 68 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,111 @@
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-
71
package org.jlab.utils.benchmark;
82

9-
import java.util.ArrayList;
103
import java.util.Arrays;
11-
import java.util.HashMap;
4+
import java.util.Collection;
5+
import java.util.LinkedHashMap;
126
import java.util.Map;
137
import java.util.Timer;
148
import java.util.TimerTask;
15-
import java.util.TreeMap;
169

1710
/**
1811
*
1912
* @author gavalian
2013
*/
2114
public class Benchmark {
2215

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<>();
2618
private Timer updateTimer = null;
19+
20+
public Benchmark(){}
2721

28-
29-
public Benchmark(){
30-
22+
public static Benchmark getInstance(){
23+
return benchmarkInstance;
3124
}
3225

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+
};
4231
updateTimer = new Timer("Benchmark");
43-
updateTimer.scheduleAtFixedRate(timerTask, 0, interval);
32+
updateTimer.scheduleAtFixedRate(timerTask, 0, 1000*seconds);
4433
}
4534

46-
4735
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();
5538
}
5639

5740
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");
6545
}
6646

6747
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
7151
timerStore.get(name).pause();
72-
}
7352
}
7453

7554
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();
8458
}
8559

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);
9162
}
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;
12369
}
70+
12471
@Override
12572
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];
13778
Arrays.fill(asterix,'*');
13879
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");
14889
}
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) {}
151109
}
152-
153-
return str.toString();
154110
}
155111
}

common-tools/clas-utils/src/main/java/org/jlab/utils/benchmark/BenchmarkTimer.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
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-
71
package org.jlab.utils.benchmark;
82

93
/**
@@ -13,16 +7,12 @@
137
public class BenchmarkTimer {
148

159
private String timerName = "generic";
16-
17-
private long lastStartTime = 0;
1810
private long totalTime = 0;
1911
private long timeAtResume = 0;
2012
private int numberOfCalls = 0;
2113
private Boolean isPaused = true;
2214

23-
public BenchmarkTimer(){
24-
25-
}
15+
public BenchmarkTimer(){}
2616

2717
public BenchmarkTimer(String name){
2818
timerName = name;
@@ -47,9 +37,13 @@ public void pause(){
4737
isPaused = true;
4838
}
4939
}
50-
40+
41+
public void add(BenchmarkTimer b) {
42+
totalTime += b.totalTime;
43+
numberOfCalls += b.numberOfCalls;
44+
}
45+
5146
public void reset(){
52-
lastStartTime = 0;
5347
totalTime = 0;
5448
timeAtResume = 0;
5549
numberOfCalls = 0;

common-tools/clas-utils/src/main/java/org/jlab/utils/benchmark/ProgressPrintout.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
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-
*/
61
package org.jlab.utils.benchmark;
72

8-
import java.util.Map;
93
import java.util.Set;
104
import java.util.TreeMap;
115
import java.util.logging.Level;
@@ -17,12 +11,9 @@
1711
*/
1812
public class ProgressPrintout {
1913

20-
private TreeMap<String,Object> items = new TreeMap<String,Object>();
21-
private TreeMap<String,Object> itemMax = new TreeMap<String,Object>();
22-
23-
private Long previousPrintoutTime = (long) 0;
24-
private Long startPrintoutTime = (long) 0;
25-
14+
private TreeMap<String,Object> items = new TreeMap<>();
15+
private Long previousPrintoutTime = (long) 0;
16+
private Long startPrintoutTime = (long) 0;
2617
private double printoutIntervalSeconds = 10.0;
2718
private String printoutLeadingString = ">>>>> progress : ";
2819
private Integer numberOfCalls = 0;
@@ -83,12 +74,12 @@ public void setAsDouble(String name, Double value){
8374

8475
public String getItemString(String itemname){
8576
StringBuilder str = new StringBuilder();
86-
if(this.items.get(itemname) instanceof Integer){
87-
str.append(String.format(" %s : %5d",itemname,(Integer)this.items.get(itemname)));
77+
if(this.items.get(itemname) instanceof Integer integer){
78+
str.append(String.format(" %s : %5d",itemname, integer));
8879
}
8980

90-
if(this.items.get(itemname) instanceof Double){
91-
str.append(String.format(" %s : %8.3f",itemname,(Double)this.items.get(itemname)));
81+
if(this.items.get(itemname) instanceof Double aDouble){
82+
str.append(String.format(" %s : %8.3f",itemname, aDouble));
9283
}
9384
return str.toString();
9485
}
@@ -103,7 +94,6 @@ public static void main(String[] args){
10394
} catch (InterruptedException ex) {
10495
Logger.getLogger(ProgressPrintout.class.getName()).log(Level.SEVERE, null, ex);
10596
}
106-
//System.out.println("cycle " + loop);
10797
progress.updateStatus();
10898
}
10999
}

0 commit comments

Comments
 (0)