假設我們有以下串列。此串列包含 REST 服務器在流量運行中的回應時間。
[1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
我需要以下輸出
特定時間內服務的請求百分比(毫秒)
50% 3
60% 4
70% 5
80% 6
90% 7
100% 9
我們如何在 python 中完成它?這是 apache bench 型別的輸出。所以基本上讓我們說在 50% 時,我們需要在串列中找到低于 50% 的串列元素存在的點,依此類推。
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
responseTimes = [1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
for time in range(3,10):
percentage = len([x for x in responseTimes if x <= time])/(len(responseTimes))
print(f'{percentage*100}%')
“所以基本上讓我們說 50%,我們需要在串列中找到低于 50% 的串列元素的點,依此類推”
responseTimes = [1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
percentage = 0
time = 0
while(percentage <= 0.5):
percentage = len([x for x in responseTimes if x <= time])/(len(responseTimes))
time =1
print(f'Every time under {time}(ms) occurrs lower than 50% of the time')
uj5u.com熱心網友回復:
您基本上需要計算排序回應時間的累積比率。
from collections import Counter
values = [1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
frequency = Counter(values) # {1: 2, 2: 1, 3: 2, ...}
total = 0
n = len(values)
for time in sorted(frequency):
total = frequency[time]
print(time, f'{100*total/n}%')
這將使用相應的比率列印所有時間。
1 20.0%
2 30.0%
3 50.0%
4 60.0%
5 70.0%
6 80.0%
7 90.0%
9 100.0%
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479554.html
下一篇:最大功能似乎無法正常作業?
