Skip to content

Commit

Permalink
springside#171 Redis的Benchmark测试用例, 完整的session性能测试。remove RateLimiter…
Browse files Browse the repository at this point in the history
…,减少依赖包使用
  • Loading branch information
calvin1978 committed Jan 24, 2013
1 parent e01174f commit d81fd0a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.springside.modules.test.benchmark;

import java.math.BigDecimal;
import java.util.Date;

import com.google.common.util.concurrent.RateLimiter;
import java.util.concurrent.TimeUnit;

/**
* Beanchmark中任务线程的基类.
Expand All @@ -14,18 +12,16 @@ public abstract class BenchmarkTask implements Runnable {

protected int threadIndex;
protected ConcurrentBenchmark parent;
protected int printBetweenSeconds;
protected int printBetweenMills;

protected RateLimiter rateLimiter;
protected Date threadStartTime;
protected long threadStartTime;
protected long previousTime;
protected long previous = 0L;

public BenchmarkTask(int threadIndex, ConcurrentBenchmark parent, int printBetweenSeconds) {
this.threadIndex = threadIndex;
this.parent = parent;
this.printBetweenSeconds = printBetweenSeconds;

this.rateLimiter = RateLimiter.create(1d / printBetweenSeconds);
this.printBetweenMills = printBetweenSeconds * 1000;
}

/**
Expand All @@ -39,7 +35,8 @@ protected void onThreadStart() {
} catch (InterruptedException e) {
e.printStackTrace();
}
threadStartTime = new Date();
threadStartTime = System.currentTimeMillis();
previousTime = threadStartTime;
}

/**
Expand All @@ -56,43 +53,47 @@ protected void onThreadFinish() {
/**
* 间隔固定时间打印进度信息.
*/
protected void printProgressMessage(int current) {
if (rateLimiter.tryAcquire()) {
long totalRequest = current + 1;
protected void printProgressMessage(int currentLoop) {
long currentTime = System.currentTimeMillis();

if ((currentTime - previousTime) > printBetweenMills) {
long lastTimeInMills = currentTime - previousTime;
previousTime = currentTime;

long totalRequest = currentLoop + 1;
long lastRequests = totalRequest - previous;

long totalTimeInMills = new Date().getTime() - threadStartTime.getTime();
totalTimeInMills = totalTimeInMills == 0 ? 1 : totalTimeInMills;
String totalTimeInSeconds = new BigDecimal(totalTimeInMills).divide(new BigDecimal(1000), 0,
BigDecimal.ROUND_HALF_UP).toString();
long totalTimeInMills = currentTime - threadStartTime;
long totalTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(totalTimeInMills);

long totalTps = totalRequest * 1000 / totalTimeInMills;
long lastTps = lastRequests / printBetweenSeconds;
long lastTps = lastRequests * 1000 / lastTimeInMills;

BigDecimal lastLatency = new BigDecimal(printBetweenSeconds * 1000).divide(new BigDecimal(lastRequests), 2,
BigDecimal lastLatency = new BigDecimal(lastTimeInMills).divide(new BigDecimal(lastRequests), 2,
BigDecimal.ROUND_HALF_UP);
BigDecimal totalLatency = new BigDecimal(totalTimeInMills).divide(new BigDecimal(totalRequest), 2,
BigDecimal.ROUND_HALF_UP);

System.out
.printf("Thread %02d process %,d requests after %s seconds. Last Tps/latency is %,d/%sms, total Tps/latency is %,d/%sms.\n",
.printf("Thread %02d process %,d requests after %s seconds. Last tps/latency is %,d/%sms. Total tps/latency is %,d/%sms.\n",
threadIndex, totalRequest, totalTimeInSeconds, lastTps, lastLatency.toString(), totalTps,
totalLatency.toString());
previous = current;

previous = currentLoop;
}
}

/**
* 打印线程结果信息.
*/
protected void printThreadFinishMessage() {
long totalTimeInMills = new Date().getTime() - threadStartTime.getTime();
long totalTimeInMills = System.currentTimeMillis() - threadStartTime;
long totalRequest = parent.loopCount;
long totalTps = totalRequest * 1000 / totalTimeInMills;
BigDecimal totalLatency = new BigDecimal(totalTimeInMills).divide(new BigDecimal(totalRequest), 2,
BigDecimal.ROUND_HALF_UP);

System.out.printf("Thread %02d finish.Total Tps/latency is %,d/%sms\n", threadIndex, totalTps,
System.out.printf("Thread %02d finish.Total tps/latency is %,d/%sms\n", threadIndex, totalTps,
totalLatency.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/**
* 多线程Benchmark测试框架.
* 使用JDK Concurrency中的CountDownLatch精确控制多线程中的测试任务启动与停止及计时.
* 使用Guava RateLimiter 间隔固定时间打印测试进度及性能数据.
*
* @author calvin
*/
Expand Down Expand Up @@ -64,7 +63,7 @@ protected void printStartMessage() {
String className = this.getClass().getSimpleName();
long invokeTimes = threadCount * loopCount;

System.out.printf("%s start at %s.\n%d threads with %,d loops, total %,d requests will be invoked.\n",
System.out.printf("%s started at %s.\n%d threads with %,d loops, totally %,d requests will be invoked.\n",
className, startTime.toString(), threadCount, loopCount, invokeTimes);
}

Expand All @@ -75,8 +74,8 @@ protected void printFinishMessage() {
long timeInMills = endTime.getTime() - startTime.getTime();
long tps = invokeTimes * 1000 / timeInMills;

System.out.printf("%s finish at %s.\n%d threads process %,d requests after %,d ms, TPS is %,d.\n", className,
endTime.toString(), threadCount, invokeTimes, timeInMills, tps);
System.out.printf("%s finished at %s.\n%d threads processed %,d requests after %,d ms, tps is %,d.\n",
className, endTime.toString(), threadCount, invokeTimes, timeInMills, tps);
}

/**
Expand Down

0 comments on commit d81fd0a

Please sign in to comment.