diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisCounterBenchmark.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisCounterBenchmark.java index 0ad520b7..39f4e4f3 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisCounterBenchmark.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisCounterBenchmark.java @@ -1,7 +1,5 @@ package org.springside.examples.showcase.demos.redis; -import java.util.Date; - import org.springside.modules.test.benchmark.BenchmarkBase; import org.springside.modules.test.benchmark.BenchmarkTask; @@ -19,7 +17,6 @@ public class RedisCounterBenchmark extends BenchmarkBase { private static final int THREAD_COUNT = 20; private static final long LOOP_COUNT = 50000; private static final int PRINT_INTERVAL_SECONDS = 10; - private static final int COUNTERS = 1; private static final String HOST = "localhost"; private static final int PORT = Protocol.DEFAULT_PORT; @@ -72,13 +69,13 @@ public class RedisCounterBenchmark extends BenchmarkBase { @Override public void run() { Jedis jedis = pool.getResource(); - Date startTime = onThreadStart(); + onThreadStart(); try { // start test loop for (int i = 0; i < loopCount; i++) { - jedis.incr(counterName + (i % COUNTERS)); - printInfo(startTime, i); + jedis.incr(counterName); + printInfo(i); } } finally { onThreadFinish(); diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisMassInsertion.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisMassInsertion.java index 15c07b2b..71bd6ca4 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisMassInsertion.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisMassInsertion.java @@ -1,7 +1,5 @@ package org.springside.examples.showcase.demos.redis; -import java.util.Date; - import org.springside.modules.mapper.JsonMapper; import org.springside.modules.test.benchmark.BenchmarkBase; import org.springside.modules.test.benchmark.BenchmarkTask; @@ -13,7 +11,7 @@ import redis.clients.jedis.Pipeline; import redis.clients.jedis.Protocol; /** - * 测试Redis批量插入时的性能, 使用PipeLine加速. + * 测试Redis批量插入时的性能, 使用PipeLine加é?. * * @author calvin */ @@ -75,7 +73,7 @@ public class RedisMassInsertion extends BenchmarkBase { @Override public void run() { Jedis jedis = pool.getResource(); - Date startTime = onThreadStart(); + onThreadStart(); try { Pipeline pl = jedis.pipelined(); @@ -94,7 +92,7 @@ public class RedisMassInsertion extends BenchmarkBase { if (i % BATCH_SIZE == 0) { pl.sync(); - printInfo(startTime, i); + printInfo(i); } } } finally { diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisSessionBenchmark.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisSessionBenchmark.java index a27f8534..0d371d78 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisSessionBenchmark.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/RedisSessionBenchmark.java @@ -1,7 +1,5 @@ package org.springside.examples.showcase.demos.redis; -import java.util.Date; - import org.springside.modules.mapper.JsonMapper; import org.springside.modules.test.benchmark.BenchmarkBase; import org.springside.modules.test.benchmark.BenchmarkTask; @@ -12,7 +10,7 @@ import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Protocol; /** - * 测试Redis用于做Session管理的setEx()与get()方法的性能, 并使用JSON格式存储数据. + * 测试Redis用于做Session管理的setEx()与get()方法的æ?èƒ? 并使用JSON格式存储数据. * * @author calvin */ @@ -72,7 +70,7 @@ public class RedisSessionBenchmark extends BenchmarkBase { @Override public void run() { Jedis jedis = pool.getResource(); - Date startTime = onThreadStart(); + onThreadStart(); try { @@ -88,7 +86,7 @@ public class RedisSessionBenchmark extends BenchmarkBase { //get it back String sessionBackString = jedis.get(keyPrefix + i); Session sessionBack = jsonMapper.fromJson(sessionBackString, Session.class); - printInfo(startTime, i); + printInfo(i); } } finally { onThreadFinish(); diff --git a/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkBase.java b/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkBase.java index 4d0984f5..2bb3a7ba 100644 --- a/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkBase.java +++ b/modules/test/src/main/java/org/springside/modules/test/benchmark/BenchmarkBase.java @@ -37,7 +37,8 @@ public abstract class BenchmarkBase { //print start message String className = this.getClass().getSimpleName(); long invokeTimes = threadCount * loopCount; - System.out.printf("%s start. %,d request will be invoked.\n", className, invokeTimes); + System.out.printf("%s start.%d threads with %,d loops, totally %,d requests will be invoked.\n", className, + threadCount, loopCount, invokeTimes); Date startTime = new Date(); //wait for all threads finish @@ -45,9 +46,10 @@ public abstract class BenchmarkBase { //print summary message long timeInMills = new Date().getTime() - startTime.getTime(); + long tps = invokeTimes * 1000 / timeInMills; - System.out.printf("%s finish.\nThread count is %d, spend %,d ms for %,d request, TPS is %,d.\n", className, - threadCount, timeInMills, invokeTimes, (invokeTimes * 1000 / timeInMills)); + System.out.printf("%s finish.\n%d thread spend %,d ms for %,d requests, TPS is %,d.\n", className, + threadCount, timeInMills, invokeTimes, tps); } finally { threadPool.shutdownNow(); onFinish(); 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 59e0954d..884978f4 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 @@ -9,6 +9,8 @@ public abstract class BenchmarkTask implements Runnable { protected int threadIndex; + protected Date startTime; + protected RateLimiter rateLimiter; protected int printInfoInterval; //单位为秒. protected long previous = 0L; @@ -26,7 +28,7 @@ public abstract class BenchmarkTask implements Runnable { /** * Must be invoked after each thread finish the prepare job, return the startTime. */ - protected Date onThreadStart() { + protected void onThreadStart() { parent.startLock.countDown(); //wait for all other threads ready try { @@ -34,7 +36,7 @@ public abstract class BenchmarkTask implements Runnable { } catch (InterruptedException e) { e.printStackTrace(); } - return new Date(); + startTime = new Date(); } /** @@ -47,21 +49,21 @@ public abstract class BenchmarkTask implements Runnable { } /** - * 间隔printInfoInterval的时间打印信息. + * 间隔printInfoInterval的时间打印信æ�? */ - protected void printInfo(Date startTime, long last) { + protected void printInfo(int current) { if (rateLimiter.tryAcquire()) { long totalTime = (new Date().getTime() - startTime.getTime()) / 1000; - long requests = (last - previous) + 1; + long requests = (current - previous) + 1; long tps = requests / printInfoInterval; - BigDecimal latency = new BigDecimal(printInfoInterval * 1000).divide(new BigDecimal(requests), 2, - BigDecimal.ROUND_HALF_UP); + String latency = new BigDecimal(printInfoInterval * 1000).divide(new BigDecimal(requests), 2, + BigDecimal.ROUND_HALF_UP).toString(); System.out.printf( - "Thread %02d finish %,d request after %d seconds. Last TPS is %,d. Last latency is %sms.\n", - threadIndex, last, totalTime, tps, latency.toString()); - previous = last; + "Thread %02d finish %,d requests after %d seconds. Last TPS is %,d and latency is %sms.\n", + threadIndex, current, totalTime, tps, latency); + previous = current; } } }