当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » Python  » 常用工具方法
分享到: 
收藏 +0
2
该工具做了如下工作:

    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
标签: <无>

代码片段(1) [全屏查看所有代码]

1. [文件] bench.py ~ 1KB     下载(13)     跳至 [1] [全屏预览]

#!/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)



开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(1)

  • 1楼:yak 发表于 2014-11-21 10:47 回复此评论
    create test folder:
    0.00339078903198

    create files:
    40.9885668755

    rewrite files:
    227.004573107

    read linear:
    21.4157478809

    read random:
    213.561552048

    delete all files:
    4.65168786049

    这个磁盘性能怎么样?
开源从代码分享开始 分享代码