Golang性能測試-通過wrk工具測試QTS,QPS,QOS

wrk下載和安裝

先安裝git

<code>

cd

/usr/

local

/src yum install git -y/<code>

下載wrk文件

<code>

git

clone https://github.com/wg/wrk.git

cd

wrk

make

/<code>

編譯成功後,目錄下就會有一個wrk文件。

環境配置信息

CPU:8核心16線程

內存:16G

服務運行平臺:windows10 64位

壓測服務運行平臺:centos6.5

golang測試接口性能指標-帶磁盤IO操作

該接口表示獲取uploads文件夾更目錄下所有文件列表。我們用wrk壓測該接口看下性能指標。

<code>

func

listHandler

(w http.ResponseWriter, r *http.Request)

{ exists := isExists(

"./uploads"

)

if

!exists { os.MkdirAll(

"./uploads"

, os.ModePerm) } fileInfoArr, err := ioutil.ReadDir(

"./uploads"

)

if

err !=

nil

{ http.Error(w, err.Error(), http.StatusInternalServerError)

return

} listHtml :=

""

for

_, fileInfo :=

range

fileInfoArr { imgid := fileInfo.Name() listHtml += + imgid +

"">"

+ imgid +

"

"相冊上傳""

""

) }/<code>

壓測命令:

<code>.

/wrk -t12 -c400 -d30s http:/

/

192.168

.136

.1

:

91

/all/<code>

壓測結果:

<code>

[root@vm1

wrk]#

./wrk

-t12

-c400

-d30s

http://192.168.136.1:91/all

Running

30s

test

@

http://192.168.136.1:91/all

12

threads

and

400

connections

Thread

Stats

Avg

Stdev

Max

+/-

Stdev

Latency

117.

79ms

14.

15ms

212.

55ms

87.51

%

Req/Sec

282.37

92.47

656.00

81.68

%

100822

requests

in

30.

06s,

21.

44MB

read

Requests/sec:

3353.87

Transfer/sec:

730.

38KB

/<code>

golang測試接口性能指標-常規接口

這是一個普通的接口,向客戶端輸出html信息即可。

<code>func upload(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { pushHtmlString(w, "

<

a

href

=

'/all'

>

相冊查看

a

>

"+"

<

form

method

=

\

"

POST

"

action

=

\

"/

upload

" "+ "

enctype

=

\

"

multipart

/

form-data

">

"+ "Choose an image to upload:

<

input

name

=

\

"

image

"

type

=

\

"

file

" />

"+ "

<

input

type

=

\

"

submit

"

value

=

\

"

Upload

" />

"+ "

form

>

") return } }/<code>

壓測結果:

<code>

[root@vm1

wrk]#

./wrk

-t12

-c400

-d30s

http://192.168.136.1:91/upload

Running

30s

test

@

http://192.168.136.1:91/upload

12

threads

and

400

connections

Thread

Stats

Avg

Stdev

Max

+/-

Stdev

Latency

23.

58ms

15.

25ms

308.

32ms

93.15

%

Req/Sec

1.

51k

349.33

10.

53k

80.72

%

535748

requests

in

30.

10s,

164.

52MB

read

Requests/sec:

17797.87

Transfer/sec:

5.

47MB

/<code>

測試總結

在當前運行環境下,golang編寫的一個常規web接口,QPS為17797.87。由於壓測服務器是vmware虛擬機裡,有性能損耗。

按參考教程裡測試結果顯示,多次測試的結果在 4 萬左右的 QPS 浮動,響應時間最多也就是 40ms 左右,對於⼀個 Web 程序來說,這已經是很不錯的成績了,我們只是照抄了別⼈的示例代碼,就完成了⼀個⾼性能的 hello world 服務器。

筆記總結

<code>

package

main

import

"fmt"

/**

golang接口性能測試

壓測工具

wrk。參考教程:

https://www.cnblogs.com/ycyzharry/p/8372168.html

性能指標:

TPS:Transactions

Per

Second

寫接口的性能指標,每秒可以完成的事務數量

QPS:

Queries

Per

Second

查詢接口的性能指標,每秒可以完成的查詢數量

也叫吞吐量

QoS:

Quality

of

Service

單個接口服務質量

1

.普通查詢接口QPS獲取

./wrk

-t12

-c400

-d30s

http://192.168.136.1:91/all

[root@vm1

wrk]#

./wrk

-t12

-c400

-d30s

http://192.168.136.1:91/all

Running

30s

test

@

http://192.168.136.1:91/all

12

threads

and

400

connections

Thread

Stats

Avg

Stdev

Max

+/-

Stdev

Latency

117.

79ms

14.

15ms

212.

55ms

87.51

%

Req/Sec

282.37

92.47

656.00

81.68

%

100822

requests

in

30.

06s,

21.

44MB

read

Requests/sec:

3353.87

Transfer/sec:

730.

38KB

Requests/sec:

x

x就是

qps

2

.數據庫寫接口TPS獲取

目前尚未提供該數據庫交互接口,應該是post接口。需要配合post.lua腳本完成

./wrk

--latency

-t100

-c1500

-d120s

--timeout=15s

-s

post.lua

http://127.0.0.1:91/saveToDb

Requests/sec:

x

x就是

tps

qps

wrk補充說明:

wrk可以配合lua腳本,完成各種接口測試,如帶文件上傳,post請求,form表單提交,隨機數等等。通過編程手段完成壓測。

*/

func

main5()

{

fmt.Println("註釋介紹golang接口性能測試")

}

/<code>

問題延伸

任何一個接口都有自己的QPS吞吐量,可以最大程度的去提高這個值,但不會達到無限大,既然總有一個極限值,那就意味著,當請求特別大總會突破這個QPS瓶頸,這時候會發生什麼呢?常規默認情況下,服務會降低速度或奔潰,所有訪問的用戶都會受到影響,訪問慢甚至服務不可達。

這時候就引入流量限制服務的概念,超出qps的請求進行合理的處理,保障qps以內的請求是可用的。

比如阿里的開源系統:sentinel流量控制,就是做這個事情的。詳細介紹參考:


https://github.com/alibaba/Sentinel/wiki/介紹

golang有自己的第三方庫進行流量控制, sentinel流量控制主要服務於java系統。