Python實現(xiàn)測試磁盤性能的方法
本文實例講述了Python實現(xiàn)測試磁盤性能的方法。分享給大家供大家參考。具體如下:
該代碼做了如下工作:
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代碼如下:
# -*- 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 PyQt5實現(xiàn)拖拽與剪貼板功能詳解
這篇文章主要為大家詳細介紹了Python PyQt5如何實現(xiàn)拖拽與剪貼板功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-12-12django restframework serializer 增加自定義字段操作
這篇文章主要介紹了django restframework serializer 增加自定義字段操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07解決pandas無法在pycharm中使用plot()方法顯示圖像的問題
今天小編就為大家分享一篇解決pandas無法在pycharm中使用plot()方法顯示圖像的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python函數(shù)基礎實例詳解【函數(shù)嵌套,命名空間,函數(shù)對象,閉包函數(shù)等】
這篇文章主要介紹了Python函數(shù)基礎,結合實例形式詳細分析了函數(shù)嵌套,命名空間,函數(shù)對象,閉包函數(shù)等相關概念、原理、用法及操作注意事項,需要的朋友可以參考下2019-03-03使用Python編寫類UNIX系統(tǒng)的命令行工具的教程
這篇文章主要介紹了使用Python編寫類UNIX系統(tǒng)的命令行工具的教程,本文來自于IBM官方網(wǎng)站技術文檔,需要的朋友可以參考下2015-04-04