File: stresstest.php

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (114 lines) | stat: -rw-r--r-- 2,902 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

/// prints usage, doesn't exit
function usage() {
    echo "php ./stresstest.php --dir DIRECTORY\n".
         "\n".
         "  DIRECTORY       the directory in which this script\n".
         "                  should randomly touch and move files.\n".
         "\n".
         "Be warned for I will eat thy kittens\n";
}

if ( in_array('--help', $_SERVER['argv']) ) {
    usage();
    exit(0);
}

$i = array_search('--dir', $_SERVER['argv']);
$dir = $i > -1 && $i < $_SERVER['argc'] - 1 ? $_SERVER['argv'][$i+1] : '';

if ( empty($dir) ) {
    usage();
    exit(1);
}

chdir($dir) or die("cannot go to folder $dir\n");

/// our data class with parent pointer so we can safely rename folders
/// and still get proper paths for the children afterwards
class Entry {
    public $p = null;
    public $name;
    /// @return path (including file- / dirname) to this entry
    public function getPath() {
        if (!is_null($this->p)) {
            return $this->p->getPath() . '/' . $this->name;
        } else {
            return $this->name;
        }
    }
}

$dirIt = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator('.', RecursiveDirectoryIterator::CURRENT_AS_SELF),
  RecursiveIteratorIterator::SELF_FIRST
);

/// we'll grep random entries out of this array
$entries = array();

/// tmp pointers to dir entries
/// [dir => &Entry]
$tmpAssoc = array();

$i = 0;
foreach($dirIt as $file) {
    if ( $file->isDot() ) {
        continue;
    }
    $entries[$i] = new Entry;
    $entries[$i]->name = $file->getFilename();
    $path = $file->getPath();

    // skip .kdev4 folder or files
    if (strpos($path, 'kdev4') != -1) {
        continue;
    }

    if ($path != './') {
        $entries[$i]->p = &$tmpAssoc[$path];
    }
    if ($file->isDir()) {
        $tmpAssoc[$file->getPathname()] = &$entries[$i];
    }
    ++$i;
}

unset($tmpAssoc);

printf("Will move %s files randomly around in %s, do you REALLY want to continue?\n".
       "Type the following if you want to continue: SPARTAAAA ", count($entries), $dir);
$ret = fgets(STDIN, 10);

if (trim($ret) != 'SPARTAAAA') {
    echo "bummer, won't do anything, bye\n";
    exit(2);
}

// microseconds to sleep after move
$sleepTime = 250;

// number of moves until we stop
$moves = 10000;

$moved = 0;
for($moved = 0; $moved < $moves; ++$moved) {
    // get random file to move
    $index = array_rand($entries, 1);
    $entry =& $entries[$index];
    $oldPath = $entry->getPath();
    $newName = $entry->name;
    $lastDot = strrpos($newName, '.');
    if ($lastDot == -1 || $lastDot == 0) {
        $lastDot = strlen($newName);
    } else {
        --$lastDot;
    }
    $newName = substr_replace($newName, rand(), rand() % $lastDot, rand() % $lastDot);
    $entry->name = $newName;
    $newPath = $entry->getPath();
    printf("moving $oldPath to $newPath\n");
    rename($oldPath, $newPath);
    usleep($sleepTime);
}