Disk speed is the key value for choosing the right VPS provider. The following tutorial is dedicated to this topic.

The most of the VPS providers will try to get you on ultra fast SSD disks but you can make your own test using the PHP code. We chose the combination of PHP and DD just for the better understanding and easier code modification.

What is dd?

“dd” is a simple Unix command primary dedicated to converting and copying the data files. The main advantage is that you can see the stats (time and speed) after the operation is finished.

Algorhitm logic

To achieve the relevant results you need to test the various sizes of blocks. We used 1k, 10k, 100k, 1M, 10M, and 100M blocks for our test. Why? Because the transfer speed depends also on the size of the file. Some providers will give you only the numbers for larger files when the speed is the highest. But in the real traffic the files are usually from 1kB to 20-50kB large. So the speed of reading / writing the 100MB file is quite irrelevant and works only as a marketing feature.

So our program will run not 1 but 7 different speed tests. The best one and the worst one are ignored and the final number is the average value from the remaining 5 tests.


Disk speed test results for MacBook Pro (Retina, 13-inch, Early 2013)

The code

Algorhitm is quite simple. It’s just one foreach command which triggers the tests of each block.

The block size is easy to edit. The main “FOR” which triggers the test 7 times is inside the command. Because the “dd” command cannot give you the direct output the test results are written in dd.log file  so you can read it in php. The other commands are explained in code:

#!/usr/bin/env php
$pattern = '/[ \n]/';
$result = array();	
$blocks = array('1k', '10k', '100k', '1M', '10M', '100M');
// Notice: On OS X you have to use 1m (lowercase) instead of 1M
// $blocks = array('1k', '10k', '100k', '1m', '10m', '100m');

foreach ($blocks as $block) {
	// providing 7 measurements
	for ($n = 1; $n <= 7; $n++) {
		// dd command execute
		shell_exec ( 'dd if=/dev/zero of=test bs='.$block.' count=1 > dd.log 2>&1' );	
		// getting file from dd.log
		$data = file_get_contents("dd.log");
		// exploding result values
		$raw = preg_split( $pattern, $data );
		$raw = array_slice($raw, -10, 10, true);
		// extracting data from result	
		// Check your output positions with var_dump($raw);
		$bytes = floatval($raw[6]); 
		$time = floatval(str_replace(',', '.', $raw[10]) );
		// converting speed to MB/S
		$speed = round(($bytes/$time)/(1024*1024),2);
		$result[] = $speed;
	// sorting array and cutting lowest and highest value
	sort($result); array_shift($result); array_pop($result);
	// calculating speed
	$result = array_sum($result) / count($result);

	// printing result
	echo "$block : $result MB/s \n";
	// cleaning variable for next measurement
	$result = array();

We used this program to test the speed of VPS in this article. The tests were repeated every hour and we wrote it directly into the external MySQL database instead of using the var_dump.

Join the community

Leave a Reply

Our sponsors

We’re social

New in Lab

New Collection

New Tutorial

Knowledge base