diff --git a/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkTask.java b/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkTask.java index f0e01c12..beca4566 100644 --- a/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkTask.java +++ b/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkTask.java @@ -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中任务线程的基类. @@ -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; } /** @@ -39,7 +35,8 @@ public abstract class BenchmarkTask implements Runnable { } catch (InterruptedException e) { e.printStackTrace(); } - threadStartTime = new Date(); + threadStartTime = System.currentTimeMillis(); + previousTime = threadStartTime; } /** @@ -56,29 +53,33 @@ public abstract class BenchmarkTask implements Runnable { /** * 间隔固定时间打印进度信息. */ - 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; } } @@ -86,13 +87,13 @@ public abstract class BenchmarkTask implements Runnable { * 打印线程结果信息. */ 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()); } } diff --git a/modules/test/src/main/java/org/springside/modules/test/benchmark/ConcurrentBenchmark.java b/modules/test/src/main/java/org/springside/modules/test/benchmark/ConcurrentBenchmark.java index c54a035e..2f90bb7f 100644 --- a/modules/test/src/main/java/org/springside/modules/test/benchmark/ConcurrentBenchmark.java +++ b/modules/test/src/main/java/org/springside/modules/test/benchmark/ConcurrentBenchmark.java @@ -8,7 +8,6 @@ import java.util.concurrent.Executors; /** * 多线程Benchmark测试框架. * 使用JDK Concurrency中的CountDownLatch精确控制多线程中的测试任务启动与停止及计时. - * 使用Guava RateLimiter 间隔固定时间打印测试进度及性能数据. * * @author calvin */ @@ -64,7 +63,7 @@ public abstract class ConcurrentBenchmark { 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); } @@ -75,8 +74,8 @@ public abstract class ConcurrentBenchmark { 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); } /**