Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common-tools/clas-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<artifactId>clas-logging</artifactId>
<version>14.1.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.jlab.utils.benchmark.BenchmarkTimer.BenchmarkMultiTimer;
import org.jlab.utils.benchmark.BenchmarkTimer.BenchmarkTimerTotal;

/**
*
Expand All @@ -14,7 +16,7 @@
public class Benchmark {

private static final Benchmark benchmarkInstance = new Benchmark();
private final Map<String,BenchmarkTimer> timerStore = new LinkedHashMap<>();
private final Map<String,BenchmarkMultiTimer> timerStore = new LinkedHashMap<>();
private Timer updateTimer = null;

private Benchmark() {}
Expand All @@ -39,24 +41,37 @@ public void reset(){

public void addTimer(String name){
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkTimer(name));
timerStore.put(name, new BenchmarkMultiTimer(name));
else
System.err.println("[Benchmark] -----> error. timer with name ("+ name + ") already exists");
}

public void pause(String name){
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkTimer(name));
timerStore.put(name, new BenchmarkMultiTimer(name));
else
timerStore.get(name).pause();
}

public void resume(String name){
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkTimer(name));
timerStore.put(name, new BenchmarkMultiTimer(name));
timerStore.get(name).resume();
}

public void pause(int thread, String name){
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkMultiTimer(name));
else
timerStore.get(name).pause(thread);
}

public void resume(int thread, String name){
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkMultiTimer(name));
timerStore.get(name).resume(thread);
}

public BenchmarkTimer getTimer(String name){
return timerStore.getOrDefault(name, null);
}
Expand All @@ -71,7 +86,7 @@ public BenchmarkTimer getTotal(String name) {
@Override
public String toString(){
StringBuilder s = new StringBuilder();
Collection<BenchmarkTimer> timers = timerStore.values();
Collection<BenchmarkMultiTimer> timers = timerStore.values();
if (!timers.isEmpty()) {
int len = timers.iterator().next().toString().length();
char[] asterix = new char[len+8];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
package org.jlab.utils.benchmark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
*
* @author gavalian
*/
public class BenchmarkTimer {

public static class BenchmarkMultiTimer extends BenchmarkTimer {
HashMap<Integer,Long> timeAtResume = new HashMap<>();
HashMap<Integer,Boolean> isPaused = new HashMap<>();
public BenchmarkMultiTimer(String name) { super(name); }
public void resume(int thread) {
if (!isPaused.containsKey(thread) || isPaused.get(thread)) {
timeAtResume.put(thread, System.nanoTime());
isPaused.put(thread, false);
}
}
public void pause(int thread) {
if (!isPaused.get(thread)) {
numberOfCalls.incrementAndGet();
totalTime.addAndGet(System.nanoTime() - timeAtResume.get(thread));
isPaused.put(thread, true);
}
}
@Override
public void reset(){
super.reset();
timeAtResume.clear();
isPaused.clear();
}
}

public static class BenchmarkTimerTotal extends BenchmarkMultiTimer {
ArrayList<BenchmarkTimer> benchmarks = new ArrayList<>();
public BenchmarkTimerTotal(String name) { super(name); }
public void add(BenchmarkTimer b) {
benchmarks.add(b);
totalTime.addAndGet(b.totalTime.get());
numberOfCalls.addAndGet(b.numberOfCalls.get());
}
}

private String timerName = "generic";
private long timeAtResume = 0;
private Boolean isPaused = true;

protected int numberOfCalls = 0;
protected long totalTime = 0;
AtomicInteger numberOfCalls = new AtomicInteger(0);
AtomicLong totalTime = new AtomicLong(0);

public BenchmarkTimer() {}

Expand All @@ -32,33 +72,32 @@ public void resume(){

public void pause(){
if(isPaused==false){
long timeAtPause = System.nanoTime();
totalTime += (timeAtPause - timeAtResume);
numberOfCalls++;
totalTime.addAndGet(System.nanoTime() - timeAtResume);
numberOfCalls.incrementAndGet();
isPaused = true;
}
}

public void reset(){
totalTime = 0;
totalTime.set(0);
numberOfCalls.set(0);
timeAtResume = 0;
numberOfCalls = 0;
isPaused = true;
}

public double getMiliseconds(){
return totalTime/(1.0e6);
return totalTime.get() / 1.0e6;
}

public double getSeconds(){
return totalTime/(1.0e9);
return totalTime.get() / 1.0e9;
}

@Override
public String toString() {
double timePerCall = 0.0;
if (numberOfCalls != 0) timePerCall = getMiliseconds() / numberOfCalls;
if (numberOfCalls.get() != 0) timePerCall = getMiliseconds() / numberOfCalls.get();
return String.format("%-15s : #Calls %12d, Total = %12.2f sec, Unit = %12.3f msec",
getName(), numberOfCalls, getSeconds(), timePerCall);
getName(), numberOfCalls.get(), getSeconds(), timePerCall);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jlab.utils.benchmark;

import org.junit.Test;

public class BenchmarkTest {

@Test
public void multi() throws InterruptedException {
Benchmark.getInstance().resume(1,"test");
Benchmark.getInstance().resume(2,"test");
Thread.sleep(1000);
Benchmark.getInstance().pause(1,"test");
Benchmark.getInstance().pause(2,"test");
System.out.println(Benchmark.getInstance());
}

}
Loading