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

作者:鉴客 时间:2022-01-31 19:00:46 

本文实例讲述了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,测试,磁盘,性能
0
投稿

猜你喜欢

  • 如何利用pytesseract识别图片中的数字

    2023-07-11 12:48:36
  • Python seek()和tell()函数的具体使用

    2023-12-07 20:59:39
  • 以SQLite和PySqlite为例来学习Python DB API

    2023-07-13 02:19:14
  • Python设计模式创建型原型模式

    2023-07-21 21:19:25
  • sql语句查询数据库中的表名/列名/主键/自动增长值实例

    2012-07-11 15:28:58
  • python 利用百度API进行淘宝评论关键词提取

    2021-11-14 19:32:36
  • PHP常用字符串函数小结(推荐)

    2023-06-14 00:18:50
  • jQuery方法扩展:type, toJSON, evalJSON

    2009-02-15 12:42:00
  • 5个有趣的浏览器地址栏Javascript代码

    2008-07-21 13:04:00
  • springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项

    2023-09-04 09:17:26
  • 随Linux开机自动启动mysql

    2009-12-29 10:14:00
  • 使用cookie和application实现在线人数统计

    2007-09-18 13:01:00
  • 从XML中读取数据到内存的实例

    2008-09-04 14:43:00
  • 加密处理使密码更安全[CFS编码加密]

    2008-03-19 13:30:00
  • 官方是这样定义 DOCTYPE HTML PUBLIC 的

    2007-05-31 09:43:00
  • 建立适当的索引是实现查询优化的首要前提

    2009-01-19 13:11:00
  • 胜过语言的图形符号

    2009-05-06 12:43:00
  • 带你轻松接触MySQL数据库的出错代码列表

    2008-12-31 15:06:00
  • PHP判断是否微信访问的方法示例

    2023-07-05 03:21:36
  • Python linecache.getline()读取文件中特定一行的脚本

    2023-03-09 13:18:05
  • asp之家 网络编程 m.aspxhome.com