Home  >  Article  >  System Tutorial  >  Simulate system load on Linux

Simulate system load on Linux

PHPz
PHPzforward
2024-04-06 13:43:01323browse

在 Linux 上模拟系统负载

System administrators often need to explore the impact on application performance under different loads. This means that the load must be artificially created repeatedly. Of course, you can do this with specialized tools, but sometimes you may not want or be able to install new tools.

Every Linux distribution comes with tools for creating workloads. They are not as flexible as specialized tools, but they are readily available and require no special learning.

CPU

The following command creates CPU load by compressing random data and sending the results to /dev/null:

cat /dev/urandom | gzip -9 > /dev/null

If you want a larger load, or the system has multiple cores, then just compress and decompress the data, like this:

cat /dev/urandom | gzip -9 | gzip -d | gzip -9 | gzip -d > /dev/null

Press CTRL C to terminate the process.

Memory usage

The following command will reduce the total amount of available memory. It does this by creating a file system in memory and writing files into it. You can use as much memory as you want, just write more files into it.

First, create a mount point and then mount the ramfs file system:

mkdir z
mount -t ramfs ramfs z/

The second step is to use dd to create a file in the directory. Here we create a 128M file:

dd if=/dev/zero of=z/file bs=1M count=128

The size of the file can be modified through the following operators:

  • bs= Block size. It can be any number followed by B (representing bytes), K (representing KB), M (representing MB) or G ( means GB).
  • count= How many blocks to write.
Disk I/O

创建磁盘 I/O 的方法是先创建一个文件,然后使用 for 循环来不停地拷贝它。

下面使用命令 dd 创建了一个全是零的 1G 大小的文件:

dd if=/dev/zero of=loadfile bs=1M count=1024

下面命令用 for 循环执行 10 次操作。每次都会拷贝 loadfile 来覆盖 loadfile1

for i in {1..10}; do cp loadfile loadfile1; done

通过修改 {1..10} 中的第二个参数来调整运行时间的长短。(LCTT 译注:你的 Linux 系统中的默认使用的 cp 命令很可能是 cp -i 的别名,这种情况下覆写会提示你输入 y 来确认,你可以使用 -f 参数的 cp 命令来覆盖此行为,或者直接用 /bin/cp 命令。)

若你想要一直运行,直到按下 CTRL+C 来停止,则运行下面命令:

while true; do cp loadfile loadfile1; done

The above is the detailed content of Simulate system load on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete