Python Module Introduction-multi-mechanize Universal Performance testing Tool

Source: Internet
Author: User
Tags html form http authentication install matplotlib

Brief introduction

Multi-mechanize is an open-source performance and load testing framework that concurrently runs multiple Python scripts to generate load on a Web site or service (a composite transaction). The test output report is saved as HTML or JMeter-compatible XML. Multi-mechanize is most commonly used for Web performance and Scalability (scalability) testing, and also for any API that Python can access. Especially suitable for background performance testing. With a little bit of programming, this tool will be far stronger than commercial performance testing tools.

Key Features:

    • Supports various HTTP methods

    • Advanced hyperlinks and HTML form support

    • Support SSL

    • Automatic processing of Cookies

    • HTTP headers can be set

    • Automatically handle redirects

    • Support Agent

    • Support HTTP Authentication

Installation

Use the standard Python installation method. Note that you need to install Matplotlib to support the drawing, so install yum-y install python27-matplotlib under CENTOS6. Multi-mechanize uses the standard Python installation method pip install Multi-mechanize or Easy_install multi-mechanize here are Linux (CentOS) as an example.

    • CentOS 5 Installation Note:

      • Note Installation dependencies: Python26 python26-devel python26-numpy* Python26-tornado

      • To Epel support: http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

Quick Start

Create a project

# Multimech-newproject My_project

Execute project

# Multimech-run My_project user_groups:2 threads:6[================100%==================] 30s/30s transactions:1 timers:119 errors:0waiting for all requests to finish...analyzing Results...transactions:125errors:0test Start:2 013-09-13 11:47:47test finish:2013-09-13 11:48:16created:./my_project/results/results_2013.09.13_11.47.46/ Results.htmldone.

Test results See:



Directory structure

Each test project contains the following content:

    • The config.cfg configuration file. Used to set test options.

    • test_scripts/the directory of virtual user scripts. Add your test script here.

    • results/: The results Store directory. For each test it is claimed that a timestamp directory contains the results of the report.

multimech-newproject, a random number script is generated by default. The script v_user.py as follows:

Import Randomimport Timeclass Transaction (object): Def __init__ (self): Pass def Run (self): R = Random. Uniform (1, 2) time.sleep (r) self.custom_timers[' Example_timer '] = rif __name__ = = ' __main__ ': trans = Tra Nsaction () Trans.run () print trans.custom_timers

    • Run_time:duration of test (seconds) execution time

    • Rampup:duration of user rampup (seconds) How many seconds Inside Out request

    • Results_ts_interval:time series interval for results analysis (seconds) results analyze time

    • Progress_bar:turn on/off console progress bar during test run show progress bar

    • Console_logging:turn on/off Logg Whether ing to stdout output to stdout

    • Xml_report:turn on/off XML/JTL report generates XML reports.

    • Results_database:database connection string (optional) to save the resulting database connection string (optional)

    • post_run_s Cript:hook to call a script at Test completion (optional) the aftercare script (optional)

For more information see: http://testutils.org/multi-mechanize/configfile.html

Script writing

Writing in Python, the test script simulates a virtual user's request for a website/service/API, the script defines the user's transaction, and more on the Scripting Manual: Http://testutils.org/multi-mechanize/scripts.html#scripts-label.

Each script must implement a transaction () class. This class must implement a run () method. The basic test script structure is as follows:

Class Transaction (object): Def run (self): # does something here return

Transaction () is instantiated once during operation, and the run () method is called repeatedly:

Class Transaction (object): Def __init__ (self): # does Per-user user Setup here # The gets called once on U Ser creation return def run (self): # does user actions here # gets called repeatedly ret Urn

Class Transaction (object): Def __init__ (self): Pass def Run (self): BR = mechanize.  Browser () br.set_handle_robots (False) resp = br.open (' http://192.168.4.13/env.htm ') assert (Resp.code = =), ' Bad response:http%s '% resp.codes assert (' service name ' in Resp.get_data ())

below, rewrite the script with the Httplib library and add the timer. With timers, you can analyze the time-consuming aspects of each step.

Import httplibimport timeclass transaction (object):     def run (self):         conn = httplib. Httpconnection (' 192.168.4.13 ')         start = time.time ()         conn.request (' GET ',  '/env.htm ')          request_time = time.time ()          Resp = conn.getresponse ()         response_time =  Time.time ()         conn.close ()          transfer_time = time.time ()         self.custom_ timers[' Request sent '] = request_time - start         self.custom_timers[' response received '] =&Nbsp;response_time - start        self.custom_timers[' Content transferred '] = transfer_time - start         assert  (resp.status == 200),  ' bad response: http %s '  %  resp.statusif __name__ ==  ' __main__ ':    trans =  Transaction ()     trans.run ()     for timer in  (' request  sent ',  ' response received ',  ' content transferred '):         print  '%s: %.5f secs '  %  (Timer, trans.custom_timers[timer])

There are more instances of Http://testutils.org/multi-mechanize/scripts.html#scripts-label.


Resources
    • https://pypi.python.org/pypi/multi-mechanize/1.2.0

    • Http://testutils.org/multi-mechanize

    • Author Blog: http://my.oschina.net/u/1433482

    • Type: Finishing


Python Module Introduction-multi-mechanize Universal Performance testing Tool

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.