本文不再继续更新,最新版本 参见:https://www.kdocs.cn/l/cguu4PQPrcez
官方的快速入门实际上很难直接运行,为此我们先插入一个Hello实例。
- 首先用python启动HTTP服务器
# python -m http.server 80
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
- 编写压测代码
- 启动locust
$ locust -f easy.py
[2021-03-20 07:51:11,886] andrew-Extensa-2519/WARNING/locust.main: System open file limit '1024' is below minimum setting '10000'. It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
[2021-03-20 07:51:11,887] andrew-Extensa-2519/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2021-03-20 07:51:11,915] andrew-Extensa-2519/INFO/locust.main: Starting Locust 1.4.3
- 发起压测
点击"Start swarming":
- 查看压测情况
- 停止压测
网页上点击"Stop"
- 关闭locust命令行
$ locust -f easy.py
[2021-03-20 09:52:47,335] andrew-Extensa-2519/WARNING/locust.main: System open file limit '1024' is below minimum setting '10000'. It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
[2021-03-20 09:52:47,336] andrew-Extensa-2519/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2021-03-20 09:52:47,370] andrew-Extensa-2519/INFO/locust.main: Starting Locust 1.4.3
[2021-03-20 09:55:26,290] andrew-Extensa-2519/INFO/locust.runners: Spawning 1000 users at the rate 100 users/s (0 users already running)...
[2021-03-20 09:55:47,650] andrew-Extensa-2519/WARNING/root: CPU usage above 90%! This may constrain your throughput and may even give inconsistent response time measurements! See https://docs.locust.io/en/stable/running-locust-distributed.html for how to distribute the load over multiple CPU cores or machines
[2021-03-20 09:56:20,115] andrew-Extensa-2519/INFO/locust.runners: All users spawned: QuickstartUser: 1000 (1000 total running)
[2021-03-20 10:01:30,313] andrew-Extensa-2519/INFO/locust.runners: Stopping 1000 users
[2021-03-20 10:01:42,594] andrew-Extensa-2519/INFO/locust.runners: 1000 Users have been stopped, 0 still running
[2021-03-20 10:01:42,595] andrew-Extensa-2519/WARNING/locust.runners: CPU usage was too high at some point during the test! See https://docs.locust.io/en/stable/running-locust-distributed.html for how to distribute the load over multiple CPU cores or machines
KeyboardInterrupt
2021-03-20T02:13:38Z
[2021-03-20 10:13:38,665] andrew-Extensa-2519/INFO/locust.main: Running teardowns...
[2021-03-20 10:13:38,665] andrew-Extensa-2519/INFO/locust.main: Shutting down (exit code 1), bye.
[2021-03-20 10:13:38,666] andrew-Extensa-2519/INFO/locust.main: Cleaning up runner...
Name # reqs # fails | Avg Min Max Median | req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET / 53315 8(0.02%) | 4770 7 118405 4900 | 141.69 0.02
--------------------------------------------------------------------------------------------------------------------------------------------
Aggregated 53315 8(0.02%) | 4770 7 118405 4900 | 141.69 0.02
Response time percentiles (approximated)
Type Name 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 100% # reqs
--------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------|
GET / 4900 5500 5900 6100 6900 7900 9400 11000 20000 118000 118000 53315
--------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------|
None Aggregated 4900 5500 5900 6100 6900 7900 9400 11000 20000 118000 118000 53315
Error report
# occurrences Error
--------------------------------------------------------------------------------------------------------------------------------------------
8 GET /: ConnectionError(ProtocolError('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')))
--------------------------------------------------------------------------------------------------------------------------------------------
- 错误分析
本次压测出现了8个ConnectionError,系服务器端处理不过来而拒绝。
此次测试只在低端笔记本电脑启了一个CPU,模拟了1000用户,现在常用的台式机或工作站,动辄8个以上的CPU,单台机器locust可以轻松模拟上万用户,性能方面远比jmeter等工具强悍。
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 76
model name : Intel(R) Celeron(R) CPU N3160 @ 1.60GHz
stepping : 4
microcode : 0x411
cpu MHz : 678.146
cache size : 1024 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat md_clear
vmx flags : vnmi preemption_timer invvpid ept_x_only flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest
bugs : cpu_meltdown spectre_v1 spectre_v2 mds msbds_only
bogomips : 3200.00
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 76
model name : Intel(R) Celeron(R) CPU N3160 @ 1.60GHz
stepping : 4
microcode : 0x411
cpu MHz : 600.727
cache size : 1024 KB
physical id : 0
siblings : 4
core id : 1
cpu cores : 4
apicid : 2
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat md_clear
vmx flags : vnmi preemption_timer invvpid ept_x_only flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest
bugs : cpu_meltdown spectre_v1 spectre_v2 mds msbds_only
bogomips : 3200.00
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 2
vendor_id : GenuineIntel
cpu family : 6
model : 76
model name : Intel(R) Celeron(R) CPU N3160 @ 1.60GHz
stepping : 4
microcode : 0x411
cpu MHz : 1119.133
cache size : 1024 KB
physical id : 0
siblings : 4
core id : 2
cpu cores : 4
apicid : 4
initial apicid : 4
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat md_clear
vmx flags : vnmi preemption_timer invvpid ept_x_only flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest
bugs : cpu_meltdown spectre_v1 spectre_v2 mds msbds_only
bogomips : 3200.00
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 3
vendor_id : GenuineIntel
cpu family : 6
model : 76
model name : Intel(R) Celeron(R) CPU N3160 @ 1.60GHz
stepping : 4
microcode : 0x411
cpu MHz : 1038.644
cache size : 1024 KB
physical id : 0
siblings : 4
core id : 3
cpu cores : 4
apicid : 6
initial apicid : 6
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat md_clear
vmx flags : vnmi preemption_timer invvpid ept_x_only flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest
bugs : cpu_meltdown spectre_v1 spectre_v2 mds msbds_only
bogomips : 3200.00
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
服务器的日志有报错:
127.0.0.1 - - [20/Mar/2021 10:01:42] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 45094)
Traceback (most recent call last):
File "/opt/anaconda/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/opt/anaconda/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/opt/anaconda/lib/python3.8/http/server.py", line 647, in __init__
super().__init__(*args, **kwargs)
File "/opt/anaconda/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/opt/anaconda/lib/python3.8/http/server.py", line 427, in handle
self.handle_one_request()
File "/opt/anaconda/lib/python3.8/http/server.py", line 415, in handle_one_request
method()
File "/opt/anaconda/lib/python3.8/http/server.py", line 654, in do_GET
self.copyfile(f, self.wfile)
File "/opt/anaconda/lib/python3.8/http/server.py", line 853, in copyfile
shutil.copyfileobj(source, outputfile)
File "/opt/anaconda/lib/python3.8/shutil.py", line 205, in copyfileobj
fdst_write(buf)
File "/opt/anaconda/lib/python3.8/socketserver.py", line 799, in write
self._sock.sendall(b)
BrokenPipeError: [Errno 32] Broken pipe
----------------------------------------
这个错误真的是服务端的问题么?Locust启动的时候有告警: System open file limit '1024' ,实际是系统打开的文件数有限制。
停止Locust,执行如下命令再启动:
$ ulimit -n 102400
现在并发达到2000,反而还没有错误了。
Locust的日志中还有提示”CPU usage above 90%!“,这块也需要注意,我们将在后面的实战中提供解决方案。