Python实现测试磁盘性能的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python实现测试磁盘性能的方法。分享给大家供大家参考。具体如下:

该代码做了如下工作:

create 300000 files (512B to 1536B) with data from /dev/urandom
rewrite 30000 random files and change the size
read 30000 sequential files
read 30000 random files
delete all files
sync and drop cache after every step

bench.py代码如下:

复制代码 代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
filecount = 300000
filesize = 1024
import random, time
from os import system
flush = "sudo su -c 'sync ; echo 3 > /proc/sys/vm/drop_caches'"
randfile = open("/dev/urandom", "r")
print "\ncreate test folder:"
starttime = time.time()
system("rm -rf test && mkdir test")
print time.time() - starttime
system(flush)
print "\ncreate files:"
starttime = time.time()
for i in xrange(filecount):
    rand = randfile.read(int(filesize * 0.5 + filesize * random.random()))
    outfile = open("test/" + unicode(i), "w")
    outfile.write(rand)
print time.time() - starttime
system(flush)
print "\nrewrite files:"
starttime = time.time()
for i in xrange(int(filecount / 10)):
    rand = randfile.read(int(filesize * 0.5 + filesize * random.random()))
    outfile = open("test/" + unicode(int(random.random() * filecount)), "w")
    outfile.write(rand)
print time.time() - starttime
system(flush)
print "\nread linear:"
starttime = time.time()
for i in xrange(int(filecount / 10)):
    infile = open("test/" + unicode(i), "r")
    outfile.write(infile.read());
print time.time() - starttime
system(flush)
print "\nread random:"
starttime = time.time()
outfile = open("/dev/null", "w")
for i in xrange(int(filecount / 10)):
    infile = open("test/" + unicode(int(random.random() * filecount)), "r")
    outfile.write(infile.read());
print time.time() - starttime
system(flush)
print "\ndelete all files:"
starttime = time.time()
system("rm -rf test")
print time.time() - starttime
system(flush)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

通过python扫描二维码/条形码并打印数据

需提前安装好pyzbar和opencv-python库(博主的电脑安装opencv-python库比较麻烦,但大部分都不会出现该问题) 安装方法:打开命令框输入 pip install...

Python常用的日期时间处理方法示例

#-*- coding: utf-8 -*- import datetime #给定日期向后N天的日期 def dateadd_day(days): d1 = datetim...

Python干货:分享Python绘制六种可视化图表

Python干货:分享Python绘制六种可视化图表

可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的。对于初学者来说,很容易被这官网上众多的图表类型给吓着了,由于种类太多,几种图表的...

Python删除windows垃圾文件的方法

本文实例讲述了Python删除windows垃圾文件的方法。分享给大家供大家参考。具体如下: #coding:utf-8 import os #from glob import gl...

Python3 pip3 list 出现 DEPRECATION 警告的解决方法

需要在 ~/.pip/pip.conf 配置文件中加入下面的语句,避免这类警告: 没有目录或没有配置文件需要自己新建 mkdir ~/.pip/ cd ~/.pip touch pip....