首页 > 系统相关 >[转]linux下的CPU压力测试工具与方法介绍

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

时间:2023-02-13 09:45:13浏览次数:64  
标签:-- print CPU mem test linux 测试工具 python cpu

原文地址:linux下的CPU压力测试工具与方法介绍 - 简书 (jianshu.com)

使用工具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


作者:陈超同学
链接:https://www.jianshu.com/p/118d5995ccdb
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:--,print,CPU,mem,test,linux,测试工具,python,cpu
From: https://www.cnblogs.com/dirgo/p/17115337.html

相关文章

  • Linux编程获取本机IP地址 + socket相关系统调用的调用流程
    1 ifaddrs结构体定义如下C代码  1structifaddrs2{3structifaddrs*ifa_next;/*Nextiteminlist*/4char......
  • linux 下 dynv6 更新 ddns 域名对应ip
    基本原理:1.通过某些网站提供的api返回公网ip2.与本地文件记录的上次ip对照,如果相同,结束;如果不同使用 dynv6提供的api进行更新3.将上述逻辑写成脚本,利用......
  • https//mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/linux-64'
    CondaHTTPError:HTTP000CONNECTIONFAILEDforurl<https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/linux-64/current_repodata.json> Elap......
  • Linux命令篇 - tar 命令
    tarGNU`tar'savesmanyfilestogetherintoasingletapeordiskarchive,andcanrestoreindividualfilesfromthearchive.tar:用于压缩和解压缩文件;格式:tar......
  • Linux入门
    Linux目录结构Linux的目录结构是一个树形结构Windows系统可以有很多盘符,如C盘、D盘、E盘Linux没有盘符这个概念,只有一个根目录/,所有文件都在它下面。在Linux操作系统......
  • MAC 安装docker,下载镜像并压缩转移到LINUX服务器
    MAC安装docker前提-已经安装homebrew(如果未安装,可以打开终端,输入以下命令安装Homebrew)/usr/bin/ruby-e"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/i......
  • linux007之文件、目录操作命令
    查看当前所在目录:pwd:查看当前所在目录路径ls:查看当前目录所有文件ls路径:查看指定目录下所有文件ls-l:以列的形式查看当前目录所有文件ls-l路......
  • Linux入门
    Linux目录结构Linux的目录结构是一个树形结构Windows系统可以有很多盘符,如C盘、D盘、E盘Linux没有盘符这个概念,只有一个根目录/,所有文件都在它下面。在Linux操作系统中......
  • Linux内核机制—smp_hotplug_thread
    基于Linux-5.10一、简介1.只是一个创建per-cpu线程执行用户提供的回调的机制。2.内核中已存在的注册staticstructsmp_hotplug_threadidle_inject_threads={//......
  • linux 基础(8)例行任务
    我们的linux系统,有时会自动进行线上更新,会定时升级locate用到的数据库。用户也会“在每天0点备份数据”或者“每天8点分析登录文件”,管理这些例行任务就叫做“工作调度”......