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 相关文章推荐
flask中使用SQLAlchemy进行辅助开发的代码
Feb 10 Python
wxpython学习笔记(推荐查看)
Jun 09 Python
python 实现红包随机生成算法的简单实例
Jan 04 Python
Python把csv数据写入list和字典类型的变量脚本方法
Jun 15 Python
Selenium的使用详解
Oct 19 Python
在Python中使用Neo4j的方法
Mar 14 Python
TensorBoard 计算图的查看方式
Feb 15 Python
使用pth文件添加Python环境变量方式
May 26 Python
python os模块在系统管理中的应用
Jun 22 Python
python进度条显示之tqmd模块
Aug 22 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
Dec 14 Python
用python 绘制茎叶图和复合饼图
Feb 26 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
PHP 解决utf-8和gb2312编码转换问题
2010/03/18 PHP
Yii2框架使用计划任务的方法
2016/05/25 PHP
JQuery入门—JQuery程序的代码风格详细介绍
2013/01/03 Javascript
基于JavaScript实现继承机制之调用call()与apply()的方法详解
2013/05/07 Javascript
js css后面所带参数含义介绍
2013/08/18 Javascript
jquery实现类似淘宝星星评分功能有截图
2014/09/15 Javascript
常见的jQuery选择器汇总
2014/11/24 Javascript
javascript跨域总结之window.name实现的跨域数据传输
2015/11/01 Javascript
js添加事件的通用方法推荐
2016/05/15 Javascript
jquery实现拖动效果
2016/08/10 Javascript
基于JavaScript实现弹幕特效
2020/08/27 Javascript
基于vue-cli创建的项目的目录结构及说明介绍
2017/11/23 Javascript
Vue与Node.js通过socket.io通信的示例代码
2018/07/25 Javascript
nodejs通过钉钉群机器人推送消息的实现代码
2019/05/05 NodeJs
vue-cli随机生成port源码的方法
2019/09/02 Javascript
vue quill editor 使用富文本添加上传音频功能
2020/01/14 Javascript
[01:06]DOTA2小知识课堂 Ep.02 吹风竟可解梦境缠绕
2019/12/05 DOTA
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
python中正则表达式的使用方法
2018/02/25 Python
python实现excel读写数据
2021/03/02 Python
python解析yaml文件过程详解
2019/08/30 Python
8种常用的Python工具
2020/08/05 Python
Python urllib库如何添加headers过程解析
2020/10/05 Python
HTML5 device access 设备访问详解
2018/05/24 HTML / CSS
英国最大的独立家具零售商:Furniture Village
2016/09/06 全球购物
C和C++经典笔试题附答案解析
2014/08/18 面试题
程序员岗位职责
2013/11/11 职场文书
一份报关员的职业规划范文
2014/01/08 职场文书
冰淇淋开店创业计划书
2014/02/01 职场文书
会计学自我鉴定
2014/02/06 职场文书
信息专业毕业生五年职业规划参考
2014/02/06 职场文书
运动会口号大全
2014/06/07 职场文书
先进班组材料范文
2014/12/25 职场文书
五年级学生期末评语
2014/12/26 职场文书
MySQL5.7并行复制原理及实现
2021/06/03 MySQL
Vue2项目中对百度地图的封装使用详解
2022/06/16 Vue.js