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的dict,set,list,tuple应用详解
Jul 24 Python
Python实现的数据结构与算法之快速排序详解
Apr 22 Python
Python自动化运维和部署项目工具Fabric使用实例
Sep 18 Python
浅谈Python类的__getitem__和__setitem__特殊方法
Dec 25 Python
python使用openpyxl库修改excel表格数据方法
May 03 Python
Python读写docx文件的方法
May 08 Python
Python删除n行后的其他行方法
Jan 28 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
Aug 28 Python
Python实现元素等待代码实例
Nov 11 Python
pytorch方法测试——激活函数(ReLU)详解
Jan 15 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 Python
Python实现打乒乓小游戏
Sep 25 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
PHP5.2中date()函数显示时间与北京时间相差8小时的解决办法
2009/05/28 PHP
PHP初学者常见问题集合 修正版(21问答)
2010/03/23 PHP
PHP编写RESTful接口
2016/02/23 PHP
php pdo oracle中文乱码的快速解决方法
2016/05/16 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
Yii框架实现对数据库的CURD操作示例
2019/09/03 PHP
JavaScript DOM 添加事件
2009/02/14 Javascript
jQuery-onload让第一次页面加载时图片是淡入方式显示
2012/05/23 Javascript
js复制网页内容并兼容各主流浏览器的代码
2013/12/17 Javascript
jQuery操作CheckBox的方法介绍(选中,取消,取值)
2014/02/04 Javascript
清除div下面的所有标签的方法
2014/02/17 Javascript
纯JS实现根据CSS的class选择DOM
2014/03/22 Javascript
用js一次改变多个input的readonly属性值的方法
2014/06/11 Javascript
JS运动基础框架实例分析
2015/03/03 Javascript
JS中prototype的用法实例分析
2015/03/19 Javascript
jquery实现简单的无缝滚动
2015/04/15 Javascript
noty ? jQuery通知插件全面解析
2016/05/18 Javascript
微信小程序 倒计时组件实现代码
2016/10/24 Javascript
Bootstrap 网格系统布局详解
2017/03/19 Javascript
利用JQUERY实现多个AJAX请求等待的实例
2017/12/14 jQuery
详解Vuex下Store的模块化拆分实践
2019/07/31 Javascript
使用easyui从servlet传递json数据到前端页面的两种方法
2019/09/05 Javascript
python中实现将多个print输出合成一个数组
2018/04/19 Python
解决pycharm安装后代码区不能编辑的问题
2018/10/28 Python
python文本数据处理学习笔记详解
2019/06/17 Python
Python 控制终端输出文字的实例
2019/07/12 Python
详解一种用django_cache实现分布式锁的方式
2019/09/01 Python
欧尚俄罗斯网上超市:Auchan俄罗斯
2018/05/03 全球购物
介绍一下SQL中union,intersect和minus
2012/04/05 面试题
调解员先进事迹材料
2014/02/07 职场文书
三年级学生期末评语
2014/12/26 职场文书
五年级作文之劳动作文
2019/11/12 职场文书
pytorch 中autograd.grad()函数的用法说明
2021/05/12 Python
用python基于appium模块开发一个自动收取能量的小助手
2021/09/25 Python
Spring中的使用@Async异步调用方法
2021/11/01 Java/Android
python数据分析之单因素分析线性拟合及地理编码
2022/06/25 Python