压力测试工具ab - Apache HTTP server benchmarking tool

时间:2023-03-08 16:14:06
压力测试工具ab - Apache HTTP server benchmarking tool

搞互联网开发,压力测试必不可少。压力测试的工具很多,我用过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、如何找到文档

压力测试工具ab - Apache HTTP server benchmarking tool压力测试工具ab - Apache HTTP server benchmarking tool

2.3、下载安装

压力测试工具ab - Apache HTTP server benchmarking tool

3、用法

3.1、测试GET请求

D:\Apache24\bin>ab -n  -c  http://www.baidu.com/
This is ApacheBench, Version . <$Revision: $>
Copyright 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 requests
Completed requests
Finished requests Server Software: BWS/.
Server Hostname: www.baidu.com
Server Port: Document Path: /
Document Length: bytes Concurrency Level:
Time taken for tests: . seconds
Complete requests:
Failed requests:
(Connect: , Receive: , Length: , Exceptions: )
Total transferred: bytes
HTML transferred: bytes
Requests per second: . [#/sec] (mean)
Time per request: . [ms] (mean)
Time per request: . [ms] (mean, across all concurrent requests)
Transfer rate: . [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: .
Processing: .
Waiting: .
Total: . Percentage of the requests served within a certain time (ms)
%
%
%
%
%
%
%
%
% (longest request) D:\Apache24\bin>
D:\Apache24\bin>ab -n  -c  http://localhost:8080/coupon/getByMechantId.json?merchantId=10002
This is ApacheBench, Version 2.3 <$Revision: $>
Copyright 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: Document Path: /coupon/getByMechantId.json?merchantId=
Document Length: bytes Concurrency Level:
Time taken for tests: 0.361 seconds
Complete requests:
Failed requests:
Total transferred: bytes
HTML transferred: 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.4
Processing: 44.7
Waiting: 45.0
Total: 44.8 Percentage of the requests served within a certain time (ms)
%
%
%
%
%
%
%
%
% (longest request) D:\Apache24\bin>

3.2、测试POST请求

D:\Apache24\bin>ab -n  -c  -p D:\data.json -T application/json http://localhost:8080/coupon/save.json
This is ApacheBench, Version 2.3 <$Revision: $>
Copyright Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient)
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Finished requests Server Software:
Server Hostname: localhost
Server Port: Document Path: /coupon/save.json
Document Length: bytes Concurrency Level:
Time taken for tests: 3.306 seconds
Complete requests:
Failed requests:
Total transferred: bytes
Total body sent:
HTML transferred: 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.5
Processing: 432.2
Waiting: 432.3
Total: 432.2 Percentage of the requests served within a certain time (ms)
%
%
%
%
%
%
%
%
% (longest request) D:\Apache24\bin>

3.3、带Cookie

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

压力测试工具ab - Apache HTTP server benchmarking tool

data.json是这样的:

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

4、Linux下使用ab

yum install httpd-tools

压力测试工具ab - Apache HTTP server benchmarking tool

压力测试工具ab - Apache HTTP server benchmarking tool

java -jar cjs-springboot-example.jar &

压力测试工具ab - Apache HTTP server benchmarking tool

用法没变

5、代码

 package com.cjs.boot.controller;

 import com.cjs.boot.domain.entity.CouponInfo;
import com.cjs.boot.response.RespResult;
import com.cjs.boot.service.CouponInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; import javax.validation.constraints.NotNull;
import java.util.List; @Controller
@RequestMapping("/coupon")
@Validated
public class CouponController extends BaseController { @Autowired
private CouponInfoService couponInfoService; @GetMapping("/detail.html")
public ModelAndView detail(@NotNull(message = "ID不能为空") Long id) {
ModelAndView modelAndView = new ModelAndView("coupon/detail");
// TODO 查询
return modelAndView;
} @GetMapping("/getByMechantId.json")
@ResponseBody
public RespResult<List<CouponInfo>> getByMechantId(Integer merchantId) {
List<CouponInfo> list = couponInfoService.getByMerchantId(merchantId);
return new RespResult<List<CouponInfo>>(list);
} @GetMapping("/deleteByMechantId.json")
@ResponseBody
public RespResult<List<CouponInfo>> deleteByMerchantId(Integer merchantId) {
couponInfoService.deleteByMerchantId(merchantId);
return RespResult.success();
} @GetMapping("/getById.json")
@ResponseBody
public RespResult<CouponInfo> getById(Long id) {
CouponInfo couponInfo = couponInfoService.getById(id);
return new RespResult<CouponInfo>(couponInfo);
} @GetMapping("/deleteById.json")
@ResponseBody
public RespResult deleteById(Long id) {
couponInfoService.deleteById(id);
return RespResult.success();
} @GetMapping("/add.html")
public ModelAndView add() {
return new ModelAndView("coupon/add");
} @PostMapping("/save.json")
@ResponseBody
public RespResult save(@RequestBody CouponInfo couponInfo, @CookieValue(required = false) String token) {
couponInfoService.save(couponInfo);
return RespResult.success();
}
}
 package com.cjs.boot;

 import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cjs.boot.event.BlackListListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableAsync; import java.util.ArrayList;
import java.util.List; //@MapperScan("com.cjs.boot.mapper")
@EnableCaching
@EnableAsync
@SpringBootApplication
public class CjsSpringbootExampleApplication { public static void main(String[] args) {
SpringApplication.run(CjsSpringbootExampleApplication.class, args); // SpringApplication springApplication = new SpringApplication(CjsSpringbootExampleApplication.class);
// springApplication.addListeners(new BlackListListener());
// springApplication.run(args); } @Bean
public ErrorPageRegistrar errorPageRegistrar() {
return new ErrorPageRegistrar() {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400.html"));
registry.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403.html"));
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
}
};
} @Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); } }

压力测试工具ab - Apache HTTP server benchmarking tool

参考

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/