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 Image模块基本图像处理操作小结

本文实例讲述了Python Image模块基本图像处理操作。分享给大家供大家参考,具体如下: Python 里面最常用的图像操作库是Image library(PIL),功能上,虽然还不...

Django中ORM表的创建和增删改查方法示例

Django中ORM表的创建和增删改查方法示例

前言 Django作为重量级的Python web框架,在做项目时肯定少不了与数据库打交道,编程人员对数据库的语法简单的还行,但过多的数据库语句不是编程人员的重点对象。因此用ORM来操作...

python程序控制NAO机器人行走

最近重新学习nao的官方文档,写点简单的程序回顾一下。主要是用python调用api,写下来保存着。 '''Walk:small example to make nao walk''...

详解Python之数据序列化(json、pickle、shelve)

一、前言 1. 现实需求 每种编程语言都有各自的数据类型,其中面向对象的编程语言还允许开发者自定义数据类型(如:自定义类),Python也是一样。很多时候我们会有这样的需求: 把内...

python多线程同步之文件读写控制

python多线程同步之文件读写控制

本文实例为大家分享了python多线程同步之文件读写控制的具体代码,供大家参考,具体内容如下 1、实现文件读写的文件ltz_schedule_times.py #! /usr/bin...