The examples in this article describe how Python implements test disk performance. Share to everyone for your reference. Specific as follows:
The code does the following work:
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
The bench.py code is as follows:
Copy the Code code 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)
Hopefully this article will help you with Python programming.