Stress Testing CPU, Memory, Disk Read/Write, and Network
Recently, a senior schoolmate in the lab had a requirement: write four small programs to stress test memory, CPU, disk, and network respectively. The test programs needed adjustable pressure levels, such as high, medium, and low. No more words; roll up the sleeves and get to work. The requirement was simple, but implementation was not. I learned while writing the programs and barely completed the task.
Test Environment
Ubuntu 14.04
CPU
Inspired by the first chapter of The Beauty of Programming, “Make the CPU Usage Curve Follow Your Commands,” I quickly completed the CPU stress testing program.
Basic Idea
Determine a small period. I set the period to 100, and this value can be changed by changing the value of PIECE; the period is 100 * PIECE.
Within each period, stress percent of the time is spent in an infinite loop, and the remaining (100-stress) percent of the time calls usleep, note the difference between sleep and usleep.
Program Source Code
Usage
1 | $ 程序名 stress # 其中 0<=stress<=100 |
Disk Read/Write
For disk stress testing, I implemented read and write operations with two separate programs.
Disk Read Operation
Basic Idea
Similar to the CPU stress test, within one period, 1s, first read a specific amount of data, speed, from disk, then sleep for the remaining time in the period.
Program Source Code
Usage
1 | $ python3 disk_read.py 硬盘名 level # 其中level可以是0, 1, 2, 3 |
Disk Write Operation
Basic Idea
The stress test for disk write operations is similar to read operations. Within one period, 1s, write a specific amount of data to a file, then delete it, and sleep for the remaining time in the period.
To write data into the file, I used the commanddd if=/dev/zero of=/path/to/targetfile bs=1024k count=speed conv=fdatasync > /dev/null 2> /dev/null.
This command reads data from /dev/zero, which is actually invalid data and is often used as a data source for initializing files, and writes it to /path/to/targetfile. To avoid the error output and standard output produced by dd affecting the readability of this program, I used redirection. /dev/null is a black-hole device; any data can be input into it without bad effects.
Program Source Code
Usage
1 | $ python3 disk_write.py 硬盘名 level # 其中level可以是0, 1, 2, 3 |
Memory
Basic Idea
For memory testing, my senior schoolmate introduced me to the program memtester. Specific installation and usage:
1 | $ # 安装 |
In general, there is no need to specify PHYSADDR, because it may destroy memory occupied by other processes, which has some risk.
Using the memtester program, I did some simple processing and completed the requirement.
Program Source Code
Usage
1 | $ python3 memory_benchmark.py <MEMORY> [ITERATIONS] |
Network
The senior schoolmate’s requirement was for public network stress testing. I looked through a lot of materials; most tools measured network speed, rather than allowing a specified upload or download rate for testing. Fortunately, after searching for several days, I finally found a tool, iperf, that could barely complete the task.
Basic Idea
One machine acts as the server, and another machine acts as the client. The client sends packets to the server, the server receives them, and the connection uses UDP. That is to say, if there is no server, the client can still stress test network upload; but without a client, the server cannot stress test network download. In simple terms, the server, download, is passive, while the client, upload, is active. You can still send packets wildly even if nobody receives them, but if nobody sends packets, no matter what, you cannot receive packets.
Installation
1 | $ sudo apt-get install iperf |
Usage
Server
Start the server:
1 | $ iperf -s |
Client
1 | $ iperf -c 服务器ip -b 压力值 |
References
How to monitor network on Linux
Linux network testing and monitoring
[Linux] Test network speed in a LAN