Skip to content

Commit 7faa204

Browse files
authored
publicize constructor and add a pretty total for human consumption (#1441)
* publicize constructor * add a TOTAL row, for human consumption * fix indentation * cleanup
1 parent ce7407a commit 7faa204

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Benchmark {
1818
private final Map<String,BenchmarkMultiTimer> timerStore = new LinkedHashMap<>();
1919
private Timer updateTimer = null;
2020

21-
private Benchmark() {}
21+
public Benchmark() {}
2222

2323
public static Benchmark getInstance(){
2424
return benchmarkInstance;
@@ -92,6 +92,11 @@ public String toString(){
9292
s.append(b);
9393
s.append(" *\n");
9494
}
95+
s.append(String.format("* %-15s : #Calls %12.2f, Total = %12.2f sec, Unit = %12.3f msec *\n",
96+
"TOTAL",
97+
((float)timers.stream().mapToInt(x -> x.numberOfCalls.get()).sum())/timers.size(),
98+
timers.stream().mapToDouble(x -> x.getSeconds()).sum(),
99+
timers.stream().mapToDouble(x -> x.getMillisecondsPerCall()).sum()));
95100
s.append(margins);
96101
s.append("\n");
97102
}
@@ -106,16 +111,4 @@ public String[] toCSV() {
106111
};
107112
}
108113

109-
public static void main(String[] args){
110-
Benchmark b = getInstance();
111-
b.printTimer(10);
112-
int loop = 0;
113-
while(true){
114-
b.resume("COUNT");
115-
loop++;
116-
b.pause("COUNT");
117-
try { Thread.sleep(2000); }
118-
catch (InterruptedException ex) {}
119-
}
120-
}
121114
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ public double getSeconds(){
8383
return totalTime.get() / 1.0e9;
8484
}
8585

86+
public double getMillisecondsPerCall() {
87+
return numberOfCalls.get() > 0 ? getMilliseconds() / numberOfCalls.get() : 0;
88+
}
89+
8690
@Override
8791
public String toString() {
8892
return String.format("%-15s : #Calls %12d, Total = %12.2f sec, Unit = %12.3f msec",
8993
getName(), numberOfCalls.get(), getSeconds(), getMillisecondsPerCall());
9094
}
9195

92-
public double getMillisecondsPerCall() {
93-
return numberOfCalls.get() > 0 ? getMilliseconds() / numberOfCalls.get() : 0;
94-
}
9596
}

0 commit comments

Comments
 (0)