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


Posted in Python onMarch 12, 2015

本文实例讲述了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 相关文章推荐
使用python开发vim插件及心得分享
Nov 04 Python
使用PDB模式调试Python程序介绍
Apr 05 Python
简单上手Python中装饰器的使用
Jul 12 Python
详解Python下载图片并保存本地的两种方式
May 15 Python
使用pyqt 实现重复打开多个相同界面
Dec 13 Python
如何基于python操作json文件获取内容
Dec 24 Python
关于多元线性回归分析——Python&SPSS
Feb 24 Python
基于python连接oracle导并出数据文件
Apr 28 Python
pygame用blit()实现动画效果的示例代码
May 28 Python
如何在Python对Excel进行读取
Jun 04 Python
python如何建立全零数组
Jul 19 Python
python代码实现猜拳小游戏
Nov 30 Python
Python解析xml中dom元素的方法
Mar 12 #Python
Python实现Tab自动补全和历史命令管理的方法
Mar 12 #Python
Python实现将n个点均匀地分布在球面上的方法
Mar 12 #Python
Python求解平方根的方法
Mar 11 #Python
python自动格式化json文件的方法
Mar 11 #Python
python处理csv数据的方法
Mar 11 #Python
python模拟鼠标拖动操作的方法
Mar 11 #Python
You might like
桌面中心(三)修改数据库
2006/10/09 PHP
mysql时区问题
2008/03/26 PHP
php 破解防盗链图片函数
2008/12/09 PHP
php在服务器执行exec命令失败的解决方法
2012/03/03 PHP
解决phpmyadmin中缺少mysqli扩展问题的方法
2013/05/06 PHP
php中mysql操作buffer用法详解
2015/03/19 PHP
javascript 日期时间函数(经典+完善+实用)
2009/05/27 Javascript
使用Javascript接收get传递的值的代码
2011/11/30 Javascript
无闪烁更新网页内容JS实现
2013/12/19 Javascript
javascript函数作用域学习示例(js作用域)
2014/01/13 Javascript
window resize和scroll事件的基本优化思路
2014/04/29 Javascript
JS获得图片alt信息的方法
2015/04/01 Javascript
JQuery中attr方法和removeAttr方法用法实例
2015/05/18 Javascript
javascript中递归函数用法注意点
2015/07/30 Javascript
javascript this详细介绍
2016/09/19 Javascript
详解JavaScript跨域总结与解决办法
2016/10/31 Javascript
[52:06]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
python引入导入自定义模块和外部文件的实例
2017/07/24 Python
Python网络编程之TCP与UDP协议套接字用法示例
2018/02/02 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
2018/03/19 Python
python利用wx实现界面按钮和按钮监听和字体改变的方法
2019/07/17 Python
python将YUV420P文件转PNG图片格式的两种方法
2021/01/22 Python
使用CSS3制作一个简单的进度条(demo)
2017/05/23 HTML / CSS
canvas与html5实现视频截图功能示例
2016/12/15 HTML / CSS
英国票务网站:Ticketmaster英国
2018/08/27 全球购物
自主招生自荐书
2013/11/29 职场文书
大学活动策划书范文
2014/01/10 职场文书
幼儿园教研活动方案
2014/01/19 职场文书
知识竞赛活动方案
2014/02/18 职场文书
项目经理任命书范本
2014/06/05 职场文书
2014年施工员工作总结
2014/11/18 职场文书
2014年小学教导处工作总结
2014/12/19 职场文书
巾帼标兵事迹材料
2014/12/26 职场文书
画展邀请函
2015/01/31 职场文书
2016师德师风学习心得体会
2016/01/12 职场文书
CSS实现单选折叠菜单功能
2021/11/01 HTML / CSS