Skip to content

Instantly share code, notes, and snippets.

@trnl
Created May 21, 2012 09:21
Show Gist options
  • Save trnl/2761451 to your computer and use it in GitHub Desktop.
Save trnl/2761451 to your computer and use it in GitHub Desktop.
Time configuration for Contiperf2
package org.databene.contiperf.time;
import org.databene.contiperf.Time;
import java.lang.management.ManagementFactory;
/**
* @author Uladzimir Mihura
* Date: 5/16/12
* Time: 6:24 PM
*/
public class CPUTime implements Time {
public long getValue() {
return ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
}
}
package org.databene.contiperf;
import org.databene.contiperf.time.RealTime;
import org.databene.contiperf.report.ReportContext;
import org.databene.contiperf.report.ReportModule;
import org.databene.contiperf.util.InvokerProxy;
import org.databene.stat.LatencyCounter;
import java.io.PrintWriter;
/**
* {@link InvokerProxy} that provides performance tracking features.<br/><br/>
* Created: 22.10.2009 16:36:43
*
* @author Volker Bergmann
* @since 1.0
*/
public class PerformanceTracker extends InvokerProxy {
private Time time;
@Override
public Object invoke(Object[] args) throws Exception {
long callStart = time.getValue();
if (warmUpFinishedTime == -1)
warmUpFinishedTime = callStart + warmUp * 1000000;
checkState(callStart);
Object result = super.invoke(args);
int latency = (int) ((time.getValue() - callStart) / 1000000);
if (counterStarted)
counter.addSample(latency);
reportInvocation(latency, callStart);
if (requirement != null && requirement.getMax() >= 0 && latency > requirement.getMax() && cancelOnViolation)
context.fail("Method " + getId() + " exceeded time limit of " +
requirement.getMax() + " ms running " + latency + " ms");
return result;
}
}
package org.databene.contiperf.time;
import org.databene.contiperf.Time;
/**
* @author Uladzimir Mihura
* Date: 5/16/12
* Time: 6:25 PM
*/
public class RealTime implements Time {
public long getValue() {
return System.nanoTime();
}
}
package org.databene.contiperf.time;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.databene.contiperf.report.CSVSummaryReportModule;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Uladzimir Mihura
* Date: 5/17/12
* Time: 11:23 AM
*/
public class TimeTest {
@Rule
public ContiPerfRule rule = new ContiPerfRule(new CSVSummaryReportModule());
@Test
@PerfTest(invocations = 100, time = RealTime.class)
public void test() {
for (int i = 0; i < 1000000; i++) {
assertEquals(i,i);
}
}
@Test
@PerfTest(invocations = 100, time = UserTime.class)
public void test1() throws InterruptedException {
Thread.sleep(100);
}
@Test
@PerfTest(invocations = 100, time = CPUTime.class)
public void test2() {
for (int i = 0; i < 1000000; i++) {
assertEquals(i,i);
}
}
}
package org.databene.contiperf.time;
import org.databene.contiperf.Time;
import java.lang.management.ManagementFactory;
/**
* @author Uladzimir Mihura
* Date: 5/16/12
* Time: 6:24 PM
*/
public class UserTime implements Time {
public long getValue() {
return ManagementFactory.getThreadMXBean().getCurrentThreadUserTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment