症狀
在使用 wrk 等壓測工具對服務進行壓測時,經常會發現 P99 和 P90 之間的差異很大,而且 wrk 得出的 P99 可能會比 prometheus 統計出的 P99 更高。
比如wrk可能得到如下結果:
Latency Distribution
50.000% 2.76ms
75.000% 5.91ms
90.000% 9.42ms
99.000% 48.00ms
99.900% 70.01ms
99.990% 183.04ms
99.999% 317.44ms
100.000% 396.80ms
但實際上 prometheus 統計出的 P99 隻有 20ms。
原因
舉個🌰
設想如下場景:
地鐵閘機,每個人通過需要耗費 10s ,A、B、C、D、E 五人按順序排隊過閘機。
對于 A 來說,消耗的時間為 10s,
對于 E 來說,消耗的時間為 50s(40s 排隊 + 10s 通過)。
現在要描述地鐵閘機的平均通過耗時,
如果不計算排隊時間,則應該是 10s,
如果計算排隊時間,則應該是 30s,
哪個對?
為了便于了解,後邊會成這種現象叫 「排隊現象」,應**「排隊現象」造成的耗時稱為「排隊耗時」。**
類比到伺服器
伺服器處理一條請求需要耗費 1s,使用 1 個線程和 5 個連接配接,每秒發送 5 個請求。
對于第一個請求,耗時毫無疑問是 1s,
但是對于後邊的請求,則可能出現排隊的情況*( 比如對于第 2 個請求,其耗時就是 1s 排隊 + 1s 處理 )。*
如果程式保持這個請求速率連續運作 1 分鐘,那應該怎麼描述伺服器的 latency ?
Coordinated Omission 問題
當壓測工具的線程數量設定不合理時,可能會遇到上邊說的類似于排隊的情況,最開始壓測工具是不會将“排隊時間“加入 latency 統計的,後來 Gil Tene 覺得忽略“排隊時間”的情況不能完整的展現壓測性能,并稱這種情況叫做 Coordinated Omission,有些地方也翻譯為 協調遺漏 。
針對 Coordinated Omission 問題,wrk 做了校正功能,将 “排隊時間” 加入了 latency 統計。
但實際上我們壓測時很避免壓測工具和伺服器之間的排隊現象,是以 latency 統計 中的 max 耗時可能有時會顯得不可思議。
更好的解決
wrk2
校正 Coordinated Omission ***的壓測結果和 未校正 Coordinated Omission 的壓測結果對我們了解伺服器性能都是有意義的,但是 wrk 簡單粗暴(隻用了10行代碼)*的将 Coordinated Omission 進行了校正,我們隻能通過不斷地調整 線程數量 來達到一個滿意的測試結果。
後來提出 Coordinated Omission 問題 的 Gil Tene 專門開發了一個 wrk2 來更好的解決這個問題。
wrk2 vs wrk
wrk2 與 wrk 的主要差別有三個
-
: 以指定的 rps 去請求伺服器-R
-
:不修正 Coordinated Omission 問題 的 latency 統計--u_latency
- 更詳細的 latency 分布統計
當然,當使用 wrk2 以指定的 rps 去請求伺服器時,受 作業系統睡眠時間行為 等情況的影響,wrk2 的 latency 準确度低于 wrk,這也是 wrk2 無法取代 wrk 的重要原因。
我們應該将 wrk2 作為 wrk 的一個補充,當然也不能過度迷信 wrk ,這些壓測工具都是隻能反映出伺服器的一個大緻性能,更重要的還是要結合多方的日志、監控甚至是 systemtap 等工具來綜合評估伺服器的性能,在做性能評估時,也應該預留出足夠的性能備援。
wrk2 結果示例
Running 1m test @ http://xxxxxx
10 threads and 200 connections
Thread calibration: mean lat.: 19.755ms, rate sampling interval: 32ms
Thread calibration: mean lat.: 24.679ms, rate sampling interval: 47ms
Thread calibration: mean lat.: 37.411ms, rate sampling interval: 70ms
Thread calibration: mean lat.: 24.097ms, rate sampling interval: 51ms
Thread calibration: mean lat.: 67.573ms, rate sampling interval: 422ms
Thread Stats Avg Stdev Max +/- Stdev
Latency 42.55ms 116.43ms 1.18s 92.78%
Req/Sec 2.07k 379.69 3.70k 72.59%
Latency Distribution (HdrHistogram - Recorded Latency)
50.000% 9.11ms
75.000% 17.25ms
90.000% 66.30ms
99.000% 655.36ms
99.900% 982.53ms
99.990% 1.16s
99.999% 1.18s
100.000% 1.19s
Detailed Percentile spectrum:
Value Percentile TotalCount 1/(1-Percentile)
1.164 0.000000 1 1.00
3.957 0.100000 77014 1.11
5.079 0.200000 154070 1.25
6.215 0.300000 230963 1.43
7.443 0.400000 307822 1.67
9.111 0.500000 384870 2.00
10.119 0.550000 423237 2.22
11.295 0.600000 461637 2.50
12.743 0.650000 500203 2.86
14.543 0.700000 538682 3.33
17.247 0.750000 577124 4.00
19.263 0.775000 596393 4.44
21.871 0.800000 615515 5.00
25.583 0.825000 634779 5.71
31.183 0.850000 654004 6.67
41.311 0.875000 673258 8.00
50.431 0.887500 682839 8.89
66.303 0.900000 692471 10.00
95.999 0.912500 702086 11.43
146.047 0.925000 711688 13.33
199.551 0.937500 721308 16.00
222.847 0.943750 726126 17.78
256.639 0.950000 730930 20.00
306.687 0.956250 735740 22.86
356.863 0.962500 740548 26.67
403.967 0.968750 745373 32.00
423.935 0.971875 747765 35.56
447.743 0.975000 750187 40.00
472.575 0.978125 752580 45.71
505.343 0.981250 754972 53.33
542.207 0.984375 757382 64.00
565.247 0.985938 758586 71.11
597.503 0.987500 759783 80.00
631.295 0.989062 760988 91.43
670.719 0.990625 762187 106.67
709.119 0.992188 763388 128.00
728.063 0.992969 763988 142.22
743.423 0.993750 764590 160.00
759.807 0.994531 765205 182.86
772.095 0.995313 765800 213.33
785.919 0.996094 766394 256.00
793.087 0.996484 766696 284.44
799.231 0.996875 766989 320.00
807.935 0.997266 767301 365.71
822.783 0.997656 767589 426.67
857.599 0.998047 767890 512.00
887.295 0.998242 768040 568.89
908.287 0.998437 768193 640.00
930.815 0.998633 768342 731.43
954.879 0.998828 768491 853.33
985.087 0.999023 768642 1024.00
999.423 0.999121 768717 1137.78
1014.783 0.999219 768791 1280.00
1025.023 0.999316 768868 1462.86
1034.751 0.999414 768942 1706.67
1050.623 0.999512 769021 2048.00
1056.767 0.999561 769055 2275.56
1064.959 0.999609 769102 2560.00
1070.079 0.999658 769135 2925.71
1077.247 0.999707 769167 3413.33
1083.391 0.999756 769206 4096.00
1096.703 0.999780 769226 4551.11
1103.871 0.999805 769242 5120.00
1120.255 0.999829 769262 5851.43
1134.591 0.999854 769280 6826.67
1154.047 0.999878 769299 8192.00
1160.191 0.999890 769309 9102.22
1163.263 0.999902 769320 10240.00
1167.359 0.999915 769327 11702.86
1169.407 0.999927 769339 13653.33
1171.455 0.999939 769346 16384.00
1173.503 0.999945 769356 18204.44
1173.503 0.999951 769356 20480.00
1174.527 0.999957 769360 23405.71
1175.551 0.999963 769366 27306.67
1176.575 0.999969 769373 32768.00
1176.575 0.999973 769373 36408.89
1177.599 0.999976 769376 40960.00
1177.599 0.999979 769376 46811.43
1178.623 0.999982 769378 54613.33
1180.671 0.999985 769382 65536.00
1180.671 0.999986 769382 72817.78
1181.695 0.999988 769387 81920.00
1181.695 0.999989 769387 93622.86
1181.695 0.999991 769387 109226.67
1181.695 0.999992 769387 131072.00
1181.695 0.999993 769387 145635.56
1182.719 0.999994 769388 163840.00
1182.719 0.999995 769388 187245.71
1183.743 0.999995 769390 218453.33
1183.743 0.999996 769390 262144.00
1183.743 0.999997 769390 291271.11
1183.743 0.999997 769390 327680.00
1183.743 0.999997 769390 374491.43
1185.791 0.999998 769392 436906.67
1185.791 1.000000 769392 inf
#[Mean = 42.551, StdDeviation = 116.429]
#[Max = 1184.768, Total count = 769392]
#[Buckets = 27, SubBuckets = 2048]
----------------------------------------------------------
Latency Distribution (HdrHistogram - Uncorrected Latency (measured without taking delayed starts into account))
50.000% 6.49ms
75.000% 9.41ms
90.000% 12.18ms
99.000% 17.38ms
99.900% 42.65ms
99.990% 399.61ms
99.999% 601.09ms
100.000% 607.23ms
Detailed Percentile spectrum:
Value Percentile TotalCount 1/(1-Percentile)
1.030 0.000000 1 1.00
3.033 0.100000 77058 1.11
3.985 0.200000 153908 1.25
4.875 0.300000 230983 1.43
5.679 0.400000 307777 1.67
6.495 0.500000 384949 2.00
6.947 0.550000 423265 2.22
7.451 0.600000 461789 2.50
8.023 0.650000 500133 2.86
8.679 0.700000 538647 3.33
9.415 0.750000 577236 4.00
9.807 0.775000 596475 4.44
10.223 0.800000 615833 5.00
10.655 0.825000 635061 5.71
11.111 0.850000 654213 6.67
11.615 0.875000 673390 8.00
11.887 0.887500 682958 8.89
12.175 0.900000 692461 10.00
12.495 0.912500 702089 11.43
12.847 0.925000 711712 13.33
13.239 0.937500 721427 16.00
13.455 0.943750 726245 17.78
13.687 0.950000 730957 20.00
13.959 0.956250 735800 22.86
14.255 0.962500 740596 26.67
14.607 0.968750 745357 32.00
14.823 0.971875 747804 35.56
15.063 0.975000 750182 40.00
15.335 0.978125 752613 45.71
15.671 0.981250 754988 53.33
16.103 0.984375 757372 64.00
16.375 0.985938 758585 71.11
16.703 0.987500 759806 80.00
17.087 0.989062 760983 91.43
17.599 0.990625 762207 106.67
18.287 0.992188 763394 128.00
18.751 0.992969 763990 142.22
19.279 0.993750 764586 160.00
20.031 0.994531 765200 182.86
20.911 0.995313 765788 213.33
22.015 0.996094 766390 256.00
22.671 0.996484 766690 284.44
23.439 0.996875 766991 320.00
24.463 0.997266 767294 365.71
25.679 0.997656 767592 426.67
27.311 0.998047 767890 512.00
28.863 0.998242 768041 568.89
31.199 0.998437 768191 640.00
34.367 0.998633 768341 731.43
38.431 0.998828 768491 853.33
43.263 0.999023 768642 1024.00
45.631 0.999121 768716 1137.78
48.703 0.999219 768791 1280.00
52.575 0.999316 768868 1462.86
74.879 0.999414 768942 1706.67
199.551 0.999512 769017 2048.00
204.543 0.999561 769054 2275.56
207.487 0.999609 769092 2560.00
211.327 0.999658 769130 2925.71
223.487 0.999707 769168 3413.33
266.239 0.999756 769206 4096.00
344.575 0.999780 769223 4551.11
356.351 0.999805 769242 5120.00
362.239 0.999829 769261 5851.43
367.359 0.999854 769280 6826.67
382.975 0.999878 769300 8192.00
386.303 0.999890 769308 9102.22
401.407 0.999902 769317 10240.00
406.783 0.999915 769327 11702.86
481.791 0.999927 769336 13653.33
570.879 0.999939 769346 16384.00
574.463 0.999945 769350 18204.44
578.559 0.999951 769356 20480.00
583.167 0.999957 769361 23405.71
584.191 0.999963 769365 27306.67
585.727 0.999969 769369 32768.00
592.895 0.999973 769372 36408.89
595.455 0.999976 769374 40960.00
596.991 0.999979 769376 46811.43
598.527 0.999982 769378 54613.33
599.551 0.999985 769381 65536.00
600.063 0.999986 769382 72817.78
600.575 0.999988 769383 81920.00
601.087 0.999989 769384 93622.86
602.111 0.999991 769385 109226.67
602.623 0.999992 769387 131072.00
602.623 0.999993 769387 145635.56
603.647 0.999994 769388 163840.00
603.647 0.999995 769388 187245.71
604.159 0.999995 769389 218453.33
605.695 0.999996 769390 262144.00
605.695 0.999997 769390 291271.11
605.695 0.999997 769390 327680.00
605.695 0.999997 769390 374491.43
607.231 0.999998 769392 436906.67
607.231 1.000000 769392 inf
#[Mean = 7.352, StdDeviation = 8.518]
#[Max = 606.720, Total count = 769392]
#[Buckets = 27, SubBuckets = 2048]
----------------------------------------------------------
969485 requests in 38.67s, 278.96MB read
Requests/sec: 25068.07
Transfer/sec: 7.21MB
參考
- https://github.com/giltene/wrk2/issues/54
- https://mp.weixin.qq.com/s/n8a4wzmf6I8kUc-T47PylA
- http://highscalability.com/blog/2015/10/5/your-load-generator-is-probably-lying-to-you-take-the-red-pi.html
- https://medium.com/@siddontang/the-coordinated-omission-problem-in-the-benchmark-tools-5d9abef79279
- https://www.jianshu.com/p/bfb2b0f50edd
- https://momeis.net/post-7362.html