note 61242 modified in ref.shmop by danbrown

From: Date: Thu, 21 May 2009 15:06:24 +0000
Subject: note 61242 modified in ref.shmop by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155847@lists.php.net to get a copy of this message
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 ++; } ?> --was-- 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 <? 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 ++; } ?> http://php.net/manual/en/ref.shmop.php

« previous php.notes (#155847) next »