--- title: JMeter tags: Projects --- # JMeter ## What is perfomance test? A non-functional software testing technique that determines how the **speed, response time, stability, reliability and scalability** of an application holds up under a given workload. ## Why do we need perfomance test? At least one of the following reasons: * To determine whether the application satisfies performance requirements (for instance, the system should handle up to 1,000 concurrent users). * To locate computing bottlenecks within an application. * A bottleneck is a point of congestion in a production system that stops or severely slows the system. * To establish whether the performance levels claimed by a software vendor are indeed true. * To compare two or more systems and identify the one that performs best. * To measure stability under peak traffic events. ## Installation 1. JMeter requires ==Java JDK 8+== to run. * **Note: Java JDK releases a new version every 6 months and releases a LTS every 2 years** * The latest LTS version is **Java JDK 21** (shows below), but you can also use any other 8+ version. ![1702768784191](https://hackmd.io/_uploads/SJ2il2oUT.jpg) * [Java JDK Download Page](https://www.oracle.com/java/technologies/downloads/) * Here I choose the **.exe** file to download, but you can choose what format you prefer. ![1702769154329](https://hackmd.io/_uploads/ryl7Ghi8T.jpg) * Open the installation file, just use default settings, don't have to change anything, click **next**, **next**, then **close** when finish. 2. For Windows system, click the words with ==the red circle== shown in the screenshot below, to download JMeter zip file. * [JMeter Download Page](https://jmeter.apache.org/download_jmeter.cgi) * Don't download it from the **source** section, otherwise you will miss a file called **ApacheJMeter** to run JMeter. ![1702770066587](https://hackmd.io/_uploads/SJ8b83oIa.jpg) * Unzip the downloaded file, execute **jmeter.bat** in the **\bin** directory, and open the JMeter software, it might take a few seconds. * YES!! YOU DON'T HAVE TO INSTALL JMETER, JUST UNZIP AND EXECUTE IT. * Be careful! It has lots of twins also named ==jmeter==, please check the file format. * Don't close the terminal window. ![1702769724498](https://hackmd.io/_uploads/HJ1q43oIa.jpg) ## Use JMeter to create a test plan. * When you open JMeter, you will see ==Test Plan== here. ![1702771301756](https://hackmd.io/_uploads/BJeK5njIa.jpg) * Right clik on Test Plan -> Add -> Threads (Users) -> Thread Group ![Screenshot 2023-12-17 010319](https://hackmd.io/_uploads/BJjJpnoUp.png) * Fill in the information. We just need a name for now. * We simulate only 1 thread because it's other people's server, that's why we don't modify other information here. ![1702852529834](https://hackmd.io/_uploads/r1I-dlpIa.jpg) * Now we configure the settings. * Setup HTTP default request HEADER * Right click on the **thread group**, here my thread group name is **reqres.in_APIs_testing**, Add -> Config Element -> HTTP Request Defaults * HTTP Request Defaults is the default setting for every HTTP Request under this thread group. * ![Screenshot 2023-12-17 234135](https://hackmd.io/_uploads/SJIjYe6I6.png) * Fill in the information. ![1702853250924](https://hackmd.io/_uploads/S1_hqgaUp.jpg) * We will send JSON in the request body for **POST** APIs later, so we add **HTTP Header Manager**, to add **Content-Type: application/json** in the request header * Content-Type: application/json indicates that the request body format will be JSON. * ![Screenshot 2023-12-18 004828](https://hackmd.io/_uploads/SJ9nFbpUa.png) * ![1703018003933](https://hackmd.io/_uploads/SJ2X1YyvT.jpg) * Now let’s add the APIs that we are going to test. * ==All the APIs we use are from [https://reqres.in/](https://reqres.in/)== * Right click on the **thread group**, here my thread group name is **reqres.in_APIs_testing**, Add -> Sampler -> HTTP Request * ![Screenshot 2023-12-17 234936](https://hackmd.io/_uploads/H1QKiepUT.png) * Fill in the information * Let's try **register** together. * 1. Name * 2. Method and path * **Register** and **login** are 99.9% **POST** method. * Of course, check API document from your project. * 3. Register data ![1702854793603](https://hackmd.io/_uploads/HJvheWp86.jpg) * Let's try **login** together. * 1. Name * 2. Method and path * 3. Login data ![1702855123088](https://hackmd.io/_uploads/HyK7GZpI6.jpg) * Same work for other APIs. * Why we don't have to fill in **protocol and server name/IP**? Because we already did that in **HTTP Request Defaults**. * **Get** method normally passes API parameters in the url, not in the HTTP request body. * Get a list of users ![Get a list of users](https://hackmd.io/_uploads/SJCC5baI6.jpg) * Get a single-user ![Get a single-user](https://hackmd.io/_uploads/rJMJj-6La.jpg) * Get another single-user ![Get another single-user](https://hackmd.io/_uploads/By2Xo-aLa.jpg) * Delay response example API ![delay](https://hackmd.io/_uploads/HkzEiZpIa.jpg) ## How to see the result and generate a report? * Commonly we add **View Results Tree** and **Summary Report** to observe the result * Right click on the **thread group**, here my thread group name is **reqres.in_APIs_testing**, Add -> Listener -> View Results Tree * Right click on the **thread group**, here my thread group name is **reqres.in_APIs_testing**, Add -> Listener -> Summary Report ![Screenshot 2023-12-18 002618](https://hackmd.io/_uploads/BJGxEW6Ua.png) * The first time when you want to execute the test flow, but haven't saved the file, JMeter will notify you. * Then we click the green triangle button to execute the test flow. ![1702782111401](https://hackmd.io/_uploads/rkqnNk2La.jpg) * Result Tree page ![Screenshot 2023-12-18 010147](https://hackmd.io/_uploads/H1t52baLa.png) * Summary Report ![Screenshot 2023-12-18 010227](https://hackmd.io/_uploads/ryo92-pLp.png) * You can also right-click and click on clear to clear the result. ![Screenshot 2023-12-18 005952](https://hackmd.io/_uploads/BJOoi-TLT.png) ## Want a more beautiful report? * **After running the test, and having the result values.** * Click on the **Summary Report** on the left. * Click on the **Save Table Data** on the bottom. * Then we can use the **.csv** file that we have just saved for our HTML report. 🥳 ![1703024236366](https://hackmd.io/_uploads/SyU0FqJPT.jpg) * Tools -> Generate HTML report ![image](https://hackmd.io/_uploads/Skq6qqyPa.png) * Fill in all fields. ![1703025757468](https://hackmd.io/_uploads/ryGDCq1wa.jpg) * Remember, in the 3rd step, the folder should be empty. ![should be empty](https://hackmd.io/_uploads/S1YFRc1vp.jpg) * After filling in all the fields, click on the **Generate report** button. ![gooooood](https://hackmd.io/_uploads/ByJ11jkP6.jpg) * You will have the report like this, just open the **index.html** file in the output directory. * ![Screenshot 2023-12-19 234829](https://hackmd.io/_uploads/rJkQkoJDT.png) * ![Screenshot 2023-12-19 234847](https://hackmd.io/_uploads/SyUQJiyD6.png) ## Go further * In real life, we might also want to * Simulate more users, you can modify the **Number of Threads (users)** * Have all the threads running not all at the beginning, but gradually, you can modify **Ramp-up period (seconds)**, it means it will reach the number of threads at this time. * If you want to run the whole plan more than 1 time, you can modify **Loop Count**. ![1702852733680](https://hackmd.io/_uploads/S1SsOlT8T.jpg) * You might want to perform "login" with many users using their usernames and passwords, you can save the information in **.csv** file, and insert the file to JMeter. * And of course, you can edit the script in JMeter to save their **token**s, and use it in the following APIs which need **auth**. * The last but not the least. * If we aim to test max 10000 users, we will schedule our testing plan to test different amount of the numbers, for example, test 1000 ppl, 3000 ppl, 5000 ppl, 8000 ppl, and finally 10000 ppl. * And We can analyse the result, to locate computing bottlenecks within an application.