Skip to content

Instantly share code, notes, and snippets.

@danieltroger
Created May 3, 2017 20:41
Show Gist options
  • Save danieltroger/5598a8426c52b99143a41bc81ea44e3e to your computer and use it in GitHub Desktop.
Save danieltroger/5598a8426c52b99143a41bc81ea44e3e to your computer and use it in GitHub Desktop.
Best disk speed test ever (going to become)
<?php
/*
sample output:
(Bad?)RAM disk:
```
Mac-mini:RAM Disk daniel$ php /Users/daniel/Documents/diskspeed.php
Wrote 128 MiB with a sample size of 1 KiB (131072 samples) in 0.44374680519104: 288.45 MiB/s
Wrote 128 MiB with a sample size of 16 KiB (8192 samples) in 0.12541198730469: 1020.64 MiB/s
Wrote 128 MiB with a sample size of 32 KiB (4096 samples) in 0.12560701370239: 1019.05 MiB/s
Wrote 128 MiB with a sample size of 64 KiB (2048 samples) in 0.12708592414856: 1007.19 MiB/s
Wrote 128 MiB with a sample size of 128 KiB (1024 samples) in 0.1308810710907: 977.99 MiB/s
Wrote 128 MiB with a sample size of 512 KiB (256 samples) in 0.13247895240784: 966.19 MiB/s
Wrote 128 MiB with a sample size of 1 MiB (128 samples) in 0.12832689285278: 997.45 MiB/s
Wrote 128 MiB with a sample size of 2 MiB (64 samples) in 0.13482904434204: 949.35 MiB/s
Wrote 128 MiB with a sample size of 4 MiB (32 samples) in 0.13064312934875: 979.77 MiB/s
Wrote 128 MiB with a sample size of 8 MiB (16 samples) in 0.14561820030212: 879.01 MiB/s
Wrote 128 MiB with a sample size of 12 MiB (10.666666666667 samples) in 0.12908315658569: 991.61 MiB/s
Wrote 128 MiB with a sample size of 16 MiB (8 samples) in 0.12106609344482: 1.03 GiB/s
Wrote 128 MiB with a sample size of 32 MiB (4 samples) in 0.12903690338135: 991.96 MiB/s
Wrote 128 MiB with a sample size of 64 MiB (2 samples) in 0.12844586372375: 996.53 MiB/s
Optimum sample size for writing is 16 MiB/s
Wrote 1 GiB (64 samples) in 1.0943610668182: 935.71 MiB/s
```
SSD:
```
Mac-mini:tmp daniel$ php /Users/daniel/Documents/diskspeed.php
Wrote 128 MiB with a sample size of 1 KiB (131072 samples) in 0.479975938797: 266.68 MiB/s
Wrote 128 MiB with a sample size of 16 KiB (8192 samples) in 0.29482007026672: 434.16 MiB/s
Wrote 128 MiB with a sample size of 32 KiB (4096 samples) in 0.29876589775085: 428.43 MiB/s
Wrote 128 MiB with a sample size of 64 KiB (2048 samples) in 0.29770278930664: 429.96 MiB/s
Wrote 128 MiB with a sample size of 128 KiB (1024 samples) in 0.29816603660583: 429.29 MiB/s
Wrote 128 MiB with a sample size of 512 KiB (256 samples) in 0.29601311683655: 432.41 MiB/s
Wrote 128 MiB with a sample size of 1 MiB (128 samples) in 0.30912804603577: 414.07 MiB/s
Wrote 128 MiB with a sample size of 2 MiB (64 samples) in 0.31252312660217: 409.57 MiB/s
Wrote 128 MiB with a sample size of 4 MiB (32 samples) in 0.29923391342163: 427.76 MiB/s
Wrote 128 MiB with a sample size of 8 MiB (16 samples) in 0.30197191238403: 423.88 MiB/s
Wrote 128 MiB with a sample size of 12 MiB (10.666666666667 samples) in 0.31166005134583: 410.7 MiB/s
Wrote 128 MiB with a sample size of 16 MiB (8 samples) in 0.29813694953918: 429.33 MiB/s
Wrote 128 MiB with a sample size of 32 MiB (4 samples) in 0.29508996009827: 433.77 MiB/s
Wrote 128 MiB with a sample size of 64 MiB (2 samples) in 0.3101909160614: 412.65 MiB/s
Optimum sample size for writing is 16 KiB/s
Wrote 1 GiB (65536 samples) in 2.5904200077057: 395.3 MiB/s
```
HDD:
```
Mac-mini:Pictures daniel$ php /Users/daniel/Documents/diskspeed.php
Wrote 128 MiB with a sample size of 1 KiB (131072 samples) in 1.0661761760712: 120.06 MiB/s
Wrote 128 MiB with a sample size of 16 KiB (8192 samples) in 0.9434609413147: 135.67 MiB/s
Wrote 128 MiB with a sample size of 32 KiB (4096 samples) in 1.0383710861206: 123.27 MiB/s
Wrote 128 MiB with a sample size of 64 KiB (2048 samples) in 0.95692801475525: 133.76 MiB/s
Wrote 128 MiB with a sample size of 128 KiB (1024 samples) in 1.022588968277: 125.17 MiB/s
Wrote 128 MiB with a sample size of 512 KiB (256 samples) in 0.9723789691925: 131.64 MiB/s
Wrote 128 MiB with a sample size of 1 MiB (128 samples) in 1.0385310649872: 123.25 MiB/s
Wrote 128 MiB with a sample size of 2 MiB (64 samples) in 0.95994400978088: 133.34 MiB/s
Wrote 128 MiB with a sample size of 4 MiB (32 samples) in 0.96675109863281: 132.4 MiB/s
Wrote 128 MiB with a sample size of 8 MiB (16 samples) in 0.93930196762085: 136.27 MiB/s
Wrote 128 MiB with a sample size of 12 MiB (10.666666666667 samples) in 1.0033588409424: 127.57 MiB/s
Wrote 128 MiB with a sample size of 16 MiB (8 samples) in 0.96458005905151: 132.7 MiB/s
Wrote 128 MiB with a sample size of 32 MiB (4 samples) in 0.94208598136902: 135.87 MiB/s
Wrote 128 MiB with a sample size of 64 MiB (2 samples) in 0.96747994422913: 132.3 MiB/s
Optimum sample size for writing is 8 MiB/s
Wrote 1 GiB (128 samples) in 8.5190529823303: 120.2 MiB/s
```
*/
ini_set("memory_limit","1G");
$fssize = 1024*1024*128;
$tsize = 1024*1024*1024;
$f = fopen("speedtest.data","w+");
$bss = Array(1024/*KiB*/,
1024*16/*16KiB*/,
1024*32/*32KiB*/,
1024*64/*64KiB*/,
1024*128/*128KiB*/,
1024*512/*512KiB*/,
1024*1024/*1MiB*/,
1024*1024*2/*2MiB*/,
1024*1024*4/*4MiB*/,
1024*1024*8/*8MiB*/,
1024*1024*12/*12MiB*/,
1024*1024*16/*16MiB*/,
1024*1024*32/*32MiB*/,
1024*1024*64/*64MiB*/);
$speeds = array();
$fastest = 0;
foreach($bss as $bs)
{
$str = str_repeat(rand(0,9), $bs);
$a = microtime(1);
$tmes = $fssize/$bs;
for($i = 0; $i < $tmes; $i++)
{
fwrite($f,$str);
}
$b = microtime(1);
$bw = $bs*$tmes;
$time = $b-$a;
$speed = $bw/$time;
echo "Wrote " . formatsize($bw) . " with a sample size of " . formatsize($bs) . " ({$tmes} samples) in " . $time . ": " . formatsize($speed) . "/s" . PHP_EOL;
$speeds[$speed] = $bs;
$fastest = max($fastest,$speed);
shell_exec("sync");
sleep(1);
}
$bs = $speeds[$fastest];
echo "Optimum sample size for writing is " . formatsize($bs) . "/s" . PHP_EOL;
$str = str_repeat(rand(0,9), $bs);
$a = microtime(1);
$tmes = $tsize/$bs;
for($i = 0; $i < $tmes; $i++)
{
fwrite($f,$str);
}
$b = microtime(1);
$bw = $bs*$tmes;
$time = $b-$a;
echo "Wrote " . formatsize($bw) . " ({$tmes} samples) in " . $time . ": " . formatsize($bw/$time) . "/s" . PHP_EOL;
function formatsize($size)
{
if($size == 0) return "0 Bytes";
if($size == 1) return "1 Byte";
if($size >= 1024 * 1024 * 1024)
{
return round($size / 1024 / 1024 / 1024,2) . " GiB";
}
elseif($size >= 1024 * 1024)
{
return round($size / 1024 / 1024,2) . " MiB";
}
elseif($size >= 1024)
{
return round($size / 1024,2) . " KiB";
}
elseif($size <= 1024)
{
return "{$size} Bytes";
}
}
fclose($f);
unlink("speedtest.data");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment