linux下的CPU压力测试工具与方法介绍

使用工具stress

Centos
# yum -y install stress

Ubantu
# apt-get install stress

# stress --help
`stress' imposes certain types of compute stress on your system

Usage: stress [OPTION [ARG]] ...
 -?, --help         show this help statement
     --version      show version statement
 -v, --verbose      be verbose
 -q, --quiet        be quiet
 -n, --dry-run      show what would have been done
 -t, --timeout N    timeout after N seconds
     --backoff N    wait factor of N microseconds before work starts
 -c, --cpu N        spawn N workers spinning on sqrt()
 -i, --io N         spawn N workers spinning on sync()
 -m, --vm N         spawn N workers spinning on malloc()/free()
     --vm-bytes B   malloc B bytes per vm worker (default is 256MB)
     --vm-stride B  touch a byte every B bytes (default is 4096)
     --vm-hang N    sleep N secs before free (default none, 0 is inf)
     --vm-keep      redirty memory instead of freeing and reallocating
 -d, --hdd N        spawn N workers spinning on write()/unlink()
     --hdd-bytes B  write B bytes per hdd worker (default is 1GB)

Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s

Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).

https://blog.csdn.net/yujia_666/article/details/89317310

查看CPU信息

cpu详细信息
# lscpu

cpu个数:
# cat /proc/cpuinfo |grep 'physical id'|sort -u|wc -l
2

cpu核数:
# cat /proc/cpuinfo |grep 'core id'|sort -u|wc -l
24

cpu线程数:
# cat /proc/cpuinfo |grep 'processor'|sort -u|wc -l
96

根据线程测试cpu:
# stress -c 96
stress: info: [7461] dispatching hogs: 96 cpu, 0 io, 0 vm, 0 hdd

再开一个终端使用top查看

以下转载自https://blog.csdn.net/YINHAOXU1/article/details/72868888

CPU:

# vim test-cpu.sh
# chmod +x test-cpu.sh
# cat test-cpu.sh
 while [ 1 ]
  do
    echo 1 >> /dev/null  
  done

或者:

# while [ 1 ];do : ;done

python 转载自https://www.cnblogs.com/guixie/p/11834707.html

# yum -y install python
# vim test_mem.py
# chmod +x test_mem.py
# cat test_mem.py
#! /user/bin/env python
# -*- encoding: utf-8 -*-
import sys
import re
import time
from multiprocessing import Process,cpu_count

def print_help():
    print('Usage: ')
    print('  python test_mem.py m 1GB')
    print('  python test_mem.py c 1')
    print('  python test_mem.py mc 1GB 2')

#实现占用内存
def mem():
    pattern = re.compile('^(\d*)([M|G]B)$')
    size = sys.argv[2].upper()
    match = pattern.match(size)
    if match:
        num = int(match.group(1))
        unit = match.group(2)
        if unit == 'MB':
            s = ' ' * (num * 1024 * 1024)
        else:
            s = ' ' * (num * 1024 * 1024 * 1024)
        time.sleep(24 * 3600)
    else:
        print("bad args.....")
        print_help()

#cpu满载
def deadloop():
    while True:
        pass

#根据传参来指定占满几个核
def cpu():
    arg = sys.argv[2] if len(sys.argv) == 3 else sys.argv[3]
    cpu_num = cpu_count()
    cores = int(arg)
    if not isinstance(cores,int):
        print("bad args not int")
        return

    if cores > cpu_num:
        print("Invalid CPU Num(cpu_count="+str(cpu_num)+")")
        return

    if cores is None or cores <1:
        cores = 1

    for i in range(cores):
        Process(target=deadloop).start()

def mem_cpu():
    Process(target=mem).start()
    Process(target=cpu).start()

if __name__ == "__main__":
    if len(sys.argv) >= 3:
        switcher = {
            'm': mem,
            'c': cpu,
            'mc': mem_cpu
        }
        switcher.get(sys.argv[1], mem)()
    else:
        print_help()

# python test_mem.py
Usage: 
  python test_mem.py m 1GB     #Mem占用1GB,支持使用MB
  python test_mem.py c 1     #CPU 1核满载
  python test_mem.py mc 1GB 2     #CPU 1核满载,并且指定Mem占用2GB

你可能感兴趣的:(linux下的CPU压力测试工具与方法介绍)