directory

  • Brief introduction:
    • Psotman: Http request emulation tool
        • 1.1 Download Address
        • 1.2 Operation Description (Using Windows 64 as an Example)
    • Apace Bench(AB) : a tool that comes with Apache to test web site performance
        • 2.1 introduction
        • 2.2 the principle
        • 2.3 Apache Bench Installation
        • 2.4 Apache Bench usage
        • 2.5 Usage Precautions
    • JMeter: Pressure test tool developed by Apache organization (use more tools)
        • 3.1 introduction
        • 3.2 JDK Installation (skip this step if JDK 1.8 is already installed)
        • 3.2 JMeter install
        • 3.3 use JMeter
        • 3.4 JMeter performance test
            • 1. Configure Http request (thread group) information
            • 2. Execute test information
            • 3. Analysis and interpretation of test results
    • Fourth, code simulation
        • 1, the Semaphore
        • 2, CountDownLatch
    • Five, the summary

Brief introduction:

A, Psotman

Second, Apache Bench (AB)

Third, JMeter

Four, code,


Psotman: Http request emulation tool

Strictly speaking, postMan is not a concurrent request, but a serial execution, now use postMan is more used to test Http connection a tool, but also a very practical tool, but also has some more powerful functions and advantages

1.1 Download Address

  1. Postman for MAC
  2. Postman for windows X64
  3. Postman for windows X86
  4. Postman for linux X64
  5. Postman for Linux X86
  6. The website address

1.2 Operation Description (Using Windows 64 as an Example)

1. Open Postman, enter the url we need to test, and click the “+” on the left to save the request



2. After clicking “+”, the following prompt will pop up. Save the link address to generate the name of the test folder



3. After the save, select “Collections” and select the folder named “gbfTest”, click the triangle — click Run

4, here we can set the number of requests and interval time, generally the interval will be set to 0, after setting, we click Run gbfTest to Run

5. Check the results. From the figure, we can see that Baidu’s response speed is very fast

6. Click Run Summary to see an overview of the results



7. All tests passed



8. Test Results can be exported by clicking Export Results button to facilitate the analysis of each request time



Apace Bench(AB) : a tool that comes with Apache to test web site performance

2.1 introduction

Apache Bench is a web stress test tool of Apache server, abbreviated as AB. Ab is also a command line tool, which has low requirements on the host that initiates the load. According to the ab command, you can create many concurrent access threads to simulate multiple visitors accessing a URL address at the same time, so it can be used to test the load pressure of the target server. In general, the AB tool is small and simple. It can stress test not only the Apache server, but also other types of servers. For example, nginx, Tomcat, and IIS are quick to learn and provide required basic performance indicators, but without graphical results, they cannot be monitored

2.2 the principle

The ab command creates multiple concurrent access threads to simulate multiple visitors accessing a URL at the same time. Its test target is URL-based, so it can be used to test the load stress of Apache as well as nginx, Lighthttp, Tomcat, IIS and other Web servers

2.3 Apache Bench Installation

Apache server download address:www.apachelounge.com/download/

Options: HTTPD – 2.4.41 – win64 – VS16

Download directory structure:

2.4 Apache Bench usage

Apache Bench version: HttpD-2.4.41-win64-VS16

1. Go to the CMD directory and go to the Apache Bench directory that we extracted



3 Using Commandsab -c 10 -n 10 http://www.baidu.com/orab.exe -n 1000 -c 500 http://www.baidu.comBaidu home page for a total of 1000 requests, the number of concurrent requests for 500 test



Parameter analysis:

C:\Users\Lenovo\Downloads\httpd-2.4. 41-win64-VS16\Apache24\bin>ab.exe -n 1000 -c
 500 http://www.baidu.com/path
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000Requests Server Software: Apache # request Server Hostname: www.baidu.com # request Server Port:80Document Path: / Path # Document Length:222Bytes # Concurrency Level:500# Concurrency, set one of the parameters Time takenfor tests:   45.805Complete requests in seconds1000# Failed requests:0# non-2xx responses:1000# If the header information of the received HTTP response data contained status codes other than 2XX, another statistical item named "Non-2XX Responses" would be displayed in the test results to count the number of requests in this part (1000) Total transferred:484000Bytes # Summation of response data length for all requests HTML transferred:222000Bytes # indicates the total of Requests per second for all Requests:21.83[#/ SEC] (mean) [#/ SEC] (mean) [#/ SEC] (mean)22902.310[ms] (mean) # User average request wait time. Time per request:45.805[MS] (mean, across all Concurrent requests) # That is, a request (in this case, one500Average time required for each Transfer rate10.32[Kbytes/ SEC] Received # Length of data received by these requests from the server in unit timeConnection Times (ms)Min mean[+/-sd] Median Max Connect: 9 46 460.5 11 9060 Processing: 25 20066 13796.4 14798 36549 Waiting: 14 16163 15029.9 8460 36484 Total: 37 20111 13785.1 22804 36562 Percentage of the requests served within a certaintime (ms)75% 34181 80% 34791 90% 35877 95% 36416 98% 36502 in 33043ms Return 99% 36512 100% 36562 within 36502ms(longest request)
Copy the code

Command parameters:

-n requests Number of requests to perform // Total number of requests initiated in this test-c concurrency Number of multiple requests to make// The number of requests (or concurrent requests) generated at one time

-t timelimit Seconds to max. wait forresponses// The maximum number of seconds that a test can take, with no time limit by default.

-r Don-p postfile File containing data to POST// A File containing data to POST, The file format is p1=1& P2 =2. TXT -t content-type content-type header for POSTing //POST data for POSTing Such as -t "application/x - WWW - form - urlencoded". (With -p) -v Verbosity How Much Troubleshooting Info to print // Sets the level of information detail. - 4 or more troubleshooting information will be displayed. A value of 3 or greater displays the response code (404, 200, etc.), and a value of 2 or greater displays warnings and other information. -v Displays the version number and exits. -c attribute Add cookie, eg. -c "C1 =1234, C2 =2,c3=3" (REPEATable) // -c cookie-name=value Attaches a cookie: line to the request. This typically takes the form of a pair of arguments to name=value. This argument can be repeated and separated by commas. Tip: you can use the session principle JSESSIONID parameters, keep the function of the session, such as - C "c1 = 1234, c2 = 2, c3 = 3, JSESSIONID = FF056CD16DA9D71CB131C1D56F0319F8". -w Print out results in HTML tables By default, it is a two-column width table on a white background.Copy the code

2.5 Usage Precautions

1. MAC comes with Apache.

2. When the ab command is used, Too many open files are generated, causing an error: Too many open files

Bench, with its lightweight features, is still suitable for small and medium-sized enterprises, especially for simulating multi-machine tests that access pages

JMeter: Pressure test tool developed by Apache organization (use more tools)

3.1 introduction

Apache JMeter is a pure Java written load functional testing and performance testing open source tools. Compared to Loadrunner, JMeter is small, lightweight and free, and has gradually become a mainstream performance testing tool that every tester must master

JDK version: 1.8 Runtime environment: Windows 7 64 JMeter version: 3.3

Note: JDK versions Java8 and above are required to install JMeter

3.2 JDK Installation (skip this step if JDK 1.8 is already installed)

1, JDK official website download address:www.oracle.com/technetwork…

2. Select Java SE 8U231 and click JDK to download



3. Install the downloaded JDK

4. Configure system environment variables

Specific can search for information, here will not do too much description

3.2 JMeter install

1, official website download address:JMeter address

Apache JMeter 5.1.1 (Requires Java 8+)

3. Decompress the ZIP package and double-click the jmeter.bat file in the bin directory

3.3 use JMeter

1. Double-click the jmeter.bat file in the bin directory to open the Apache Jmeter tool



Test Plan – > Add – > Thread(User) – > Thread Group



3. Set the name and number of threads



Thread parameter interpretation:

Number of Threads (users): Number of virtual users (i.e. number of threads). One virtual user occupies one process or thread

Ramp-Up Period(in seconds): Preparation duration, which indicates the time required for all virtual users to start.

