壓力測試工具ab - Apache HTTP server benchmarking tool

不要亂摸發表於2018-05-15

搞網際網路開發,壓力測試必不可少。壓力測試的工具很多,我用過ab和JMeter,今天主要講ab的用法。

1、ab是什麼

ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.

2、官網

2.1、文件地址

http://httpd.apache.org/docs/2.4/programs/ab.html

2.2、如何找到文件

 

2.3、下載安裝

3、用法

3.1、測試GET請求

D:\Apache24\bin>ab -n 200 -c 100 http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        BWS/1.1
Server Hostname:        www.baidu.com
Server Port:            80

Document Path:          /
Document Length:        114870 bytes

Concurrency Level:      100
Time taken for tests:   3.065 seconds
Complete requests:      200
Failed requests:        192
   (Connect: 0, Receive: 0, Length: 192, Exceptions: 0)
Total transferred:      23169728 bytes
HTML transferred:       22986269 bytes
Requests per second:    65.25 [#/sec] (mean)
Time per request:       1532.640 [ms] (mean)
Time per request:       15.326 [ms] (mean, across all concurrent requests)
Transfer rate:          7381.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   14   8.4     12      67
Processing:    39 1183 540.3   1400    2163
Waiting:        6  613 422.4    595    1368
Total:         47 1198 539.8   1414    2168

Percentage of the requests served within a certain time (ms)
  50%   1414
  66%   1514
  75%   1565
  80%   1603
  90%   1773
  95%   1865
  98%   2062
  99%   2074
 100%   2168 (longest request)

D:\Apache24\bin>
D:\Apache24\bin>ab -n 100 -c 50 http://localhost:8080/coupon/getByMechantId.json?merchantId=10002
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /coupon/getByMechantId.json?merchantId=10002
Document Length:        1031 bytes

Concurrency Level:      50
Time taken for tests:   0.361 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      117500 bytes
HTML transferred:       103100 bytes
Requests per second:    276.97 [#/sec] (mean)
Time per request:       180.527 [ms] (mean)
Time per request:       3.611 [ms] (mean, across all concurrent requests)
Transfer rate:          317.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:    18   90  44.7     87     178
Waiting:       18   87  45.0     83     177
Total:         18   90  44.8     87     178

Percentage of the requests served within a certain time (ms)
  50%     87
  66%     94
  75%    144
  80%    146
  90%    152
  95%    154
  98%    178
  99%    178
 100%    178 (longest request)

D:\Apache24\bin>

3.2、測試POST請求

D:\Apache24\bin>ab -n 1000 -c 200 -p D:\data.json -T application/json http://localhost:8080/coupon/save.json
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8080

Document Path:          /coupon/save.json
Document Length:        49 bytes

Concurrency Level:      200
Time taken for tests:   3.306 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      191000 bytes
Total body sent:        433000
HTML transferred:       49000 bytes
Requests per second:    302.52 [#/sec] (mean)
Time per request:       661.121 [ms] (mean)
Time per request:       3.306 [ms] (mean, across all concurrent requests)
Transfer rate:          56.43 [Kbytes/sec] received
                        127.92 kb/s sent
                        184.35 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       5
Processing:    18  589 432.2    603    2459
Waiting:       18  588 432.3    602    2459
Total:         19  589 432.2    603    2459

Percentage of the requests served within a certain time (ms)
  50%    603
  66%    662
  75%    713
  80%    742
  90%   1210
  95%   1270
  98%   1805
  99%   1848
 100%   2459 (longest request)

D:\Apache24\bin>

3.3、帶Cookie

D:\Apache24\bin>ab -n 1000 -c 200 -C token=1234 -p D:\data.json -T application/json http://localhost:8080/coupon/save.json

data.json是這樣的:

{
    "merchantId": 10004,
    "couponName": "我媽最美",
    "couponType": 1,
    "parValue": 520,
    "quantity": 1000,
    "releaseStartTime": "2018-05-13 00:00:00",
    "releaseEndTime": "2018-05-13 23:59:59",
    "limitType": 1,
    "limitNum": 1,
    "remark": "媽媽,您辛苦了!"
}

4、Linux下使用ab

yum install httpd-tools

java -jar cjs-springboot-example.jar &

用法沒變

5、程式碼

 1 package com.cjs.boot.controller;
 2 
 3 import com.cjs.boot.domain.entity.CouponInfo;
 4 import com.cjs.boot.response.RespResult;
 5 import com.cjs.boot.service.CouponInfoService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.validation.annotation.Validated;
 9 import org.springframework.web.bind.annotation.*;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import javax.validation.constraints.NotNull;
13 import java.util.List;
14 
15 
16 @Controller
17 @RequestMapping("/coupon")
18 @Validated
19 public class CouponController extends BaseController {
20 
21     @Autowired
22     private CouponInfoService couponInfoService;
23 
24     @GetMapping("/detail.html")
25     public ModelAndView detail(@NotNull(message = "ID不能為空") Long id) {
26         ModelAndView modelAndView = new ModelAndView("coupon/detail");
27         //  TODO 查詢
28         return modelAndView;
29     }
30 
31     @GetMapping("/getByMechantId.json")
32     @ResponseBody
33     public RespResult<List<CouponInfo>> getByMechantId(Integer merchantId) {
34         List<CouponInfo> list = couponInfoService.getByMerchantId(merchantId);
35         return new RespResult<List<CouponInfo>>(list);
36     }
37 
38     @GetMapping("/deleteByMechantId.json")
39     @ResponseBody
40     public RespResult<List<CouponInfo>> deleteByMerchantId(Integer merchantId) {
41         couponInfoService.deleteByMerchantId(merchantId);
42         return RespResult.success();
43     }
44 
45     @GetMapping("/getById.json")
46     @ResponseBody
47     public RespResult<CouponInfo> getById(Long id) {
48         CouponInfo couponInfo = couponInfoService.getById(id);
49         return new RespResult<CouponInfo>(couponInfo);
50     }
51 
52     @GetMapping("/deleteById.json")
53     @ResponseBody
54     public RespResult deleteById(Long id) {
55         couponInfoService.deleteById(id);
56         return RespResult.success();
57     }
58 
59     @GetMapping("/add.html")
60     public ModelAndView add() {
61         return new ModelAndView("coupon/add");
62     }
63 
64     @PostMapping("/save.json")
65     @ResponseBody
66     public RespResult save(@RequestBody CouponInfo couponInfo, @CookieValue(required = false) String token) {
67         couponInfoService.save(couponInfo);
68         return RespResult.success();
69     }
70 }
 1 package com.cjs.boot;
 2 
 3 import com.alibaba.fastjson.serializer.SerializerFeature;
 4 import com.alibaba.fastjson.support.config.FastJsonConfig;
 5 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 6 import com.cjs.boot.event.BlackListListener;
 7 import org.springframework.boot.SpringApplication;
 8 import org.springframework.boot.autoconfigure.SpringBootApplication;
 9 import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
10 import org.springframework.boot.web.server.ErrorPage;
11 import org.springframework.boot.web.server.ErrorPageRegistrar;
12 import org.springframework.boot.web.server.ErrorPageRegistry;
13 import org.springframework.cache.annotation.EnableCaching;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.MediaType;
17 import org.springframework.scheduling.annotation.EnableAsync;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 //@MapperScan("com.cjs.boot.mapper")
23 @EnableCaching
24 @EnableAsync
25 @SpringBootApplication
26 public class CjsSpringbootExampleApplication {
27 
28     public static void main(String[] args) {
29         SpringApplication.run(CjsSpringbootExampleApplication.class, args);
30 
31 //        SpringApplication springApplication = new SpringApplication(CjsSpringbootExampleApplication.class);
32 //        springApplication.addListeners(new BlackListListener());
33 //        springApplication.run(args);
34 
35     }
36 
37     @Bean
38     public ErrorPageRegistrar errorPageRegistrar() {
39         return new ErrorPageRegistrar() {
40             @Override
41             public void registerErrorPages(ErrorPageRegistry registry) {
42                 registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400.html"));
43                 registry.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403.html"));
44                 registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
45                 registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
46             }
47         };
48     }
49 
50     @Bean
51     public HttpMessageConverters fastJsonHttpMessageConverters(){
52         FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
53         FastJsonConfig fastJsonConfig = new FastJsonConfig();
54         fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
55         List<MediaType> mediaTypes = new ArrayList<>();
56         mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
57         mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
58         fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
59         fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
60 
61         return new HttpMessageConverters(fastJsonHttpMessageConverter);
62 
63     }
64 
65 }

參考

https://www.cnblogs.com/EthanCai/archive/2014/05/11/3721656.html

https://blog.csdn.net/wx19900503/article/details/56847264

https://www.jianshu.com/p/e3793ae91a62

https://blog.csdn.net/dreamer2020/article/details/52904686

http://wetest.qq.com/

http://wetest.qq.com/gaps/

 

相關文章