tio-http-server性能测试
1.tio-http-server
1.1.apache benchmark
测试方式
通过 apache benchmark 工具进行压力测试
测试环境
JDK信息:
java version "1.8.0_361"
Java(TM) SE Runtime Environment (build 1.8.0_361-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)
硬件信息
处理器:2.3 GHz Intel Core i7
内存:16 GB 1600 MHz DDR3
系统:macOS 10.13.4 (17E202)
硬件:MacBook Pro (Retina, 15-inch, Late 2013
测试代码
package com.litongjava.tio.http.server;
import java.io.IOException;
import com.litongjava.tio.http.common.HttpConfig;
import com.litongjava.tio.http.common.handler.HttpRequestHandler;
import com.litongjava.tio.http.server.controller.IndexController;
import com.litongjava.tio.http.server.handler.HttpRoutes;
import com.litongjava.tio.http.server.handler.SimpleHttpDispatcherHandler;
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
import com.litongjava.tio.server.ServerTioConfig;
public class MainApp {
public static void main(String[] args) throws IOException {
// 手动添加路由
IndexController controller = new IndexController();
HttpRoutes simpleHttpRoutes = new SimpleHttpRoutes();
simpleHttpRoutes.add("/plaintext", controller::plaintext);
// config server
HttpConfig httpConfig = new HttpConfig(80, null, null, null);
// 关闭心跳
HttpRequestHandler requestHandler = new SimpleHttpDispatcherHandler(httpConfig, simpleHttpRoutes);
HttpServerStarter httpServerStarter = new HttpServerStarter(httpConfig, requestHandler);
ServerTioConfig serverTioConfig = httpServerStarter.getServerTioConfig();
serverTioConfig.setHeartbeatTimeout(0);
// start server
httpServerStarter.start();
}
}
package com.litongjava.tio.http.server.controller;
import java.util.HashMap;
import java.util.Map;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.util.Resps;
public class IndexController {
public HttpResponse plaintext(HttpRequest request) {
return Resps.txt(request, "Hello, World!");
}
}
代码含义:
启动Http服务器
对外提供1个接口,"plaintext"
测试过程
启动服务端
java -jar target\tio-server-study-1.0.jar
模拟10个并发,十万次访问:
ab -c10 -n100000 http://localhost/plaintext
测试结果
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
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 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: -io
Server Hostname: localhost
Server Port: 80
Document Path: /plaintext
Document Length: 13 bytes
Concurrency Level: 10
Time taken for tests: 235.745 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 13900000 bytes
HTML transferred: 1300000 bytes
Requests per second: 424.19 [#/sec] (mean)
Time per request: 23.574 [ms] (mean)
Time per request: 2.357 [ms] (mean, across all concurrent requests)
Transfer rate: 57.58 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 11 441.5 0 19326
Processing: 0 13 69.9 1 1404
Waiting: 0 12 67.6 1 1404
Total: 0 24 446.8 2 19330
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 3
95% 6
98% 399
99% 439
100% 19330 (longest request)
这是一个使用ApacheBench进行的性能测试报告,主要测试了服务器在并发访问下的性能。下面是对报告的一些关键指标的解释:
- Concurrency Level: 并发用户数,这里是10个。
- Time taken for tests: 所有请求完成所需的总时间,这里是235.745秒。
- Complete requests: 完成的请求总数,这里是100000次请求。
- Failed requests: 失败的请求总数,这里是0,表示所有请求都成功了。
- Requests per second: 平均每秒的请求数,这里是424.19次/秒。
- Time per request: 用户平均请求等待时间,这里是23.574毫秒。
- Transfer rate: 服务器的平均传输速率,这里是57.58KB/sec。
在"Connection Times"部分,给出了连接、处理和等待的时间统计信息,包括最小值、平均值、中位数和最大值。
在最后的百分比部分,列出了所有请求中有多少百分比的请求可以在特定的时间内完成。例如,50%的请求在2毫秒内完成,98%的请求在399毫秒内完成,最长的请求需要19330毫秒才能完成。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。