How to get read/write disk speed in Python?
<p>In a Pyhton program I need to get the accumulated read/write speeds of all disks on the host. I was doing it with <code>subprocess.check_output()</code> to call the following Linux shell command:</p>

<pre><code>$ sudo hdparm -t /dev/sda
</code></pre>

<p>This gives as a result:</p>

<pre><code>/dev/sda:
 Timing buffered disk reads: 1488 MB in  3.00 seconds = 495.55 MB/sec
</code></pre>

<p>then I can parse the 495.55. OK, so far so good.</p>

<p>But on the man page of <code>hdparm</code> i found this explanation for the <code>-t</code> flag that basically says that when performing measurements no other process should read/write to disk at same time:</p>

<blockquote>
  <p>Perform  timings  of  device  reads  for benchmark and comparison purposes. For meaningful results, this operation should be repeated 2-3 times on an otherwise inactive system (no other active processes) with at least a couple of megabytes of free memory.  This displays the speed of reading through the buffer cache to the disk without any prior caching  of  data. This measurement  is  an indication of how fast the drive can sustain sequential data reads under Linux, without any filesystem overhead. To ensure accurate measurements, the buffer cache is flushed during the processing of -t using the BLKFLSBUF ioctl.</p>
</blockquote>

<p><strong>The question is</strong>:</p>

<p>How can I ensure that no other process is accessing disk at the same time when measurements are performed?</p>

<p>According to <a href="http://unix.stackexchange.com/questions/55212/how-can-i-monitor-disk-io">http://unix.stackexchange.com/questions/55212/how-can-i-monitor-disk-io</a> the most usable solution includes the tool sysstat or iostat (same package).</p>

<p>But seriously, since you have sudo permissions on the host, you can check yourself whether any IO intensive tasks are going on using any of the popular system monitoring tools. You cannot kill all IO effectively without your measurements also going nuts. Over a longer time the measurements should give you reasonable results nonetheless, since the deviations converge towards stable background noise.</p>

<p>Aside from that what would you need artificial measurements for? If you simply want to test the hardware capabilities without any RL context, <strong>do not mount the disk</strong> and test it in binary mode. A measurement while real traffic is going on usually gives you results that are closer to what you can actually expect at load times.</p>

python linux bash performance