JDK, Tomcat, Redis, and MySQL are assembled to teach you how to build an efficient performance testing project

Foreword:

As a software engineer, how can you not know how to build a performance test project? Performance testing is an indispensable part of a software engineer, because the performance of a software is directly related to the user experience, and the user experience is the most important factor for the success of a software. This article will take you step by step to build a performance testing project, including how to build JDK, Tomcat, Redis and database, and introduce how to use these tools for performance testing in a WEB project.

1. JDK installation

I believe everyone is familiar with the installation of JDK, so I won't say much here. After installing the JDK, we can check whether the JDK is successfully installed with the following command:

$ javac -version

If the command line outputs the JDK version information, it means that the JDK installation is successful.

2. Tomcat installation

Tomcat is an open source Java web application server, which needs to be manually downloaded and uncompressed to any directory you want. We call it `<TOMCAT_HOME>` here.

$ wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-10/v10.1.0-M3/bin/apache-tomcat-10.1.0-M3.tar.gz
$ tar -xzvf apache-tomcat-10.1.0-M3.tar.gz

After Tomcat is installed, we need to configure it. Open the `conf` directory under the Tomcat directory, find the `server.xml` file, and edit it to the following content:

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<Service name="Catalina">

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost">

<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />

</Host>
</Realm>
</Engine>
</Service>
</Server>

This configuration file is used to start the Tomcat server. After the configuration is complete, we can try to start the server:

$ cd <TOMCAT_HOME>/bin
$ ./startup.sh

Through the above command, we successfully started the Tomcat server.

3. Redis environment construction

Redis is an open source memory data structure storage system that can be used as a database, cache and message middleware. We need to install Redis on the server, the following is the installation process:

$ wget http://download.redis.io/releases/redis-6.2.4.tar.gz
$ tar -xzf redis-6.2.4.tar.gz
$ cd redis-6.2.4
$ make

If no error is reported after all commands are executed, it means that Redis is installed successfully.

4. Database installation

Here, we choose MySQL as our database. MySQL is an open source relational database that can provide efficient and stable data storage.

First, we need to install MySQL. The following is the installation process of MySQL:

$ sudo apt update
$ sudo apt install mysql-server
$ sudo mysql_secure_installation

The above command installs MySQL, and at the same time performs basic security settings, including setting the password of the root user and prohibiting root remote login.

Also, we need to create a test database in MySQL, the command is as follows:

mysql> CREATE DATABASE testdb;

5. WEB project construction

Next, we need to build a WEB project for our performance testing. Here we use the Spring Boot framework to build a simple WEB project. The following is the construction process of the Spring Boot project:

First, we need to add Spring Boot Starter dependencies in Maven:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.0</version>
</dependency>

Next, we need to write a Controller, the following is a simple Controller:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@RestController
public class HelloController {

@Autowired
DataSource dataSource;

@GetMapping("/hello")
public String hello() {
return "Hello World!";
}

@GetMapping("/mysql")
public String mysql() {
try {
Connection connection = dataSource.getConnection();

PreparedStatement stmt = connection.prepareStatement("SELECT 1");

ResultSet rs = stmt.executeQuery();

rs.close();
stmt.close();
connection.close();

return "MySQL OK";
} catch (SQLException e) {
e.printStackTrace();
return "MySQL Error";
}
}

@GetMapping("/redis")
public String redis() {
Jedis jedis = new Jedis("localhost");
String value = jedis.get("testkey");
jedis.close();
if (value != null) {
return "Redis OK";
} else {
return "Redis Error";
}
}
}

Finally, we need to package and run our project:

$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

At this point, the WEB project is built.

6. Performance testing

Now we need to test the performance test project we built. Here we will use Apache JMeter for stress testing. The following is the process of Apache JMeter installation:

$ wget https://downloads.apache.org//jmeter/binaries/apache-jmeter-5.4.1.tgz
$ tar -xzvf apache-jmeter-5.4.1.tgz

After the installation is complete, we can start JMeter:

$ /path/to/apache-jmeter-5.4.1/bin/jmeter

After JMeter starts, we need to create a new test plan:

1. Click `File -> New` to create a new test plan.
2. Select `Test Plan`, right-click on the `Thread Group` node on the right, and select `Add -> Sampler -> HTTP Request`.
3. Enter our server address (such as `localhost`) in `Server Name or IP` of `HTTP Request`, enter the port started by Tomcat (such as `8080`) in `Port Number`, and enter the port in `Path` Enter the address of the method we want to perform performance testing (such as `/hello`).
4. Select the `Thread Group` node, and set basic information such as server address and port number in `HTTP Request Defaults` on the right, so that each `HTTP Request` path can be used directly.
5. Click the `Start` button or press `Ctrl+R` to start the test.

The above is a basic performance test, and we can conduct more tests as needed, such as concurrency testing, load testing, and so on. No specific introduction is given here, and interested readers can learn more about it by themselves.

7. Summary

This article introduces how to build a performance test project and conduct a simple performance test. We have covered the construction of tools such as JDK, Tomcat, Redis and MySQL, and also introduced how to use Spring Boot to build a simple WEB project. Performance testing is an indispensable step in software development. Through testing, we can discover and fix potential performance problems, improve user experience and software quality.

In subsequent development, we can also use these tools and methods to continuously perform performance testing and optimization to ensure that our software always has good performance and user experience.

The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/131703690