1.測試前提,參考先熟悉grpc測試demo:html
參考官網:python 實現grpc client以及service :java
https://grpc.io/docs/quickstart/python/python
java實現client 和service:https://grpc.io/docs/quickstart/java/linux
細節我就不講了:git
主要說下.proto文件,他是實現grpc的基礎,根據他產生契約helloworld_pb2,以及客戶端服務端調用類:helloworld_pb2_grpc(核心調用client編寫就靠他的Stub 類):github
生成契約和grpc client和 service 類golang
$ python -m grpc_tools.protoc -I ../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.protoshell
依賴文件:json
grpcio==1.28.1 grpcio-tools==1.28.1 protobuf==3.11.3 coverage>=4.0 cython>=0.29.8 enum34>=1.0.4 #protobuf>=3.5.0.post1 six>=1.10 wheel>=0.29
setuptools==46.1.3
2.linux 環境搭建安裝python3:工具
虛擬環境:基於virtualenv
安裝依賴
3.安裝ghz工具,安裝go:
安裝go環境linux教程:https://www.runoob.com/go/go-environment.html
ghza安裝包下載:https://github.com/bojand/ghz/releases
wget https://github.com/bojand/ghz/releases/download/v0.52.0/ghz_0.52.0_Linux_x86_64.tar.gz
ghz 幫助文檔:https://ghz.sh/docs/options
配置ghz和go環境變量vi/etc/profile:
export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:/project
source /etc/profile:
驗證golang ,ghz 環境:
go version
ghz -h
4.啓動grpc server:
編寫一個簡單的服務端grpc_server和對應的client 先mock下server可用,而後進行ghz工具壓測server :
[root@vm_master helloword]# cat grpc_client.py import logging import time import grpc import helloworld_pb2 import helloworld_pb2_grpc import json def run(): with grpc.insecure_channel('localhost:50051') as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) req={"name":"jack","id":1001}
# transfer json object to bytes to send grpc request body=json.dumps(req).encode("utf-8") response = stub.SayHello(helloworld_pb2.HelloRequest(name=body)) # print(type(response.message)) print(response.message) if __name__ == '__main__': logging.basicConfig() run()
grpc_server.py:
from concurrent import futures import logging import grpc import helloworld_pb2 import helloworld_pb2_grpc class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): return helloworld_pb2.HelloReply(message= request.name) def serve(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port('[::]:50051') server.start() server.wait_for_termination() if __name__ == '__main__': logging.basicConfig() serve()
啓動服務端 python grpc_server.py,在運行client測試,查看結果:
C:\Python37\python.exe D:/workspace/demos/python/helloword/grpc_client.py
{"name": "jack", "id": 1001}
Process finished with exit code 0
,接着進行壓測:
前面已經安裝好了ghz ,接着進行壓力測試:
鑑於ghz 命令基於linux因此打算藉助腳本調用 ghz 壓力測試命令,以及請求參數構造:
腳本:
import subprocess import json def exe_cmd(args): sub=subprocess.Popen(args,shell=False,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) out,err=sub.communicate() return out.decode("utf-8") def demo(): bytes_data={"name":"zhangsan","id":"10001"} bytes_data=json.dumps(bytes_data).encode("utf-8") args=["ghz","--skipTLS","--insecure","--proto","../../protos/helloworld.proto", "--call","helloworld.Greeter.SayHello","--metadata",bytes_data,"-c","5","--qps","100","-z","1m","--connections","5","-t","20s","0.0.0.0:50051"] print(exe_cmd(args)) if __name__ == '__main__': demo()
這裏輸出默認是命令行展現結果:
[root@vm_master helloword]# python deso.py
Summary:
Count: 29761
Total: 60.01 s
Slowest: 71.66 ms
Fastest: 0.52 ms
Average: 3.63 ms
Requests/sec: 495.95
Response time histogram:
0.515 [1] |
7.630 [29219] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
14.744 [384] |∎
21.859 [133] |
28.973 [8] |
36.088 [2] |
43.202 [0] |
50.317 [3] |
57.431 [2] |
64.546 [4] |
71.660 [1] |
Latency distribution:
10 % in 1.67 ms
25 % in 2.03 ms
50 % in 3.36 ms
75 % in 4.82 ms
90 % in 5.76 ms
95 % in 6.37 ms
99 % in 12.27 ms
Status code distribution:
[OK] 29757 responses
[Unavailable] 2 responses
[Canceled] 2 responses
Error distribution:
[2] rpc error: code = Unavailable desc = transport is closing
[2] rpc error: code = Canceled desc = grpc: the client connection is closing
爲了方便閱讀,能夠保存爲html 只需添加參數 -o ./result.html -O html既可
args=["ghz","--skipTLS","--insecure","--proto","../../protos/helloworld.proto",
"--call","helloworld.Greeter.SayHello","--metadata",bytes_data,"-o","./result.html","-O","html","-c","5","-n","10","0.0.0.0:50051"]
html參考效果:https://ghz.sh/sample.html
output指標解釋:https://ghz.sh/docs/output
注意事項:
def demo():
bytes_data={"name":"zhangsan","id":"10001"}
bytes_data=json.dumps(bytes_data).encode("utf-8")
args=["ghz","--skipTLS","--insecure","--proto","../../protos/helloworld.proto",
"--call","helloworld.Greeter.SayHello","--metadata",bytes_data,"-c","5","--qps","100","-z","1m","--connections","5","-t","20s","0.0.0.0:50051"]
print(exe_cmd(args))
這裏bytes_data=裏整數按字符串填寫,不然go 轉換出錯,其次 通常 -d {"key":"value"}須要在proto文件定義type,這裏爲了通用傳輸字節,我直接按metadata元數據傳輸他會自動處理
技術羣答疑羣: