💻

JMeter Tutorial on Performance Testing

Jun 7, 2024

JMeter Tutorial on Performance Testing

Introduction to JMeter

  • JMeter: An open-source, Java-based application for performance testing.
  • Initially designed for web applications, now supports API performance testing too.

Importance of Performance Testing

  • Ensures smooth user experience.
  • Prevents application slowdown or crash during high traffic, which can impact revenue.
  • Essential for applications expecting high traffic, such as e-commerce platforms.

Setting Up JMeter on Windows

  1. Download and Install Java
    • Needed because JMeter is a Java application.
    • Set environment variables for Java.
  2. Download JMeter
    • Verify Java installation.
    • Download JMeter from the official website.
    • Extract and run the JMeter batch file.

JMeter Directory Structure

  • Root Folder: Apache-JMeter-<version>
  • Subfolders explained:
    • bin: Scripts to run JMeter, examples, templates.
    • lib: Libraries and extensions.
    • docs: Technical documentation.
    • extras: Files for building JMeter.
    • printable_docs: Printable documentation and user manuals.
  • Backups for script recovery.

JMeter User Interface Overview

  • Test Plan: Area to create test scripts.
  • Thread Groups: Define number of virtual users.
  • Samplers: Types of requests to the server.
  • Listeners: Analyze performance metrics and logs.
  • Menu Bar Options: File, Edit, Run, Options, Tools, Help.
    • Best Practice: Use command line (CLI) mode for load testing.
  • Tool Bar Shortcuts for quick actions.
  • Thread Properties: Configure number of threads, ramp-up period, and loop count.

JMeter Elements

  • Test Elements:
    • Thread Groups: Number of users/threads.
    • Samplers: Types of requests (HTTP, FTP, JDBC, etc.).
    • Listeners: View test results and logs.
    • Config Elements: Configuration settings for requests.
    • Timers: Add delays between requests.
    • Pre-processors and Post-processors: Modify request data.
    • Assertions: Validate response data.
    • Logic Controllers: Control the flow of test script execution.
  • Non-test Elements: Help create test scripts (e.g., HTTP Test Script Recorder).

Creating a JMeter Script

  • Example: E-commerce promotion load test.
    • Create Thread Group, Add Sampler, Add Listener.
    • Define user flow and validate responses.
    • Use assertions to validate response data.

Assertions in JMeter

  • Purpose: Verification of response data or performance metrics.
  • Types: Response Assertions, Duration Assertions, Size Assertions, etc.
  • Usage: Configure fields to test, pattern matching rules, and expected outcomes.
  • Listener: Assertion Results for detailed assertion logs.

Using Plugins in JMeter

  • Plugins Manager: Install, manage, and update plugins.
  • Popular Plugins: Extensions to enhance JMeter functionality.
  • Installation: Download plugin manager JAR, add to /lib/ext directory, restart JMeter.
  • Configuration: Customize plugin settings via GUI.

Database Performance Testing with JMeter

  • JDBC Connection Configuration: Set up connection to the database.
  • JDBC Request: Execute SQL queries and validate results.
  • Example: Test query performance with multiple users.

API Testing with JMeter

  • SOAP and REST API Testing:
    • SOAP: Use HTTP Sampler, specify XML body.
    • REST: HTTP Sampler, specify query parameters and JSON body.
    • Validate API responses and measure performance.

FTP Testing with JMeter

  • FTP Sampler: Upload and download files.
  • Configuration: Set up server connection and choose file transfer operations.
  • Performance Testing: Measure upload/download times with multiple users.

Running JMeter Scripts from Command Line (CLI)

  • Benefits: Saves memory and system resources, suitable for continuous integration (CI).
  • Command Syntax: jmeter -n -t test_plan.jmx -l result_file.csv -e -o /path/to/output/folder
  • Handling Errors: Verify paths, ensure result files are empty before execution.

Generating HTML Reports in JMeter

  • Methods:
    • Command Line with new results: -e -o directory flag.
    • Command Line with existing results: Use -g option with result file and output directory.
    • GUI: Tools > Generate HTML Report.
  • Customizing Reports: Edit user.properties, update thresholds and titles.

Making JMeter Scripts Data-Driven

  • CSV Data Set Config: Load data from CSV files.
  • Parameterization: Replace hardcoded values with CSV variables.
  • Example: Dynamic user data for API tests.

Timers in JMeter

  • Purpose: Simulate real user think-time between requests.
  • Types: Constant Timer, Uniform Random Timer, etc.
  • Configuration: Add timers at test plan, thread group, or request level.

Variables and Functions in JMeter

  • Variables: Store values to make scripts modular and easy to update.
  • Functions: Predefined methods to generate data (e.g., random strings, dates).
  • Helper Dialog: Tool to generate and test functions within JMeter.

Logic Controllers in JMeter

  • Simple Controller: Organize test plans without affecting execution.
  • Once Only Controller: Execute requests only once per thread, regardless of loop count.
  • Loop Controller: Execute requests multiple times within specified loops.
  • Interleave Controller: Alternates execution between nested requests.

HTML Link Parser in JMeter

  • Pre-Processor: Parses HTML response and extracts URLs or forms.
  • Use Cases: Spidering (random link clicking), handling dynamic data.
  • Example: Randomly navigate through web pages using parsed links.