This article describes how to test disk performance using Python. it involves Python's hardware-related operating skills and has some reference value, for more information about how to test disk performance using Python, see the following example. Share it with you for your reference. The details are as follows:
The code does the following:
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
Pipeline. py code is as follows:
The code is as follows:
#! /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)
I hope this article will help you with Python programming.