File indexing completed on 2024-05-19 15:45:48
0001 <?php 0002 0003 /// prints usage, doesn't exit 0004 function usage() { 0005 echo "php ./stresstest.php --dir DIRECTORY\n". 0006 "\n". 0007 " DIRECTORY the directory in which this script\n". 0008 " should randomly touch and move files.\n". 0009 "\n". 0010 "Be warned for I will eat thy kittens\n"; 0011 } 0012 0013 if ( in_array('--help', $_SERVER['argv']) ) { 0014 usage(); 0015 exit(0); 0016 } 0017 0018 $i = array_search('--dir', $_SERVER['argv']); 0019 $dir = $i > -1 && $i < $_SERVER['argc'] - 1 ? $_SERVER['argv'][$i+1] : ''; 0020 0021 if ( empty($dir) ) { 0022 usage(); 0023 exit(1); 0024 } 0025 0026 chdir($dir) or die("cannot go to folder $dir\n"); 0027 0028 /// our data class with parent pointer so we can safely rename folders 0029 /// and still get proper paths for the children afterwards 0030 class Entry { 0031 public $p = null; 0032 public $name; 0033 /// @return path (including file- / dirname) to this entry 0034 public function getPath() { 0035 if (!is_null($this->p)) { 0036 return $this->p->getPath() . '/' . $this->name; 0037 } else { 0038 return $this->name; 0039 } 0040 } 0041 } 0042 0043 $dirIt = new RecursiveIteratorIterator( 0044 new RecursiveDirectoryIterator('.', RecursiveDirectoryIterator::CURRENT_AS_SELF), 0045 RecursiveIteratorIterator::SELF_FIRST 0046 ); 0047 0048 /// we'll grep random entries out of this array 0049 $entries = array(); 0050 0051 /// tmp pointers to dir entries 0052 /// [dir => &Entry] 0053 $tmpAssoc = array(); 0054 0055 $i = 0; 0056 foreach($dirIt as $file) { 0057 if ( $file->isDot() ) { 0058 continue; 0059 } 0060 $entries[$i] = new Entry; 0061 $entries[$i]->name = $file->getFilename(); 0062 $path = $file->getPath(); 0063 0064 // skip .kdev4 folder or files 0065 if (strpos($path, 'kdev4') != -1) { 0066 continue; 0067 } 0068 0069 if ($path != './') { 0070 $entries[$i]->p = &$tmpAssoc[$path]; 0071 } 0072 if ($file->isDir()) { 0073 $tmpAssoc[$file->getPathname()] = &$entries[$i]; 0074 } 0075 ++$i; 0076 } 0077 0078 unset($tmpAssoc); 0079 0080 printf("Will move %s files randomly around in %s, do you REALLY want to continue?\n". 0081 "Type the following if you want to continue: SPARTAAAA ", count($entries), $dir); 0082 $ret = fgets(STDIN, 10); 0083 0084 if (trim($ret) != 'SPARTAAAA') { 0085 echo "bummer, won't do anything, bye\n"; 0086 exit(2); 0087 } 0088 0089 // microseconds to sleep after move 0090 $sleepTime = 250; 0091 0092 // number of moves until we stop 0093 $moves = 10000; 0094 0095 $moved = 0; 0096 for($moved = 0; $moved < $moves; ++$moved) { 0097 // get random file to move 0098 $index = array_rand($entries, 1); 0099 $entry =& $entries[$index]; 0100 $oldPath = $entry->getPath(); 0101 $newName = $entry->name; 0102 $lastDot = strrpos($newName, '.'); 0103 if ($lastDot == -1 || $lastDot == 0) { 0104 $lastDot = strlen($newName); 0105 } else { 0106 --$lastDot; 0107 } 0108 $newName = substr_replace($newName, rand(), rand() % $lastDot, rand() % $lastDot); 0109 $entry->name = $newName; 0110 $newPath = $entry->getPath(); 0111 printf("moving $oldPath to $newPath\n"); 0112 rename($oldPath, $newPath); 0113 usleep($sleepTime); 0114 }