Stresstester源码分析
http://click.aliyun.com/m/1000005923/开启多线程去跑目标代码(每个线程访问的次数=总请求次数/并发数)
public StressResult test(int concurrencyLevel, int totalRequests, StressTask stressTask, int warmUpTime){if(stressTask ==null) {stressTask =this.emptyTestService;}warmUp(warmUpTime, stressTask);int everyThreadCount = totalRequests / concurrencyLevel;CyclicBarrier threadStartBarrier =newCyclicBarrier(concurrencyLevel);CountDownLatch threadEndLatch =newCountDownLatch(concurrencyLevel);AtomicInteger failedCounter =newAtomicInteger();StressContext stressContext =newStressContext();stressContext.setTestService(stressTask);stressContext.setEveryThreadCount(everyThreadCount);stressContext.setThreadStartBarrier(threadStartBarrier);stressContext.setThreadEndLatch(threadEndLatch);stressContext.setFailedCounter(failedCounter);ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel);List workers =newArrayList(concurrencyLevel);for(int i =0; i < concurrencyLevel; i++){StressThreadWorker worker =newStressThreadWorker(stressContext, everyThreadCount);workers.add(worker);}for(int i =0; i < concurrencyLevel; i++){StressThreadWorker worker = (StressThreadWorker)workers.get(i);executorService.submit(worker);}try{threadEndLatch.await();}catch(InterruptedException e){log.error(“InterruptedException”, e);}executorService.shutdownNow();int realTotalRequests = everyThreadCount * concurrencyLevel;int failedRequests = failedCounter.get();StressResult stressResult =newStressResult();SortResult sortResult = getSortedTimes(workers);List allTimes = sortResult.allTimes;stressResult.setAllTimes(allTimes);List trheadTimes = sortResult.trheadTimes;long totalTime = ((Long)trheadTimes.get(trheadTimes.size() –1)).longValue();stressResult.setTestsTakenTime(totalTime);stressResult.setFailedRequests(failedRequests);stressResult.setTotalRequests(realTotalRequests);stressResult.setConcurrencyLevel(concurrencyLevel);stressResult.setWorkers(workers);returnstressResult;}
为什么要用2个同步辅助类CyclicBarrier,CountDownLatch
//设置线程集合点,等所有现存启动完毕在一起请求任务CyclicBarrier threadStartBarrier =newCyclicBarrier(concurrencyLevel);//控制所有线程做完任务后状态,数量为0的时间,所有任务执行完毕CountDownLatch threadEndLatch =newCountDownLatch(concurrencyLevel);
2.记录每次调用代码的时间,放到一个集合中
SortResult sortResult = getSortedTimes(workers);List allTimes = sortResult.allTimes;3.计算打印出tps /平均耗时/最短耗时/最长耗时
TPS=并发数/平均的相应时间
平均响应时间=测试总时间/总请求次数
package com.taobao.stresstester.core;importjava.io.IOException;importjava.io.Writer;importjava.util.List;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;publicclassSimpleResultFormaterimplementsStressResultFormater{protectedstaticLogger log = LoggerFactory.getLogger(SimpleResultFormater.class);publicvoidformat(StressResult stressResult, Writer writer){long testsTakenTime = stressResult.getTestsTakenTime();int totalRequests = stressResult.getTotalRequests();int concurrencyLevel = stressResult.getConcurrencyLevel();float takes = StatisticsUtils.toMs(testsTakenTime);List allTimes = stressResult.getAllTimes();long totaleTimes = StatisticsUtils.getTotal(allTimes);float tps =1.0E+009F * (concurrencyLevel * (totalRequests / (float)totaleTimes));float averageTime = StatisticsUtils.getAverage(totaleTimes,totalRequests);float onTheadAverageTime = averageTime / concurrencyLevel;int count_50 = totalRequests /2;int count_66 = totalRequests *66/100;int count_75 = totalRequests *75/100;int count_80 = totalRequests *80/100;int count_90 = totalRequests *90/100;int count_95 = totalRequests *95/100;int count_98 = totalRequests *98/100;int count_99 = totalRequests *99/100;long longestRequest = ((Long)allTimes.get(allTimes.size() –1)).longValue();long shortestRequest = ((Long)allTimes.get(0)).longValue();StringBuilder view =newStringBuilder();view.append(” Concurrency Level:\t”).append(concurrencyLevel).append(“–并发数”);view.append(“\r\n Time takenfortests:\t”).append(takes).append(” ms”).append(“–测试耗时”);view.append(“\r\n Complete Requests:\t”).append(totalRequests).append(“–完成测试次数”);