How Python implements Test disk performance

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.