python测试grpc_如何在python中用蝗虫io负载测试grpc应用程序

python测试grpc

This article gives a brief introduction to how you can load test gRPC based applications written in any programming languages like Java or Python using an open-source tool called Locust.io. This article preassumes that you have a good understanding of gRPC service and python as a programming language. Locust doesn’t support gRPC officially but this blog will help you in implementing it when you need to load test your applications based on gRPC.

本文简要介绍了如何使用称为Locust.io的开源工具加载以Java或Python等任何编程语言编写的基于gRPC的应用程序的测试。 本文假定您对gRPC服务和python作为一种编程语言有很好的了解。 Locust并不正式支持gRPC,但是当您需要基于gRPC进行应用程序负载测试时,此博客将帮助您实现它。

To learn about gRPC, you can browse through the official documentation. To understand the capability of Locust as a tool for load test you can read it’s documents and implementations.

要了解gRPC,您可以浏览官方文档 。 要了解Locust作为负载测试工具的功能,可以阅读Locust的文档实现

加载测试步骤 (Steps to Load Test)

  1. The first requirement is we need to have a server that consumes gRPC services, please note that the server can be written in any programming language and it does not impact our load test model. For simplicity, I have taken a dummy `Hello world` server in python from the gRPC official code repository.

    第一个要求是我们需要有一台使用gRPC服务的服务器,请注意,该服务器可以用任何编程语言编写,并且不会影响我们的负载测试模型。 为简单起见,我从gRPC官方代码存储库中以python提取了一个虚拟的“ Hello world”服务器。

  2. Clone the code from the language of your choice, gRPC by default gives dummy server in their different examples as listed in the python repository.

    从您选择的语言中克隆代码,默认情况下,gRPC在python存储库中列出的不同示例中提供了虚拟服务器。
Image for post

We will take a basic example of HelloWorld and implement its load test in the further part of the blog. If in case you choose a server in different languages then what needs to be done here is start your server and compile your proto file in your load test repository and use the locust code as shared in the blogs.

我们将以HelloWorld的基本示例为例,并在博客的另一部分中实现其负载测试。 如果万一您选择了其他语言的服务器,那么这里需要做的就是启动服务器并在负载测试存储库中编译原始文件,并使用博客中共享的蝗虫代码。

3. To simplify even more I have prepared a code repository with all default servers in python provided by gRPC and implemented locust on HelloWorld example. Here is a link to clone the repository or download it as zip.

3.为了进一步简化,我准备了一个代码存储库,其中包含gRPC提供的python中的所有默认服务器,并在HelloWorld示例中实现了蝗虫。 这是克隆存储库或将其下载为zip的链接。

Clone: https://github.com/ManojSingh0302/automation.gitDowloand: https://github.com/ManojSingh0302/automation

4. Now as you have codes, we need to install few dependencies before we start execution. Open the repository in the editor of your choice and simply install it by running the below commands on the terminal.

4.现在有了代码,我们需要在开始执行之前安装一些依赖项。 在您选择的编辑器中打开存储库,只需在终端上运行以下命令即可安装它。

Note: It is advised to create a virtual environment in python 3.7 and follow the below steps.

注意:建议在python 3.7中创建虚拟环境,并按照以下步骤操作。

----- For creating Virtual environment -----
$ python -m virtualenv venv
$ source venv/bin/activate----- For Installing dependencies -----$ pip install -r requirement.txt
$ pip install -e .

5. To start the server run the following command $ python server/python/helloworld/greeter_server.py

5.要启动服务器,请运行以下命令$ python server/python/helloworld/greeter_server.py

6. To manually test the server you can run the default client by the following commands $ python server/python/helloworld/greeter_client.py Yeah! we got the response from the server and so the server is running successfully.

6.要手动测试服务器,您可以通过以下命令运行默认客户端$ python server/python/helloworld/greeter_client.py是的! 我们从服务器获得了响应,因此服务器正在成功运行。

7. Now in a given scenario to test gRPC, you will be given the .proto file, from this file we need to generate the python code which we will be using in our test scripts for making a call to the server. Follow the below steps to generate compile files in python from .proto file. For understanding the repository contains a separate folder as proto which will have a proto file and compile files.

7.现在,在给定方案中测试gRPC,将为您提供.proto文件,我们需要从该文件中生成python代码,该代码将在测试脚本中用于调用服务器。 请按照以下步骤从.proto文件生成python编译文件。 为了理解,存储库包含一个单独的文件夹作为proto,它将具有proto文件和编译文件。

Image for post
fig: Proto file
无花果:原始文件

Run the following command from the project directory (grpc-load-test-with-locust).

从项目目录(grpc-load-test-with-locust)运行以下命令。

command syntax:$ grpc_tools.protoc -I=$SRC_DIR - python_out=$DST_DIR - grpc_python_out=$SRC_DIR proto/<app-name>.protoActual command:$ python3 -m grpc_tools.protoc -I=server/proto --python_out=server/proto --grpc_python_out=server/proto server/proto/helloworld.proto

After the successful execution of the command, the python compile files will be generated as shown below.

成功执行命令后,将生成python编译文件,如下所示。

Image for post
Fig: Python Compile files
图:Python编译文件

