Python uses JMeter to test the multithreading of tornado

Time:2021-2-24

Introduction to JMeter

  JMeterIs a Java basedStress testing tools. It was originally designed for web application testing, but later extended to other testing areas. It can be used to test static and dynamic resources, such as static files, Java applet, CGI script, Java object, database, FTP server, etc.

JMeter can be downloaded from: https://jmeter.apache.org/download_ jmeter.cgi Before downloading, please make sure that Java is installed in your computer. After downloading, click the“ meter.bat (Windows system) or “JMeter” executable file (UNIX System), the interface is as follows:

We will learn how to use JMeter in the following chapters, taking tornado’s multithreading as an example.

JMeter, taking tornado multi process as an example

We will take tornado’s multithreading as an example to describe how to use JMeter. The Python code of tornado multithreading tested is as follows:

# -*- coding: utf-8 -*-
# author: Jclian91
# place: Sanya Hainan
# time: 2020-01-08 21:48
import time
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from tornado import gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor

#Define the port as 9090
define("port", default=9090, help="run on the given port", type=int)


#Single threaded testing
class SanyaHelloHandler(tornado.web.RequestHandler):
  #Get function
  def get(self):
    time.sleep(0.5)
    self.write('Hello from Sanya!')

#Multithreading test
class ShanghaiHelloHandler(tornado.web.RequestHandler):
  executor = ThreadPoolExecutor(4)

  @gen.coroutine
  def get(self):
    result = yield self.doing()
    self.write(result)

  #Using tornado thread pool
  @run_on_executor
  def doing(self):
    time.sleep(0.5)
    return 'Hello from Shanghai!'


#Principal function
def server():
  #Start tornado service
  tornado.options.parse_command_line()
  #Define app
  app = tornado.web.Application(
      handlers=[(r'/sanya', SanyaHelloHandler),
           (r'/shanghai', ShanghaiHelloHandler)
           ]Web page path control
     )
  http_server = tornado.httpserver.HTTPServer(app)
  http_server.listen(options.port)
  http_server.start()
  tornado.ioloop.IOLoop.instance().start()

server()

Make a brief introduction to the above program:

  • /Sanya path corresponds to sanyahellohandler, the HTTP interface is get request, single thread;
  • /Shanghai path corresponds to Shanghai hellohandler. The HTTP interface is get request. Multithreading is realized by thread pool. The number of threads is 4;

   start the python program above, and the next step is to describe how to use JMeter.

First, create a new test plan, named tornao test, as follows:

Right click add, select threads (users), and then select thread group. The information is as follows:

Fill in the name of thread group as “tornado single thread test”, “number of threads” is 20, which means to simulate 20 users, “ramp up period” is 1, which means to send all users’ requests within 1 second, “loop count” is 2, which means that each user sends 2 requests, so the total number of requests is 40.

Right click on “tornado single threaded test” and select add, select sampler, and then select HTTP request. The information is as follows:

The thread group is used to test the HTTP request corresponding to the Sanya path of tornado.

   finally, we add some listeners to the test to count the performance of the test. In “Sanya”_ Right click add on “test”, select listener, and select “view results tree”, “summary report”, “aggregate report” and “aggregate graph” successively to represent each test result, total test result, aggregate test result and aggregate test graph respectively.

Right click on “tornado single threaded test” and select start. The test results are as follows (only summary report and aggregate graph are displayed)

   it can be found that under this stress test, the average request time is 7472 MS, 2.0 requests per second (TPS).

We test the Shanghai interface in the same way, and the test results are as follows:

It can be found that under the stress test, the average request time is 1718 MS, 7.7 requests per second (TPS).

According to this test, we also found that tornao’s multithreading mechanism played a role.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]