Jmeter introduction and use

Introduction to jmeter

Basic concepts of jmeter

  • Apache JMeter is a Java-based stress testing tool developed by the Apache organization. Used for stress testing of software, it was originally designed for web application testing, but later expanded to other testing areas. It can be used to test static and dynamic resources, such as static files, Java servlets, CGI scripts, Java objects, databases, FTP servers, etc. JMeter can be used to simulate huge loads on servers, networks or objects, test their strength under different stress categories and analyze overall performance. In addition, JMeter can perform functional/regression testing of the application, by creating a script with assertions to verify that your program returns the results you expect. For maximum flexibility, JMeter allows the use of regular expressions to create assertions

Advantages of jmeter

  • Open source and free, written based on Java, can be integrated into other systems, can expand various functional plug-ins
  • Support interface test, stress test and other functions, support recording and playback, easy to get started
  • Compared with writing frameworks or other open source tools by yourself, it has a more complete UI interface, which is convenient for interface debugging
  • Multi-platform support, can run on Linux, Windows, Mac
  • Supported protocol
    • web: HTTP, HTTPS site Web1.0 Web 2.0 (ajax, flex and flex-ws-amf)
    • Web Services: SOAP / XML-RPC
    • Database via JDBC driver
    • Directory: LDAP
    • Message-oriented services through JMS
    • Service: POP3, IMAP, SMTP
    • FTP service

jmeter installation

  • 1. Download the JDK file to install, and configure the environment variables
  • 2. Download and unzip from jmeter official website
  • 3. Start jmeter.bat in the bin directory
  • 4. Sinicization and garbled resolution
    • Modify the jmeter.properties file under the bin directory
      • sampleresult.default.encoding=utf-8
      • language=zh_CN

jmeter directory structure

  • bin directory (introduction to commonly used files)
    • examples: The directory contains Jmeter usage examples
    • ApacheJMeter.jar: JMeter source code package
    • jmeter.log: JMeter running log file
    • jmeter.properties: JMeter configuration file
      • The attribute log_level.Jmeter (log detail level), the default imfo, can be modified to Debug-
    • jmeter.bat is the startup file, some of the parameters support configuration
      • The heap (heap memory) is set to half of the physical memory at most, and the default setting is 521M; if the heap exceeds half of the physical memory, running jmeter may be slow or even overflow
  • docs directory: JMeter help documentation
  • printable_docs directory
    • usermanual subdirectory: user manual,
      • component_reference.html: The most commonly used core file help manual
  • extras directory: provide support for ant, and use ant to realize automation; for example, execute scripts in batches and generate html reports
  • lib directory
    • The ext subdirectory is the core jar package of jmeter;
    • The junit subdirectory is for the junit script
    • The package that the user extension depends on can be placed directly in the lib directory

jmeter use

Test setup

  • Construction steps and meaning
    • Configuration elements (config elements) --- all elements within the scope
    • Pre-processors (Per-processors) --- executed before each sampler component within the scope
    • Timer (timers) ---Each sampler in the scope is valid
    • The Sampler component does not interact with other components, so there is no scope problem
    • Post-processors---execute after each sampler component in the scope
    • Assertions --- Perform verification of the results of each sampler component within the scope of execution
    • Listeners --- Collect and present the information of each sampler element in its scope
  • Execution order and scope
    • Execute from top to bottom in a tree structure
    • The control element under a sampler is only valid for that sampler

Thread group

  • Meaning of each field
    • Number of threads: the number of simulated users
    • ramp-up period: preparation time, how long does it take for the set virtual users to start up
    • Number of cycles: the number of cycles for a single user and a single interface
  • Field expansion
    • Total number of threads: number of threads * number of cycles
    • Total duration: ramp-up period * number of cycles
    • Scene loading strategy: ramp-up period / number of threads

