Introduction to Performance Testing Tool Gatling

Source: Internet
Author: User
Tags builtin repetition macbook

Introduction to Performance Testing Tool Gatling

1 Introduction

Gatling is a high-performance server performance testing tool developed based on Scala. It is mainly used to perform load tests on servers and analyze and measure various server performance indicators. Gatling is mainly used to measure HTTP-based servers, such as Web applications and RESTful services. In addition, it has the following features:

  • Supports Akka Actors and Async IO to achieve high performance.
  • Supports generating Html dynamic and lightweight reports in real time, making the reports easier to read and analyze data.
  • Supports DSL scripts for easier development and maintenance.
  • Supports recording and generating test scripts to generate test scripts conveniently.
  • Supports importing HAR (Http Archive) and generating test scripts
  • Supports Maven, Eclipse, and IntelliJ for development.
  • Support for Jenkins for continuous integration
  • Supports plug-ins to expand their functions, such as the Support for other protocols.
  • Open source free

    The applicable scenarios of Gatling include: frequent changes in test requirements, frequent maintenance of test scripts, poor client performance in the test environment, but the ultimate performance of hardware; the test script can be well managed, and CI can be used for continuous performance tests. It is expected that the test results are lightweight and easy to read.

    2. Comparison with Jmeter:

    Figures 1 and 2 show their performance in terms of concurrency performance.

    Figure 1 JMeter 2.8

    Figure 2 Gatling 1.3.2

    3 use,

    3.1 download: http://gatling.io/#/download.

    3.2 file directory Introduction

    • The bin directory contains two scripts: gatling and recorder. gatling is used to run the test, and recorder is used to start the UI of the recording script (not recommended ),
    • The conf directory is about some configurations of Gatling itself.
    • The lib directory is the library file on which Gatling depends.
    • The results directory is used to store test reports.
    • The user-files directory is used to store test scripts.

      When running the gating script, It scans all files in the user-files directory and lists all the Simulation (a test class that can contain any number of test scenarios ). Select one of them, and then fill in the Simulation ID and running description. This is for the report description service.

      3.3 recording-

      3.3.1 start recording:

      • On Linux/Unix:
        $ GATLING_HOME/Bin/recorder. sh
         
      • On Windows:
        %GATLING_HOME% \ Bin \ recorder.

        3.3.2 interface:

         

        3.3.3 recording Configuration

        You must configure an Internet proxy to successfully record the video. After the recording is complete, the code is saved in the user-files/simulations/directory.

        4 run:

        4.1 run the script

        • On Linux/Unix:
          $ GATLING_HOME/Bin/gatling. sh
           
        • On Windows:
          %GATLING_HOME% \ Bin \ gatling. bat

          4.2 all the running scenarios (simulation) will be listed during running, and you can select the one you want to run.

           

          5 test results

           

          The Gatling Test Report is based on HTML and has been generated during the test, so it is very fast to open. In addition, when you move the mouse over different data axes, the detailed test data information is displayed in the pop-up box. This dynamic data display method makes it easy to view and analyze data. Considering the inconvenience caused by real project data, I will explain it through the sample report provided on the official Gatling website.

          Gatling reports are classified into two types: GLOBAL and DETAILS. GLOBAL mainly includes request-related statistics, such as the number of requests per second, and the number of successful and failed requests; among them, DETAILS is mainly the statistics related to the request time, such as the request response time and request response delay time.

          Figure 4 Number of requests per second,

          When you move the cursor to any point in the graph, the detailed data requested at the corresponding time point will be displayed in the white pop-up box in the image. The same function is available in the following request response latency diagram.

          Figure 5 request response latency,

          6. Advanced

          Script Parsing:

          PackageComputerdatabase // 1 package name

          ImportIo. gatling. core. Predef. _ // 2 must be imported

          ImportIo. gatling. http. Predef ._

          ImportScala. concurrent. duration ._

          Class BasicSimulation Extends Simulation{// Class 3 Declaration, must inherit the Simulation

          ValHttpConf=Http // 4 common configuration of all Http requests

          . BaseURL ("http://computer-database.gatling.io") // 5 base URL

          . AcceptHeader ("text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8") // 6 request headers

          . DoNotTrackHeader ("1 ")

          . AcceptLanguageHeader ("en-US, en; q = 0.5 ")

          . AcceptEncodingHeader ("gzip, deflate ")

          . UserAgentHeader ("Mozilla/5.0 (Windows NT 5.1; rv: 31.0) Gecko/20100101 Firefox/31.0 ")

           

          ValScn=Scenario ("BasicSimulation") // 7 define a scenario

          . Exec (http ("request_1") // 8 the name of the http request request_1, which is finally displayed in the report.

          . Get ("/") // 9 get Request Method

          . Pause (5) // 10 pause/think time 5S

          SetUp (// 11 create a scenario

          Scn. inject (atOnceUsers (1) // 12 declare to inject a user

          ). Protocols (httpConf) // Http request configuration declared earlier than 13

          }

          6.1 separation scenario: separation operations: such as browsing, searching, and editing operations

          ObjectSearch{ValSearch=Exec (http ("Home") // let's give proper names, as they are displayed in the reports

          . Get ("/" zookeeper .pause(72.16.exe c (http ("Search"). get ("/computers? F = macbook "paipai.pause(22.16.exe c (http (" Select "). get ("/computers/6 "). pause (3 )}ObjectBrowse{ValBrowse=???}ObjectEdit{ValEdit=???}

          We can now rewrite our scenario using these reusable business processes:

          ValScn=Scenario ("Scenario Name" cmd.exe c (Search. Search,Browse. Browse,Edit. Edit)

          6.2 Set regular users and administrator accounts

          ValUsers=Scenario ("Users" cmd.exe c (Search. Search,Browse. Browse)

          ValAdmins=Scenario ("Admins" cmd.exe c (Search. Search,Browse. Browse,Edit. Edit)

          6.3 set virtual users

          SetUp (users. inject (atOnceUsers (10). protocols (httpConf ))

          6.4 set the pressure to rise, that is, the number of users started at intervals

          SetUp (users. inject (rampUsers (10) over (10 seconds), admins. inject (rampUsers (2) over (10 seconds). protocols (httpConf)

          Start 10 users and 2 administrators within 10 s

          6.5 parameterization or dynamic data

          6.5.1 create a file: Create a cvs file in the user-files/data folder, for example:Search.csv, The content is as follows:

          SearchCriterion, searchComputerName

          Macbook and MacBook Pro

          Eee, ASUS Eee PC 1005PE

          6.5.2 files used:

          ObjectSearch{

          ValFeeder=Csv ("search.csv"). random // 1, 2

          ValSearch=Exec (http ("Home"). get ("/"). pause (1). feed (feeder) // 3

          . Exec (http ("Search"). get ("/computers? F =$ {searchCriterion} ") // 4

          . Check (css ("a: contains ('$ {searchComputerName}')", "href"). saveAs ("computerURL") // 5

          .Pause(1).exe c (http ("Select"). get ("$ {computerURL}") // 6

          . Pause (1 )}

          6.5.3 explanation:

          Explanations:

        • First we create a feeder from a csv file with the following columns:SearchCriterion,SearchComputerName.
        • As the default feeder strategy isQueue, We will useRandomStrategy for this test to avoid feeder starvation.
        • Every time a user reaches the feed step, it picks a random record from the feeder. This user has two new session attributes namedSearchCriterion,SearchComputerName.
        • We use session data through Gatling's EL to parametrize the search.
        • We use a CSS selector with an EL to capture a part of the HTML response, here a hyperlink, and save it in the user session with the nameComputerURL. Note how Scala triple quotes are handy: you don't have to escape double quotes inside the regex with backslashes.
        • We use the previusly saved hyperlink to get a specific page.

          6.6 loop call

          InBrowseProcess we have a lot of repetition when iterating through the pages. We have four times the same request with a different query param value. Can we change this to not violate the DRY principle?

          First we will extract the repeated exec block to a function. Indeed, Simulation's are plain Scala classes so we can use all the power of the language if needed:

          ObjectBrowse{DefGotoPage (page: Int)=Exec (http ("Page" + page). get ("/computers? P = "+ page). pause (1)ValBrowse=Exec (gotoPage (0), gotoPage (1), gotoPage (2), gotoPage (3), gotoPage (4 ))}

          We can now call this function and pass the desired page number. But we still have repetition, it's time to introduce another builtin structure:

          ObjectBrowse{ValBrowse=Repeat (5, "n") {// 1 exec (http ("Page $ {n}"). get ("/computers? P = $ {n} ") // 2. pause (1 )}}

          Explanations:

        • The repeat builtin is a loop resolvedRuntime. It takes the number of repetitions and, optionally, the name of the counter that's stored in the user's Session.
        • As we force the counter name we can use it in Gatling EL and access the nth page

          6.7 result verification :()

          ImportJava. util. concurrent. ThreadLocalRandom // 1

          ValEdit=Exec (http ("Form "). get ("/computers/new" zookeeper .pause(1).exe c (http ("Post "). post ("/computers "). check (status. is (session=>200 +ThreadLocalRandom. Current. nextInt (2) // 2

          Explanations:

          1.
        • We do a check on a condition that's been customized with a lambda. It will be evaluated every time a user executes the request and randomly return200Or201. As response status is 200, the check will fail randomly.

          To handle this random failure we use the tryMax and exitHereIfFailed constructs as follow:

          ValTryMaxEdit=TryMax (2) {// 1 exec (edit)}. exitHereIfFailed // 2

          Explanations:

        • TryMax tries a given block up to n times. Here we try a maximum of two times.
        • If all tries failed, the user exits the whole scenario due to exitHereIfFailed.

          7 others

          7.1 integration with CI

          7.2 Real-time Monitoring: real-time monitoring of users and requests

          7.3 System under test metrics ).

          Reference link:

        • First we import ThreadLocalRandom, to generate random values.

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.