===================================================================
代码
-
这个脚本功能如下【这个脚本不难,代码就不做解释了】
-
1、想占用多少内存【单位是M(不能超过现有内存,否则可能会蹦)】
-
2、占用多长时间【单位是秒】
-
3、占用时间到达以后,会自动释放被占用的内存【如果提前结束,需要手动删除占用文件】
[root@centos-76-qcow2-50g-3 ~]# free -g
total used free shared buff/cache available
Mem: 62 0 62 0 0 61
Swap: 0 0 0
[root@centos-76-qcow2-50g-3 ~]#
[root@centos-76-qcow2-50g-3 ~]# cat free.sh
#!/bin/bash
if [ $# != 2 ]; then
echo “USAGE: $0 <free,sleep time>”
exit 1;
fi
free -m > /tmp/freee
cat /tmp/freee
mkdir /tmp/memory
mount -t tmpfs -o size=$1M tmpfs /tmp/memory
dd if=/dev/zero of=/tmp/memory/block
free -m > /tmp/freee
cat /tmp/freee
echo “If executed ctrl+C,Please execute the following lines manually”
echo “execute: rm -rf /tmp/memory/block”
echo “execute: umount /tmp/memory”
echo “execute: rmdir /tmp/memory”
echo “Please wait $2 seconds”
sleep $2
rm -rf /tmp/memory/block
umount /tmp/memory
rmdir /tmp/memory
[root@centos-76-qcow2-50g-3 ~]#
测试
-
执行:
sh free.sh 2048 120
【2048是内存数量,单位是M,120是时间,单位是秒】 -
因为我这台虚机有64,所以我定义的是20480,占用20G
如果参数不够 会报错的
[root@centos-76-qcow2-50g-3 ~]# sh free.sh 1024
USAGE: free.sh <free,sleep time>
[root@centos-76-qcow2-50g-3 ~]#
执行以后可以就会出现下面内容【此时该脚本并不会结束,而是处于 sleep 120状态
[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120
total used free shared buff/cache available
Mem: 64264 451 63559 16 253 63300
Swap: 0 0 0
dd: writing to ‘/tmp/memory/block’: No space left on device
41943041+0 records in
41943040+0 records out
21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s
total used free shared buff/cache available
Mem: 64264 451 43033 20496 20779 42797
Swap: 0 0 0
If executed ctrl+C,Please execute the following lines manually
execute: rm -rf /tmp/memory/block
execute: umount /tmp/memory
execute: rmdir /tmp/memory
Please wait 120 seconds
- 这个占用的是 shared共享内存,可以看到数量已经有变化了,并且free可用内存也相应减少了
- 等待120秒以后呢,该脚本运行结束,并且占用的内存也会被释放
[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120
total used free shared buff/cache available
Mem: 64264 451 63559 16 253 63300
Swap: 0 0 0
dd: writing to ‘/tmp/memory/block’: No space left on device
41943041+0 records in
41943040+0 records out
21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s
total used free shared buff/cache available
Mem: 64264 451 43033 20496 20779 42797
Swap: 0 0 0
If executed ctrl+C,Please execute the following lines manually
execute: rm -rf /tmp/memory/block
execute: umount /tmp/memory
execute: rmdir /tmp/memory
Please wait 120 seconds
[root@centos-76-qcow2-50g-3 ~]#
[root@centos-76-qcow2-50g-3 ~]# free -m
total used free shared buff/cache available
Mem: 64264 451 63559 16 253 63300
Swap: 0 0 0
[root@centos-76-qcow2-50g-3 ~]#
rpm包下载安装
- 这个rpm包下载地址:内存占用rpm包.rar
下载好以后上传到linux主机上,然后通过下面方法安装好。
[root@ccx ~]# ls /opt
memload-7.0-1.r29766.x86_64.rpm
[root@ccx ~]# rpm -ivh /opt/memload-7.0-1.r29766.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:memload-7.0-1.r29766 ################################# [100%]
[root@ccx ~]#
占用测试
- 基本环境弄好了,我们现在开始消耗内存看看【占用内存不要超过总内存】
我下面是在多个窗口中,注意看主机名 的变化
可以看到node2现在消耗了1G内存
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 1 28 0 1 29
Swap: 0 0 0
[root@ccx ~]#
#现在回到容器内部开始占用内容,因为我这虚机是32G内存,所以我占用多一点吧
#容器中占用了10G
[root@ccx ~]# memload 10240
Attempting to allocate 10240 Mebibytes of resident memory…
回到node2,可以看到10G确实可以被占用的
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 1 28 0 1 29
Swap: 0 0 0
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 4 24 0 1 25
Swap: 0 0 0
[root@ccx ~]#
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 7 22 0 1 23
Swap: 0 0 0
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 8 21 0 1 22
Swap: 0 0 0
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 11 18 0 1 19
Swap: 0 0 0
[root@ccx ~]#
释放
- 上面呢是可以正常释放的,现在我们释放掉占用的这10G
#容器ctrl+c即可释放
[root@ccx ~]# memload 10240
Attempting to allocate 10240 Mebibytes of resident memory…
Allocated 10000 pages
^C
[root@ccx ~]#
回到node2看是否已经被释放,然后再释放下缓存内存
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 1 28 0 1 29
Swap: 0 0 0
[root@ccx ~]# echo 3 > /proc/sys/vm/drop_caches
[root@ccx ~]#
[root@ccx ~]# free -g
total used free shared buff/cache available
Mem: 31 1 29 0 0 29
Swap: 0 0 0
[root@ccx ~]#
=====================================================================
-
这是一个是用来获取ATA/IDE硬盘的参数的命令,是由早期Linux IDE驱动的开发和维护人员 Mark Lord开发编写的( hdparm has been written by Mark Lord mlord@pobox.com, the primary developer and maintainer of the (E)IDE driver for Linux, with suggestions from many netfolk).该命令应该也是仅用于Linux系统,对于UNIX系统,ATA/IDE硬盘用的可能比较少,一般大型的系统都是使用磁盘阵列的.
-
使用方法很简单
/dev/sda3 是设备名称
[root@centos-76-qcow2-50g-3 ~]# hdparm -Tt /dev/sda3
/dev/sda3:
Timing cached reads: 13034 MB in 2.00 seconds = 6527.09 MB/sec
Timing buffered disk reads: 194 MB in 5.60 seconds = 34.64 MB/sec
[root@centos-76-qcow2-50g-3 ~]#
可以看到,2秒钟读取了13034MB的缓存,约合6527.09 MB/sec;
在3.11秒中读取了194MB磁盘(物理读),读取速度约合4.64 MB/sec
-
这不是一个专业的测试工具,不过如果对于测试结果的要求不是很苛刻的话,平时可以使用来对磁盘的读写速度作一个简单的评估.另外由于这是一个免费软件,基本上×NIX系统上都有安装。
-
dd命令可以通用,但不够专业,也没有考虑到缓存和物理读的区分,测试的数据也是仅作参考,不能算是权威。
-
首先了解两个特殊设备
-
/dev/null
伪设备,回收站.写该文件不会产生IO -
/dev/zero
伪设备,会产生空字符流,对它不会产生IO
硬盘IO写速度测试
- 测试逻辑速度【结果较快】
表示 每次写入8k的数据,执行300000次
[root@centos-73-iso-100g-test data_vdb1]# time dd if=/dev/zero of=test.dbf bs=8k count=300000
300000+0 records in
300000+0 records out
2457600000 bytes (2.5 GB) copied, 1.46149 s, 1.7 GB/s
real 0m1.464s
user 0m0.027s
sys 0m1.436s
[root@centos-73-iso-100g-test data_vdb1]#
- 测试真实的IO速度,需要在后面加上参数
oflag=direct
【这个过程较慢】
[root@centos-73-iso-100g-test data_vdb1]# time dd if=/dev/zero of=test.dbf bs=8k count=300000 oflag=direct
300000+0 records in
300000+0 records out
2457600000 bytes (2.5 GB) copied, 423.33 s, 5.8 MB/s
real 7m3.561s
user 0m0.992s
sys 0m28.849s
[root@centos-73-iso-100g-test data_vdb1]#
- 注意,上面操作会在当前路径留下一个test文件,记得删除啊
[root@centos-73-iso-100g-test data_vdb1]# du -sh * | tail -n1
2.3G test.dbf
[root@centos-73-iso-100g-test data_vdb1]# rm -rf test.dbf
[root@centos-73-iso-100g-test data_vdb1]#
硬盘IO读速度测试
-
- 测试逻辑速度【结果较快】
表示 每次读取8k的数据,执行300000次
[root@centos-76-qcow2-50g-3 ~]# dd if=test.dbf bs=8k count=300000 of=/dev/null
300000+0 records in
300000+0 records out
2457600000 bytes (2.5 GB) copied, 5.97231 s, 411 MB/s
[root@centos-76-qcow2-50g-3 ~]#
- 这个没法测试真实的IO速度,会报错
[root@centos-76-qcow2-50g-3 ~]# dd if=test.dbf bs=8k count=300000 of=/dev/null oflag=direct
dd: failed to open ‘/dev/null’: Invalid argument
[root@centos-76-qcow2-50g-3 ~]#
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
count=300000 of=/dev/null
300000+0 records in
300000+0 records out
2457600000 bytes (2.5 GB) copied, 5.97231 s, 411 MB/s
[root@centos-76-qcow2-50g-3 ~]#
- 这个没法测试真实的IO速度,会报错
[root@centos-76-qcow2-50g-3 ~]# dd if=test.dbf bs=8k count=300000 of=/dev/null oflag=direct
dd: failed to open ‘/dev/null’: Invalid argument
[root@centos-76-qcow2-50g-3 ~]#
[外链图片转存中…(img-PexQN6H9-1726121547567)]
[外链图片转存中…(img-ISOg1FAc-1726121547568)]
[外链图片转存中…(img-VQ0Br1rg-1726121547568)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!