8. For our load test, we already have compiled files in the official repository of gRPC to run from our code I have just updated the imports w.r.t our framework. Now we need to load test this server and for the job, we have the locust code inlocust/load_test_grpc.py

8.对于负载测试,我们已经在gRPC的官方存储库中编译了文件,可以从我们的代码运行,而我刚刚更新了框架的导入。 现在我们需要对该服务器进行负载测试,并且为了完成这项工作,我们在locust/load_test_grpc.py有蝗虫代码。

#!interpreter [optional-arg]
# -*- coding: utf-8 -*-


"""
This contains to load test of Hello World example of gRPC call with locust.
"""


# Built-in/Generic Imports
import sys
import grpc
import inspect
import time
import gevent


# Libs
from locust.contrib.fasthttp import FastHttpUser
from locust import task, events, constant
from locust.runners import STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP, WorkerRunner
from server.python.helloworld import helloworld_pb2
from server.python.helloworld import helloworld_pb2_grpc




__author__ = 'Manoj Singh'


def stopwatch(func):
    """To be updated"""


    def wrapper(*args, **kwargs):
        """To be updated"""
        # get task's function name
        previous_frame = inspect.currentframe().f_back
        _, _, task_name, _, _ = inspect.getframeinfo(previous_frame)


        start = time.time()
        result = None
        try:
            result = func(*args, **kwargs)
        except Exception as e:
            total = int((time.time() - start) * 1000)
            events.request_failure.fire(request_type="TYPE",
                                        name=task_name,
                                        response_time=total,
                                        response_length=0,
                                        exception=e)
        else:
            total = int((time.time() - start) * 1000)
            events.request_success.fire(request_type="TYPE",
                                        name=task_name,
                                        response_time=total,
                                        response_length=0)
        return result


    return wrapper


class GRPCMyLocust(FastHttpUser):
    host = 'http://127.0.0.1:50051'
    wait_time = constant(0)


    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        pass


    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        pass


    @task
    @stopwatch
    def grpc_client_task(self):
        """To be updated"""
        try:
            with grpc.insecure_channel('127.0.0.1:50051') as channel:
                stub = helloworld_pb2_grpc.GreeterStub(channel)
                response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
                print(response)
        except (KeyboardInterrupt, SystemExit):
            sys.exit(0)


# Stopping the locust if a threshold (in this case the fail ratio) is exceeded
def checker(environment):
    while not environment.runner.state in [STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP]:
        time.sleep(1)
        if environment.runner.stats.total.fail_ratio > 0.2:
            print(f"fail ratio was {environment.runner.stats.total.fail_ratio}, quitting")
            environment.runner.quit()
            return




@events.init.add_listener
def on_locust_init(environment, **_kwargs):
    if not isinstance(environment.runner, WorkerRunner):
        gevent.spawn(checker, environment)

9. Now to Run the locust in web mode run the following command$ locust -f locust/load_test_grpc.py

9.现在要以Web模式运行蝗虫,请运行以下命令$ locust -f locust/load_test_grpc.py

10. Now in the logs, we can observe that the Locust load test sever started on default port[8089] and we can access by URL http://localhost:8089/ in any browser.

10.现在,在日志中,我们可以看到Locust负载测试服务器在默认端口[8089]上启动,并且可以在任何浏览器中通过URL http:// localhost:8089 /进行访问。

11. Now to start the test select Number of total users to simulate along with the Hatch rate (users spawned/second) of your choice as shown below.

11.现在,要开始测试,请选择要模拟的总用户数以及您选择的孵化率 (产生的用户数/秒),如下所示。

Image for post

Note: initially select smaller number so that your system responde to the load.

注意:最初选择较小的数字,以便您的系统响应负载。

12. Now Start Swarming and observe the statistics and charts, you can stop the locust at any given point.

12.现在开始群居并观察统计数据和图表,您可以在任何给定的点停止蝗虫。

图表 (Charts)

Image for post
Fig: Charts
图:图表

统计 (Statistics)

Image for post
Fig: Statistics
图:统计

It also provides you the feature to download the statistics from the load test.

它还提供了从负载测试下载统计信息的功能。

Image for post
Fig: Download link
图:下载链接

蝗虫防治方法 (Ways to run locust)

To run locust from command line, without web-ui, run following command
locust -f locust/load_test_grpc.py --headless -u <no-of-users> -r <hatch/second> -t <stop-time>
e.g. locust -f locust/load_test_grpc.py --headless -u 10 -r 5 -t 3s
WHERE
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users. Only used together
with --headless
-r HATCH_RATE, --hatch-rate HATCH_RATE
The rate per second in which users are spawned. Only
used together with --headless
-t RUN_TIME, --run-time RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with
--headless

Console Output

控制台输出

Image for post
Fig: Console output
图:控制台输出

Hurray!! We have done it. :-)

欢呼!! 我们已经做到了。 :-)

I hope this blog was useful to you. Please leave comments or send me an email if you think I missed any important details or if you have any other questions or feedback about this topic.

我希望这个博客对您有用。 如果您认为我错过了任何重要的详细信息,或者对此主题还有其他疑问或反馈,请发表评论或给我发送电子邮件。

翻译自: https://medium.com/@manojsingh0302/how-load-test-grpc-applications-with-locust-io-in-python-2dd79114b6e6

python测试grpc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值