python性能测试方法_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程序設計有所幫助。
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!
相關文章
相關視頻
網友評論
文明上網理性發言,請遵守 新聞評論服務協議我要評論
立即提交
專題推薦獨孤九賤-php全棧開發教程
全棧 100W+
主講:Peter-Zhu 輕松幽默、簡短易學,非常適合PHP學習入門
玉女心經-web前端開發教程
入門 50W+
主講:滅絕師太 由淺入深、明快簡潔,非常適合前端學習入門
天龍八部-實戰開發教程
實戰 80W+
主講:西門大官人 思路清晰、嚴謹規范,適合有一定web編程基礎學習
總結
以上是生活随笔為你收集整理的python性能测试方法_Python实现测试磁盘性能的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 事务 数据库 事务_Java数
- 下一篇: c mysql 内存泄露_c代码连接my