For example, if the number of threads is 20 and the preparation time is 2, it takes 2 seconds to start 20 threads, or 10 threads per second

Loop Count: number of cycles Number of requests sent per thread

If the number of threads is 20 and the number of loops is 100, then each thread sends 100 requests. Total number of requests: 20 x 100=2000. If "Forever" is checked, all threads will keep sending requests and stop running the script as soon as they stop.

Delay Thread creation until needed: Delays thread creation until needed

Scheduler: scheduler, set the start time and end time of the thread group (when configuring the scheduler, you need to check the cycle times to be forever)

Duration(Seconds): Duration (seconds), test duration, will overwrite the end time

Startup delay(Seconds): Startup delay (s) : indicates the test startup delay time, which overwrites the startup time

4. Add the Http request

Right-click Htto Request > Add > Sampler > Http Request

Next, let’s pair interfaceshttps://www.baidu.com/s?ie=UTF-8&wd=edgPerform the performance test, as shown below:

Request parameters:

  • Web Server(Http service) : 1, Protocol[Http] : Protocol for sending Http requests to the target Server. The value can be Http or HTTPS. The default value is Http. Name (domain name) or IP address of the server to which the HTTP request is sent
  • Http Reuqeset(Http request body) : 1. Method: indicates the request Method type, such as GET, POST, HEAD, PUT, OPTIONS, TRACE, and DELETE. 2. Destination URL, excluding the server address, port, and request parameters. Content encoding: utF-8
  • Request parameters: Set request parameters, all set in the list below,
  • URL Encode: specifies whether the URL is encoded. Conten-type: specifies the content Type, which needs to be adjusted accordingly (the default Value is generally used). Include Equals: specifies whether the URL is encoded

Note: When the parameter is passed into Chinese, you need to check “URL Encode”.

The buttons here operate on the data in the list

Delte: delete a row of data Up: set the list parameters Up Down: set the list parameters Down Add from Clipdoard: Add from what we copiedCopy the code



5. Add view result tree

1, right-click Http request > Add > Listener > View Results Tree

2. Here we set the response data format:HTML Source FormattedClick Run to see the result of the request



3. The title of the returned result page isEdg_ Baidu search

6. Add user-defined variables

Add user-defined Variables for Http request parameterization: Http request > Add > Config Element > User Defined Variables

2. Add request parameters and store search keywords



3. Use this parameter in the Http test request in the format ${wd}, as shown in the figure below



7. Add response assertions

Right click on Http test request > Add > Assertions > Response Assertion



To verify that the returned text contains the search term, add the parameter ${wd} to the schema to be tested

Contains Matches, Equals, and SubString. Not ContainsCopy the code

Add the result of the response assertion

Right click Http test Request > Add > Listener > Assertion Results



Click Run to query the running result

Add aggregate reports

Right-click Http request > Add > Listener > Aggregate Report

Now that we’ve written a complete PERFORMANCE test for the Http interface, we’ll test its performance

3.4 JMeter performance test

1. Configure Http request (thread group) information

Click Http Request (Thread group) to configure the parameters related to the performance test

Number of threads: 50

Number of cycles: forever

Duration: 60 seconds

2. Execute test information

Select the aggregate report, query the structure, click the arrow button to start the test, if you want to clear the specific content, click the small broom, clear the debugging result

3. Analysis and interpretation of test results

1. Open the aggregate report

Parameter description:

  1. Label: Each JMeter element has a Name attribute, and the value of the Name attribute is shown here
  2. #Samples: Number of requests – Indicates how many requests were made in this test

    If you simulate 10 users and each user iterates 10 times, then 100 is shown here
  3. Average: Average response time – By default, this is the average response time of a single Request

    When Transaction Controller is used, the average response time is displayed in terms of Transaction
  4. Median: The response time of 50% of users
  5. 90% Line: indicates the response time of 90% users
  6. 99% Line: indicates the response time of 99% users
  7. Min: indicates the minimum response time
  8. Max: indicates the maximum response time
  9. Error% : Error rate – number of incorrect requests/total requests
  10. Throughput: throughput — The number of requests completed per Second by default

    When Transaction Controller is used, it can also represent Transaction per Second numbers similar to LoadRunner
  11. KB/Sec: data received from the server per second

