Python Web Performance and stress test multi-mechanize

Source: Internet
Author: User

Http://www.aikaiyuan.com/5318.html

The most common tool for Web services to do performance & load testing is the Apache benchmark commonly known as AB and Commercial tool LoadRunner. AB Simple direct, the function is also relatively weak, but we often see some of the Web server or the framework of performance testing with AB, and the LoadRunner function is indeed very powerful, a variety of large software companies, software outsourcing enterprises almost necessary, very high, Of course, the price is really high.

The multi-mechanize (this one) is a Python-developed performance & load Testing tool, developed recently by Pylot's authors, and is a product of upgrading. Multi-mechanize can be used to write Python script to implement more complex test logic, and its concurrency test is implemented by multiprocessing (multi-process) and multithreading mechanism.

1. InstallationUniversal Pip&easy_installpip Install multi-mechanize mechanize numpy matplotlib
    • Mechanize is a library of simulated browser behavior, but you can also use other libraries such as URLLIB2, request, tornado.httpclient, and so on, not required.
    • The next two numpy and Matplotlib are also optional and will be used when you need it to automatically generate graphical reports, installing matplotlib your system may need to install libpng and freetype libraries.
2. How to use
    • Create a project
    Multimech-newproject My_project

Automatically create a my_project directory, subdirectory test_scripts used to put the test script, Config.cfg is the test configuration, mainly to match the test time, test script and concurrency threads amount.

    • Scripting, a simple example of borrowing from the official:
 # # Copyright (c) Corey Goldberg ([email protected]) # LICENSE:GNU lgplv3## This file was part of Multi-me  Chanize#import mechanizeimport timeclass Transaction (object): Def __init__ (self): Self.custom_timers = {} def Run (self): BR = mechanize. Browser () br.set_handle_robots (False) Start_timer = Time.time () resp = Br.open (' http://www.example.co         m/') resp.read () latency = Time.time ()-Start_timer self.custom_timers[' example_homepage '] = Latency ASSERT (Resp.code = =), ' Bad HTTP Response ' assert (' Example Web Page ' in Resp.get_data ()), ' Failed Cont ENT verification ' if __name__ = = ' __main__ ': trans = Transaction () trans.run () print trans.custom_timers 

Note: By default for multi-mechanize, each script must have a transaction class that has a run method that writes the test business logic in run. This example is to open http://www.example.com, record access time is long, very simple and clear, and the actual scenario you may need to have a user login, and then test one or more pages (API), just test the business more complex, the wording is similar. A script file can only have one transaction class, class can only have a run method, is it very inconvenient to write case? Do not rush, in response to this point, the latter part of the tips will be a way to point you to the road.

    • Run a test script for the project
Multimech-run My_project

The results of the test results report and the original data are placed in a subdirectory generated by the test time in the results directory, and the result of the produced HTML version is as follows:

3. Tips for using
    • Cookies:

If you are using mechanize, you can get cookie information from the browser object BR above in the following way.

br._ua_handlers["_cookies"].cookieja

      • Support for multiple test cases for a single script: this idea stems from the concept of testsuite, the same testsuite case as a set of related cases can share some code logic and resources (such as browser objects), and multi-mechanize default way is not supported, to achieve this, only need a little bit of skill, on the code:

Base.py,transaction base class:

#-*-Coding:utf-8-*-import mechanizeimport timeimport tracebackimport loggingclass BaseTransaction (object): _TEST_CA Se_prefix = "Test_" def __init__ (self): self._init () self.custom_timers = {} Self.browser = Mechan Ize. Browser () self.browser.set_handle_robots (False) self.browser.set_handle_redirect (True) SELF.BROWSER.S            Et_handle_referer (True) def _init (self): Self.funcs = [] Funcs_ = Dir (self) for func_ in Funcs_: If Func_.startswith (self._test_case_prefix): Self.funcs.append (FUNC_) def run (self): "" "All                Inheriting the Basetransaction class, you only need to implement the test case at the beginning of the Test_ method, and the runtime can get the test "" "Try:for func in Self.funcs: Start_timer = Time.time () getattr (Self, Func) () # Run Test latency = Time.time ()-S  Tart_timer self.custom_timers['%s '% Func[len (self._test_case_prefix):]] = latency except Exception, E:loggiNg.error (Traceback.format_exc ()) Raise E 

Test_case_google.py is a real test case where you test multiple Google sites at the same time:

#-*-Coding:utf-8-*-from Base import basetransactionclass Transaction (basetransaction):    def test_google_com_hk ( Self):       #  Test logic code, like test example.com        pass    def test_google_com_sg (self):        pass    def test_ Google_com (self):        Pass
    • True concurrency calculation: Multi-mechanize uses the multiprocessing library, with multiple processes, and each process implements concurrency testing by configuring multiple threads in CONFIG. But the amount of concurrency in a real unit time is not a representation of 10 concurrency per second in config threads=10. The actual concurrency needs to be calculated based on the number of final transaction and how many HTTP requests and total completion times are included in these transaction, which is not very intuitive.
    • Custom stats: You can plug any custom statistics into Self.custom_timers's built-in dictionary, which they can get in the report.

For more documentation and information, please refer to document http://testutils.org/multi-mechanize/and Git code base https://github.com/cgoldberg/multi-mechanize. Finally multi-mechanize is not very good, one is the use of the process found that there are some situations will throw abnormal, resulting in the report can not be generated correctly, another awkward case is written not unittest that set, is the author of the Transaction Flow:)

Python Web Performance and stress test multi-mechanize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.