I have written a script to highlight the superiority of shared memory storage.
Although it doesn't use the shmop function, the underlying concept is similar.
'/shm_dir/' is a tmpfs directory, which is based on shared memory, that I have mounted on the server.
Below is the result on an Intel Pentium VI 2.8 server:
IO test on 1000 files
IO Result of Regular Directory : 0.079015016555786
IO Result of Shared Memory Directory : 0.047761917114258
IO test on 10000 files
IO Result of Regular Directory : 3.7090260982513
IO Result of Shared Memory Directory : 0.46256303787231
IO test on 40000 files
IO Result of Regular Directory : 117.35703110695 seconds
IO Result of Shared Memory Directory : 2.6221358776093 seconds
The difference is not very apparent nor convincing at 100 files.
But when we step it up a level to 10000 and 40000 files, it becomes pretty obvious that Shared Memory is a better contender.
Script courtesy of http://www.enhost.com
<?php
set_time_limit(0);
// Your regular directory. Make sure it is write enabled
$setting['regular_dir'] = '/home/user/regular_directory/';
// Your shared memory directory.
$setting['shm_dir'] = '/shm_dir/';
// Number of files to read and write
$setting['files'] = 40000;
function IO_Test($mode)
{
$starttime = time()+microtime();
global $setting;
for($i = 0 ; $i< $setting['files'] ;$i++)
{
$filename = $setting[$mode].'test'.$i.'.txt';
$content = "Just a random content";
// Just some error detection
if (!$handle = fopen($filename, 'w+'))
{
echo "Can't open the file ".$filename;
exit;
}
if (fwrite($handle, $content ) === FALSE)
{
echo "Can't write to file : ".$filename;
exit;
}
fclose($handle);
// Read Test
file_get_contents($filename);
}
$endtime = time()+microtime();
$totaltime = ($endtime - $starttime);
return $totaltime;
}
echo '<b>IO test on '.$setting['files']. ' files</b><br>';
echo 'IO Result of <b>Regular</b> Directory : '.IO_Test('regular_dir') .' seconds<br>';
echo 'IO Result of <b>Shared Memory</b> Directory : '.IO_Test('shm_dir') .' seconds<br>';
/* Removal of files to avoid underestimation
#
# Failure to remove files will result in inaccurate benchmark
# as it will result in the IO_Test function not re-creating the existing files
*/
foreach ( glob($setting['regular_dir']."*.txt") as $filename) {
unlink($filename);$cnt ++;
}
foreach ( glob($setting['shm_dir']."*.txt") as $filename) {
unlink($filename);$cnt ++;
}
?>