HTTP request

  • Protocol and port
    • http:80
    • https:443
  • Request method (commonly used)
    • get
      • The parameter is in the url, starting with? Variable 1 & Variable 2
    • post
      • key=value format: put in the parameter
      • json format: put in the message body
        • Need to add http request header Conten-type: application/json
      • key=json format: put in the parameter
  • Timeout setting ( unit: ms )
    • Request and response timeout (5s or 10s is recommended)

BeanShell related

  • classification

    • Preprocessor: BeanShell PreProcesser
    • Sampler: BeanShell Sampler
    • Post processor: BeanShell PostProcesser
    • Assertions: BeanShell assertions
  • Write the script directly in the script

  • Commonly used built-in variables and methods

    • log: write log to the console
      • log.info('variable name')
    • vars: manipulate jmeter variables
      • vars.get("skuId"): Get the value of the ${skuId} variable from jmeter
      • vars.put("name","test"): Save "test" to the ${name} variable
    • prev: Get the information returned by the previous sampler
      • getResponseDataAsString(): Get response information
      • getResponseCode(): Get response code
  • Call jar package

    • Import the jar package in the test plan
    • import required classes;
  • Call java source code

    • source('jar path')
  • BeanShell assertion

    • Failure: Whether it failed, boolean type
    • FailureMessage: Failure log, displayed when the assertion fails

Use of checkpoints (assertions)

  • Assertion classification (commonly used)
    • Response assertion
    • Json Assertion
    • Assertion duration
    • Size Assertion: response byte size

to parameterize

  • Three parameterization methods
    • User parameters
    • CVS Data Config: Import files
    • Random variables---generated by the function assistant
  • Commonly used random functions
    • __Random: Generate random numbers
    • __RadndomString: Generate a random string
    • __time: timestamp
    • __UUID: Generate a unique UUID
    • __CSVRead: CSV file read
      • A thread selects only the first row
  • Optional parameters generated by the function
    • When the function is generated, the random number can be stored in a variable
    • The variable can be called again

Logic controller

  • Commonly used logic controller
    • Loop controller: You can set the number of executions of the sampler in the controller, the number of cycles and the number of threads are independent of each other
    • if controller: Decide whether to execute the request in the controller according to the judgment condition. If it is a string comparison condition, the parameter and string need to be quoted
      • Conditional format: {__jexl3({__jexl3("${num}" == "10")}
      • jex
    • Only once controller: The request in the controller is executed only once, no matter how many times the thread loops
    • foreach controller: It can traverse a parameter array and get the parameters in the array in a loop

Timer

  • Commonly used timers
    • Fixed timer: set a fixed sleep time
    • Synchronization timer (meeting point)
    • Constant throughput timer
  • Synchronization timer (meeting point)
    • What is a meeting point?
      • Multiple users gather to a point in time concurrently
    • Meeting point settings
      • Number of user groups: 0, which is equivalent to the number of threads in the thread group
      • Number of user groups: non-zero, which means that after reaching the set users (threads), subsequent operations will be executed concurrently (the number of threads cannot be exceeded )
      • Timeout time: 0, Timer will wait until the number of threads reaches the value set for the number of user groups before executing. If it is not enough, it will wait all the time.
      • Timeout time: non-zero, if the set maximum waiting time is exceeded, but the value set by the number of user groups has not been reached, the Timer will no longer wait
  • Constant throughput timer
    • What is a constant throughput timer?
      • TPS for control interface
    • Why control?
      • To control the interface with too high TPS, occupy the CPU
    • Field
      • Target throughput
      • Based on computational throughput: It is recommended to select all active threads in the current thread group

Configuration element

  • HTTP Cookie Manager
    • How Jmeter automatically associates cookies
      • 1. Add an empty HTTP Cookie manager to the thread group
      • 2. Bring in after extracting through regular
  • HTTP header management
    • Data stored in the interface header

Dynamic association

  • Two ways
    • Regular expression extractor
      • Reference name: variable name
      • Regular expression generation
        • 1. Copy target data and left and right boundaries
        • 2. Enclose the target data in parentheses
        • 3. Replace the target data with .+?
      • template:
      • Matching number: 1
    • json extractor
      • Format: $. Path
      • Debug sampler used in conjunction with viewing result tree

Plug-in extension

  • Plugin official website
  • Several useful plugins
    • 3 Basic Graph: real-time TPS and response time plug-ins available under windows
    • Customer Jmeter Functions: Extension functions
    • Random CSV Data Set Config: Random csv file parameterization
    • PerfMon: Server-side performance real-time monitoring plug-in

Use of the database

  • step
    • Add the mysql-driven jar package to the test plan
      • Add directory or jar to classpath
    • JDBC Connection Configuration
      • DataBaseURL
      • JDBC Driver Class
      • Username
      • Password
    • JDBC Request
      • query type
        • Select Statement query
        • Update Statement
      • If you need multiple users to configure different SQL, you can put the SQL statement in the csv file and call it through parameterization
    • Affirmation
    • Listener

Distributed performance test

  • Why is it distributed?
  • Answer: jmeter is developed by java and consumes memory and cpu, so it still needs to be distributed under large concurrency
  • step
    • Turn off the firewall
    • Select one machine as the controller and other machines as the agent, install all jmeter applications, and run the jmeter-server file in the bin directory of the agent machine
    • Find the jmeter.properties file in the bin directory of the controller machine and modify the remote_hosts address
    • Click Run in the jmeter application of the controller machine-->Remote start

Listener

  • Commonly used listeners
    • Assertion result
    • View the result tree
    • Aggregate report
    • View the results in a table
    • Graphical results
    • aggregate graph
    • and so on. . . . .
  • File save
    • Save as jtl file by default
    • jtl file analysis
  • Convergence report meaning and analysis
    • The meaning of each field ( unit: milliseconds )
      • Lable: Define the name of the HTTP request
      • Samples: How many requests were issued in this test
      • Average: Average response time
      • Median: 50% of users’ response time
      • 90%Line99%Line: 90%99% user response time
      • Min: minimum response time
      • Max: Maximum response time
      • Error%: Error rate
      • Throughput: throughput-the default indicates the number of completed requests per second
      • Received KB/src: The amount of data received from the server per second
      • Sent KB/src: The number of requests sent from the client per second
    • Performance index analysis
      • Concurrent number (number of users)
      • Response time
      • Throughput (TPS)
      • flow
      • Hardware index
        • CPU
        • I / O
        • Memory

Command line execution

  • Ready to work
    • 1. Install and configure the JDK on the press
    • 2. Configure jmeter environment variables
    • 3. Debug the jmeter script
  • Single machine test steps:
    • Execute jmeter -n -t test.jmx -l result.jtl
      • -n: command line mode, no-gui
      • -t: jmx script path;
      • -l: jtl result file storage path
  • Html report generation steps
    • 1. Enter the bin directory of jmeter and modify reportgenerator.properties
    • 2. Modify jmeter.reportgenerator.overall_granularity=1000 (the interval between data display in the report is 1 second)
    • 3. Create a folder to store data reports
    • 4. Execute the command: jmeter -g result.jtl -o ./output
      • -g: Specify the path of the jtl file
      • -o: Specify the folder under which the html report is generated

Project practice

File upload and download

  • Upload
    • POST request, check use …for post
    • In the file sent with the request, fill in the file name and parameter name
    • MIME type: application/octet-stream
  • download
    • How to judge whether the download is successful?
      • Determine that the bytes of the response body are larger than the bytes of the real file

webservice interface

  • webservice URL
  • Similarities and differences between webservice interface and http interface
    • The webservice is encapsulated based on the http interface and is xml
  • The key points of webservice interface call
    • 1. Put the parameters in the message body
    • 2. Add header: Conent-type: text/xml

Interface signature verification

--------------To be added-------------------

Performance test report

  • table of Contents:
    • Test Plan
    • Reference
    • Term explanation
    • system introduction
    • test environment
    • Test index
    • Testing tools and testing strategies
    • Test data collection
    • Test result data and screenshots
    • Test conclusion

Guess you like

Origin blog.csdn.net/weixin_47967031/article/details/115095777