Home 性能测试
Post
Cancel

性能测试

性能测试工具,用过LR,jmeter, ngrinder jmeter压力测试

脚本暂时是直接从win写好,copy过去。然后就可以改jmx文件的参数了。

执行命令

1
 bin/jmeter.sh -n -t xxx-api.jmx -l xxx.jtl >>xxx.log & 

完了之后xxx.jtl列出了每次请求的数据,其中最后一列是每个请求的总耗时

1
2
3
4
5
1459328374461,707,HTTP请求,200,OK,线程组 1-13,text,true,338,243,243,707
1459328374467,701,HTTP请求,200,OK,线程组 1-49,text,true,338,243,243,701
1459328374470,707,HTTP请求,200,OK,线程组 1-34,text,true,338,245,245,707
1459328374447,724,HTTP请求,200,OK,线程组 1-54,text,true,338,243,243,724
1459328374463,708,HTTP请求,200,OK,线程组 1-58,text,true,338,243,243,708

linux下有个不好就是没有win下的聚合报告,可以统计90%,95%,99%耗时的比例,linux只能自己算?

1
2
cat xxx.jtl |awk -F"," '{if($12>2000){print $12}}'|wc -l
cat xxx.jtl |awk -F"," '{if($12>2000){print $12}}'|wc -l

xxx.log,是统计数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Creating summariser <summary>
Created the tree successfully using infobip-api.jmx
Starting the test @ Wed Mar 30 09:59:34 BST 2016 (1459328374151)
Waiting for possible shutdown message on port 4447
生成概要结果 +      1 in     1s =    1.0/s Avg:   707 Min:   707 Max:   707 Err:     0 (0.00%) Active: 243 Started: 243 Finished: 0
summary +      1 in     1s =    1.0/s Avg:   707 Min:   707 Max:   707 Err:     0 (0.00%) Active: 245 Started: 245 Finished: 0
生成概要结果 +   9647 in    25s =  388.5/s Avg:   771 Min:   627 Max:  4205 Err:     0 (0.00%) Active: 300 Started: 300 Finished: 0
生成概要结果 =   9648 in    26s =  373.9/s Avg:   771 Min:   627 Max:  4205 Err:     0 (0.00%)
summary +   9647 in    25s =  388.6/s Avg:   771 Min:   627 Max:  4205 Err:     0 (0.00%) Active: 300 Started: 300 Finished: 0
summary =   9648 in    26s =  373.9/s Avg:   771 Min:   627 Max:  4205 Err:     0 (0.00%)
生成概要结果 +   9184 in    30s =  306.1/s Avg:   967 Min:   629 Max:  4762 Err:     0 (0.00%) Active: 300 Started: 300 Finished: 0
生成概要结果 =  18832 in    56s =  337.5/s Avg:   866 Min:   627 Max:  4762 Err:     0 (0.00%)
summary +   9184 in    30s =  306.1/s Avg:   967 Min:   629 Max:  4762 Err:     0 (0.00%) Active: 300 Started: 300 Finished: 0
summary =  18832 in    56s =  337.4/s Avg:   866 Min:   627 Max:  4762 Err:     0 (0.00%)

简单代码计算QPS

线程方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class ThreadPtestMain {
	
	public static void main(String[] args) {
		
		BizService bizService  = new BizService();
		
		String[] randomParamsArray = {"param1","param2","param3"};
		
		Random rd=new Random();
		int threadCount = 200;
        long reqPerThread = 10000;
        
    	CountDownLatch countDownLatch = new CountDownLatch(threadCount);
    	
    	long start = System.currentTimeMillis();
    	
        Thread[] threads = new Thread[threadCount];
        for(int i=0; i<threadCount; i++){
        	threads[i] = new PThread(bizService,
					reqPerThread, randomParamsArray, rd, countDownLatch);
        }
        
        for(int i=0; i<threadCount; i++){
            threads[i].start();
        }
        
        for(int i=0; i<threadCount; i++){
            try {
            	 threads[i].join();
            } catch (InterruptedException e) { 
               System.out.println("error: "+e.getMessage());
            }
        }
        long end = System.currentTimeMillis();
         
        System.out.println("QPS: "+(reqPerThread*threadCount)*1000.0/(end-start)+" /s");
	}
	
static class PThread   extends Thread{
		
		private BizService bizService;
		
		private long reqPerThread;
		
		private String[] randomParamsArray;
		
		private Random rd;
		
		private CountDownLatch countDownLatch;
		
		public PThread(BizService bizService,long reqPerThread, String[] randomParamsArray,Random rd,CountDownLatch countDownLatch) {
			
			this.bizService =bizService;
			this.reqPerThread =reqPerThread;
			this.randomParamsArray =randomParamsArray;
			this.rd=rd;
			this.countDownLatch = countDownLatch;
			
		}

		@Override
		public void run() {
			try {
				for (int i = 0; i < reqPerThread; i++) {
				 bizService.doSomeBiz(randomParamsArray[rd.nextInt(randomParamsArray.length)]);
				}
				
			} catch (Exception e) {
				
			}finally{
				
			}
			countDownLatch.countDown();
		
		}
	}

}

ExecutorService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class ExecPtestMain {
	
	public static void main(String[] args) {
		
		BizService bizService  = new BizService();
		
		String[] randomParamsArray = {"param1","param2","param3"};
		Random rd=new Random();
		
		long reqPerThread = 10000;
		int threadCount = 100;
		
		CountDownLatch countDownLatch = new CountDownLatch(threadCount);
		List<FutureTask>  list= new ArrayList<FutureTask>();
		ExecutorService executors = Executors.newCachedThreadPool();
		
		long start = System.currentTimeMillis();
		
		for (int i = 0; i < threadCount; i++) {
			FutureTask futureTask = new FutureTask<Object>(new PThread(bizService,
					reqPerThread, randomParamsArray, rd, countDownLatch));
			executors.execute(futureTask);
			list.add(futureTask);
			executors.execute(futureTask);
//			futureTask.get();
		}

		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			System.out.println("await" + e);
		}
		
		long end = System.currentTimeMillis();
		
		System.out.println("QPS: " + threadCount * reqPerThread * 1000.0 / (end - start));
		
	}
	
	
static class PThread   implements Callable{
		
		private BizService bizService;
		
		private long reqPerThread;
		
		private String[] randomParamsArray;
		
		private Random rd;
		
		private CountDownLatch countDownLatch;
		
		public PThread(BizService bizService,long reqPerThread, String[] randomParamsArray,Random rd,CountDownLatch countDownLatch) {
			
			this.bizService =bizService;
			this.reqPerThread =reqPerThread;
			this.randomParamsArray =randomParamsArray;
			this.rd=rd;
			this.countDownLatch = countDownLatch;
			
		}

		@Override
		public Object call() throws Exception {
			Object ret = null;
			try {
				for (int i = 0; i < reqPerThread; i++) {
				 bizService.doSomeBiz(randomParamsArray[rd.nextInt(randomParamsArray.length)]);
				}
				
			} catch (Exception e) {
				
			}finally{
				
			}
			countDownLatch.countDown();
		
			return ret;
		}
	}
}
This post is licensed under CC BY 4.0 by the author.