In practice, the only points we need to pay attention to are #Samples request number, Average Average response time, Min minimum response time, Max maximum response time, Error% Error rate and Throughput

Fourth, code simulation

1, the Semaphore

Semaphore is a counting Semaphore. Semaphore manages a range of licenses. Each acquire method blocks until a license is available and then takes a license; Each release method adds a license, which may free a blocking acquire method. However, there is no actual license object, Semaphore just maintains a number of licenses available.



1.1 Code demo: Get a license

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreExample1 {

    private final static int threadCount = 20;

    public static void main(String[] args) throws Exception {

        ExecutorService exec = Executors.newCachedThreadPool();

        final Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    semaphore.acquire(); // Get a license
                    test(threadNum);
                    semaphore.release(); // Release a license
                } catch (Exception e) {
                    log.error("exception", e); }}); } exec.shutdown(); }private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000); }}Copy the code

As shown in the figure below, we can see that although the result is unordered, the number of threads requested is correct. Here we can obtain one license and also release one license. Can we obtain multiple licenses and release multiple licenses



1.2 Code demonstration: Obtain multiple licenses

package com.mmall.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreExample {

    private final static int threadCount = 20;

    public static void main(String[] args) throws Exception {

        ExecutorService exec = Executors.newCachedThreadPool();

        final Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    semaphore.acquire(3); // Obtain multiple permissions
                    test(threadNum);
                    semaphore.release(3); // Release multiple permissions
                } catch (Exception e) {
                    log.error("exception", e); }}); } exec.shutdown(); }private static void test(int threadNum) throws Exception {
        log.info("{}", threadNum);
        Thread.sleep(1000); }}Copy the code

Execution result: Here we see that we set up to acquire and release multiple permissions, put back the correct number of threads, and execute results in order

2, CountDownLatch

CountDownLatch is a synchronization utility class that is used to coordinate synchronization between multiple threads, or for communication between threads (rather than as a mutual exclusion). CountDownLatch enables one thread to wait for other threads to complete their work before continuing. This is implemented using a counter. The initial value of the counter is the number of threads. As each thread completes its task, the counter is reduced by one. When the counter value is 0, all threads have completed their work, and the threads waiting on CountDownLatch can resume their work

2.1 Code Demonstration:


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CountDownLatchExample1 {

    private final static int threadCount = 200;

    public static void main(String[] args) throws Exception {
        int num = 0;

        ExecutorService exec = Executors.newCachedThreadPool();

        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            num++;
            exec.execute(() -> {
                try {
                    test(threadNum);
                } catch (Exception e) {
                    log.error("exception", e);
                } finally{ countDownLatch.countDown(); }}); } countDownLatch.await(); log.info("finish——"+num);
        exec.shutdown();
    }

    private static void test(int threadNum) throws Exception {
        Thread.sleep(100);
        log.info("{}", threadNum);
        Thread.sleep(100); }}Copy the code



We can see that the number of threads we set is the same as the number of threads we set to 200

Five, the summary

PostMan: non-professional concurrent testing, postMan is not strictly a concurrent request, but a serial execution, postMan is more used to test a tool for Http connections, is a very practical tool

Apache Bench: Apache Bench is a web stress test tool built with the Apache server. Ab is simple and easy to learn, and can provide basic performance indicators. However, it does not have graphical results and cannot be monitored

JMeter: Apache JMeter is a Java-based stress testing tool developed by the Apache organization. A tool for stress testing software that can be used to test static and dynamic resources such as static files, Java applets, CGI scripts, Java objects, databases, FTP servers, and so on

In general, JMeter and Apache Bench are better choices for concurrent testing. Ab is faster because Apache Bench is command line based, while JMeter is more accurate because JMeter itself supports assertions, variable parameters, and CSV data set input. PostMan is a tool that simulates Http requests and is not a professional concurrent request tool.