Skip to content

Commit a4f6b79

Browse files
authored
add benchmark total (#1027)
1 parent 09941e3 commit a4f6b79

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public static Benchmark getInstance(){
2525

2626
public void printTimer(int seconds){
2727
TimerTask timerTask = new TimerTask() {
28-
@Override
29-
public void run() { System.out.println(getInstance()); }
28+
@Override
29+
public void run() { System.out.println(benchmarkInstance); }
3030
};
31-
updateTimer = new Timer("Benchmark");
31+
updateTimer = new Timer("Benchmark", true);
3232
updateTimer.scheduleAtFixedRate(timerTask, 0, 1000*seconds);
3333
}
3434

@@ -62,7 +62,7 @@ public BenchmarkTimer getTimer(String name){
6262
}
6363

6464
public BenchmarkTimer getTotal(String name) {
65-
BenchmarkTimer total = new BenchmarkTimer(name);
65+
BenchmarkTimerTotal total = new BenchmarkTimerTotal(name);
6666
for (BenchmarkTimer b : timerStore.values())
6767
total.add(b);
6868
return total;

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
* @author gavalian
66
*/
77
public class BenchmarkTimer {
8-
8+
99
private String timerName = "generic";
10-
private long totalTime = 0;
11-
private long timeAtResume = 0;
12-
private int numberOfCalls = 0;
10+
private long timeAtResume = 0;
1311
private Boolean isPaused = true;
14-
15-
public BenchmarkTimer(){}
16-
17-
public BenchmarkTimer(String name){
12+
13+
protected int numberOfCalls = 0;
14+
protected long totalTime = 0;
15+
16+
public BenchmarkTimer() {}
17+
18+
public BenchmarkTimer(String name) {
1819
timerName = name;
1920
}
2021

@@ -38,11 +39,6 @@ public void pause(){
3839
}
3940
}
4041

41-
public void add(BenchmarkTimer b) {
42-
totalTime += b.totalTime;
43-
numberOfCalls += b.numberOfCalls;
44-
}
45-
4642
public void reset(){
4743
totalTime = 0;
4844
timeAtResume = 0;
@@ -59,12 +55,10 @@ public double getSeconds(){
5955
}
6056

6157
@Override
62-
public String toString(){
63-
StringBuilder str = new StringBuilder();
58+
public String toString() {
6459
double timePerCall = 0.0;
65-
if(numberOfCalls!=0) timePerCall = this.getMiliseconds()/numberOfCalls;
66-
str.append(String.format("TIMER (%-12s) : N Calls %12d, Total Time = %12.2f sec, Unit Time = %12.3f msec",
67-
this.getName(),numberOfCalls,this.getSeconds(),timePerCall));
68-
return str.toString();
60+
if (numberOfCalls != 0) timePerCall = getMiliseconds() / numberOfCalls;
61+
return String.format("TIMER (%-12s) : N Calls %12d, Total Time = %12.2f sec, Unit Time = %12.3f msec",
62+
getName(), numberOfCalls, getSeconds(), timePerCall);
6963
}
7064
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.jlab.utils.benchmark;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
*
7+
* @author baltzell
8+
*/
9+
public class BenchmarkTimerTotal extends BenchmarkTimer {
10+
11+
ArrayList<BenchmarkTimer> benchmarks = new ArrayList<>();
12+
13+
public BenchmarkTimerTotal(String name) {
14+
super(name);
15+
}
16+
17+
@Override
18+
public String toString() {
19+
double timePerCall = 0.0;
20+
if (numberOfCalls != 0) timePerCall = getMiliseconds() / numberOfCalls * benchmarks.size();
21+
return String.format("TIMER (%-12s) : N Calls %12d, Total Time = %12.2f sec, Unit Time = %12.3f msec",
22+
getName(), numberOfCalls, getSeconds(), timePerCall);
23+
}
24+
25+
public void add(BenchmarkTimer b) {
26+
benchmarks.add(b);
27+
totalTime += b.totalTime;
28+
numberOfCalls += b.numberOfCalls;
29+
}
30+
}

0 commit comments

Comments
 (0)