Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,155 +1,111 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.jlab.utils.benchmark;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

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

private static Benchmark benchmarkInstance = new Benchmark();

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

public Benchmark(){}


public Benchmark(){

public static Benchmark getInstance(){
return benchmarkInstance;
}

public void printTimer(int interval){
TimerTask timerTask = new TimerTask()
{
public void run()
{
//what to do at each excecution
System.out.println(benchmarkStringValue());
}
};
public void printTimer(int seconds){
TimerTask timerTask = new TimerTask() {
@Override
public void run() { System.out.println(getInstance()); }
};
updateTimer = new Timer("Benchmark");
updateTimer.scheduleAtFixedRate(timerTask, 0, interval);
updateTimer.scheduleAtFixedRate(timerTask, 0, 1000*seconds);
}


public void reset(){
for(Map.Entry<String,BenchmarkTimer> entry : this.timerStore.entrySet()){
entry.getValue().reset();
}
}

public static Benchmark getInstance(){
return benchmarkInstance;
for (BenchmarkTimer bt : timerStore.values())
bt.reset();
}

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

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

public void resume(String name){
if(timerStore.containsKey(name)==false){
//System.err.println("[Benchmark] -----> error. no timer defined with name ("
//+ name + ")");
addTimer(name);
timerStore.get(name).resume();
} else {
timerStore.get(name).resume();
}
if (!timerStore.containsKey(name))
timerStore.put(name, new BenchmarkTimer(name));
timerStore.get(name).resume();
}

public BenchmarkTimer getTimer(String name){
if(timerStore.containsKey(name)==true){
return timerStore.get(name);
}
return null;
public BenchmarkTimer getTimer(String name){
return timerStore.getOrDefault(name, null);
}


public String benchmarkStringValue(){
StringBuilder str = new StringBuilder();
ArrayList<String> timerStrings = new ArrayList<String>();
for(Map.Entry<String,BenchmarkTimer> timer : timerStore.entrySet()){
timerStrings.add(timer.getValue().toString());
//str.append(timer.getValue().toString());
//str.append("\n");
}

if(timerStrings.size()>0){
int len = timerStrings.get(0).length();
char[] asterix = new char[len+8];
Arrays.fill(asterix,'*');
String margins = new String(asterix);
str.append(margins);
str.append("\n");
str.append("* BENCHMARK RESULTS \n");
str.append(margins);
str.append("\n");
for(String lines : timerStrings){
str.append("* ");
str.append(lines);
str.append(" *\n");
}
str.append(margins);
str.append("\n");
}

return str.toString();

public BenchmarkTimer getTotal(String name) {
BenchmarkTimer total = new BenchmarkTimer(name);
for (BenchmarkTimer b : timerStore.values())
total.add(b);
return total;
}

@Override
public String toString(){
StringBuilder str = new StringBuilder();
ArrayList<String> timerStrings = new ArrayList<String>();
for(Map.Entry<String,BenchmarkTimer> timer : timerStore.entrySet()){
timerStrings.add(timer.getValue().toString());
//str.append(timer.getValue().toString());
//str.append("\n");
}

if(timerStrings.size()>0){
int len = timerStrings.get(0).length();
char[] asterix = new char[len+8];
StringBuilder s = new StringBuilder();
Collection<BenchmarkTimer> timers = timerStore.values();
if (!timers.isEmpty()) {
int len = timers.iterator().next().toString().length();
char[] asterix = new char[len+8];
Arrays.fill(asterix,'*');
String margins = new String(asterix);
str.append(margins);
str.append("\n");
str.append("* BENCHMARK RESULTS \n");
str.append(margins);
str.append("\n");
for(String lines : timerStrings){
str.append("* ");
str.append(lines);
str.append(" *\n");
s.append(margins);
s.append("\n");
s.append("* BENCHMARK RESULTS \n");
s.append(margins);
s.append("\n");
for (BenchmarkTimer b : timers) {
s.append("* ");
s.append(b);
s.append(" *\n");
}
str.append(margins);
str.append("\n");
s.append("* ");
s.append(getTotal(""));
s.append(" *\n");
s.append(margins);
s.append("\n");
}
return s.toString();
}

public static void main(String[] args){
Benchmark b = getInstance();
b.printTimer(10);
int loop = 0;
while(true){
b.resume("COUNT");
loop++;
b.pause("COUNT");
try { Thread.sleep(2000); }
catch (InterruptedException ex) {}
}

return str.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.jlab.utils.benchmark;

/**
Expand All @@ -13,16 +7,12 @@
public class BenchmarkTimer {

private String timerName = "generic";

private long lastStartTime = 0;
private long totalTime = 0;
private long timeAtResume = 0;
private int numberOfCalls = 0;
private Boolean isPaused = true;

public BenchmarkTimer(){

}
public BenchmarkTimer(){}

public BenchmarkTimer(String name){
timerName = name;
Expand All @@ -47,9 +37,13 @@ public void pause(){
isPaused = true;
}
}


public void add(BenchmarkTimer b) {
totalTime += b.totalTime;
numberOfCalls += b.numberOfCalls;
}

public void reset(){
lastStartTime = 0;
totalTime = 0;
timeAtResume = 0;
numberOfCalls = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jlab.utils.benchmark;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
Expand All @@ -17,12 +11,9 @@
*/
public class ProgressPrintout {

private TreeMap<String,Object> items = new TreeMap<String,Object>();
private TreeMap<String,Object> itemMax = new TreeMap<String,Object>();

private Long previousPrintoutTime = (long) 0;
private Long startPrintoutTime = (long) 0;

private TreeMap<String,Object> items = new TreeMap<>();
private Long previousPrintoutTime = (long) 0;
private Long startPrintoutTime = (long) 0;
private double printoutIntervalSeconds = 10.0;
private String printoutLeadingString = ">>>>> progress : ";
private Integer numberOfCalls = 0;
Expand Down Expand Up @@ -83,12 +74,12 @@ public void setAsDouble(String name, Double value){

public String getItemString(String itemname){
StringBuilder str = new StringBuilder();
if(this.items.get(itemname) instanceof Integer){
str.append(String.format(" %s : %5d",itemname,(Integer)this.items.get(itemname)));
if(this.items.get(itemname) instanceof Integer integer){
str.append(String.format(" %s : %5d",itemname, integer));
}

if(this.items.get(itemname) instanceof Double){
str.append(String.format(" %s : %8.3f",itemname,(Double)this.items.get(itemname)));
if(this.items.get(itemname) instanceof Double aDouble){
str.append(String.format(" %s : %8.3f",itemname, aDouble));
}
return str.toString();
}
Expand All @@ -103,7 +94,6 @@ public static void main(String[] args){
} catch (InterruptedException ex) {
Logger.getLogger(ProgressPrintout.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println("cycle " + loop);
progress.updateStatus();
}
}
Expand Down