性能测试中P99的计算
性能中的測試指標有很多,對于響應時間,除了查看90%、95%、99%的響應時間外,現在還有一個P99(可能比較小眾,所以相關資料較少),表示過去 10 秒內最慢的 1% 請求的平均延遲。這個值的意義在于:如果這個值從測試開始到測試結束變化都不大的話,說明程序比較穩定,如果變化非常大,起起伏伏,說明程序里面有些地方在某種情況下有些邏輯沒有處理好,導致問題,當然需要結合其他的內存,CPU,IOPS以及當前的請求數,響應數等等,這個在亞馬遜云服務器也有說明。https://docs.aws.amazon.com/zh_cn/elasticbeanstalk/latest/dg/health-enhanced-metrics.html
那么怎么得到這個值呢?在使用jmeter性能測試中,發現插件和各種處理器都沒有提供這個值的直接獲取。那么我們就自己來編寫腳本獲取了,思路如下:①通過jmeter腳本導出每個請求的響應時間和結束時間;②通過Python腳本計算P99的值,并做出折線圖。具體步驟如下:
一、通過jmeter腳本導出每個請求的響應時間和結束時間;
1、安裝插件:Flexible File Writer
2、編輯Flexible File Writer,獲取響應時間和結束時間等值,保存在txt文檔里,用以后面處理
其中filename 是保存的文件路徑;write file header 是保存文件中的第一行說明,用以標識每列的信息,值中間用空格隔開;record each sample as 記錄的是每個樣本對應的參數值,記錄時,需要和下面的available sample fields保持一致(點擊即可復制),值中間用| |隔開。
3、腳本運行后,就會記錄對應的值在剛保存的txt文檔里,例子如下:
Python讀取第二行的內容是:b'426 1616068614679 2.get meetingroom inf 426 200
'
二、通過Python腳本計算P99的值,并做出折線圖。
具體思路如下:
1、讀取文本文件,獲取第一列和第二列的值,并把這些值保存在列表里。第一列的值是響應時間,第二列的值是endtime;
2、定義一個函數,用于計算99%之外的響應時間的平均數;
3、使用循環語句,計算所有過去10秒內的響應時間的開始位置和結束位置,當總的結束時間不超過10S時,也需要考慮;然后每個位置調用步驟2中的函數,得到P99列表;
4、計算出需要繪圖的X軸和Y軸列表。X軸用相對結束時間列表,Y軸用P99列表;使用matplotlib這個數學繪圖庫進行繪圖。
5、完整代碼如下:
# -*- coding: utf-8 -*-
import logging
import matplotlib.pyplot as plt #Python數據可視化最流行的工具之一是 matplotlib,它是一個數學繪圖庫
import numpy as np
logging.basicConfig(level=logging.DEBUG, filename='logger.log',format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 設置日志級別
file=r'F:/99.practice/paixu/testresult.txt'
other_ave=99
#獲取文檔中第一列:響應時間,輸入到列表里
restimelist=[] #響應時間列表
endtimelist=[] #結束時間列表
otheravelist=[] #其他外響應時間平均值列表
othertimelist=[] #其他結束時間值列表
findstring='\'
with open(file,'rb+') as f:
content=f.readlines() #readlines返回文件中行內容列表
lines=len(content) #readlines返回文件中行內容列表,列表長度即為行數
#print(lines)
print(content[1]) #顯示第2行內容,例如:b'426 1616068614679 2.get meetingroom inf 426 200 '
for i in range(lines):
if i>0: #去除第一行表頭
digit=str(content[i]).find(findstring) #查找每一行中findstring的第一個位置
restime = int(str(content[i])[2:digit]) #每行內容轉換為字符串,使用切片截取字符串
endtime= int(str(content[i])[digit+2:digit+15])
restimelist.append(restime)
endtimelist.append(endtime)
logging.info('response time list is {}'.format(restimelist))
logging.info('end time list is {}'.format(endtimelist))
len_restime=len(restimelist)
def count_other_ave(start,end): #定義函數,計算列表中other_ave之外的響應時間平均數
restimelist_sort=sorted(restimelist[start:end]) #排序響應時間
restimelist_num = len(restimelist_sort) #長度
list_index = round(restimelist_num * other_ave / 100) # 計算第other_ave%的位置,round四舍五入取整
other_ave_result = round(sum(restimelist_sort[list_index:]) / (restimelist_num - list_index)) # 使用sum函數求和,除以剩余值的個數
otheravelist.append(other_ave_result) #插入other_ave之外的響應時間平均數
return
for i in range(len_restime):
if (endtimelist[i]+10000)<endtimelist[-1]: #結束時間+10000毫秒
for j in range(i,len_restime): #從第i位開始,到最后
if endtimelist[j]>=(endtimelist[i]+10000): #從第i位開始,到最后,判斷結束時間大于10S后的位置。
startindex=i
endindex=j
if i%1000==0: #根據樣本數,選擇性來記錄下endtime的開始和結束位置
logging.info('Endtime index begin at {},finish at {}'.format(startindex,endindex))
count_other_ave(startindex,endindex) #計算響應時間列表中other_ave之外的響應時間平均數
othertimelist.append(endtimelist[j])
break #計算一次,跳出循環
else:
count_other_ave(i,len_restime)
othertimelist.append(endtimelist[j])
break #當結束時間第一個和最后一個差距不超過10S,計算一次,跳出循環
logging.info('P{} list is {}'.format(other_ave,otheravelist))
print('The length of p{} list is {}'.format(other_ave,len(otheravelist)))
print('The p{} list is {}'.format(other_ave,otheravelist))
othertimelist_relative=(np.array(othertimelist)-othertimelist[0]).tolist() #先把list變成array,進行減法或者加法操作,再把array變成list
logging.info('end time list relative is {}'.format(othertimelist_relative))
#繪圖開始
plt.figure(figsize=(8,4)) #創建繪圖對象,figsize參數可以指定繪圖對象的寬度和高度,單位為英寸,一英寸=80px
plt.plot(othertimelist_relative,otheravelist,"b--",linewidth=1) #在當前繪圖對象中畫圖(x軸,y軸,藍色虛線,畫線寬度)
plt.xlabel("Time(ms)") #X軸的文字
plt.ylabel("ave_res_time") #Y軸標簽
plt.title("P{} Line plot".format(other_ave)) #圖標題
#plt.show() #顯示圖
plt.savefig("F:/99.practice/paixu/line.png") #保存圖
繪圖函數詳細實例可以參考:https://www.jb51.net/article/130979.htm
總結
以上是生活随笔為你收集整理的性能测试中P99的计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: autoCAD如何绘制多段体
- 下一篇: WINDOWS系统安装JAVA(详细版)