-
Notifications
You must be signed in to change notification settings - Fork 6
/
stress-tests.php
122 lines (94 loc) · 2.97 KB
/
stress-tests.php
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
115
116
117
118
119
120
121
122
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* Stress tests runner
* Usage: php stress-tests.php
*/
$cliOptions = getopt('n:c:r:h', [
'name:',
'config:',
'repeat:',
'help',
]);
$options = [
'collectionName' => $cliOptions['name'] ?? $cliOptions['n'] ?? null,
'configFile' => $cliOptions['config'] ?? $cliOptions['c'] ?? null,
'repeatNo' => $cliOptions['repeat'] ?? $cliOptions['r'] ?? 1e3,
'help' => $cliOptions['help'] ?? $cliOptions['h'] ?? null,
];
// Show help
if ($options['help'] !== null) {
echo <<<HTML
Run stress tests on current cockpit installation
Usage:
php {$argv[0]} --name articles --config tests/config.php
Options:
-n --name Collection name
-c --conifg Path to config file. When not defined using default one.
-r --repeat Repeat number
-c --help Show help
HTML;
exit(0);
}
// Read collection name
if ($options['collectionName']) {
sprintf('Info: Using collection `%s`', $options['collectionName']) . PHP_EOL;
} else {
echo 'Info: Using all available collections' . PHP_EOL;
}
// Get path to config from command line
if (!$options['configFile']) {
echo 'Info: Using default config' . PHP_EOL;
} else {
echo sprintf('Info: Using custom config `%s`', $options['configFile']) . PHP_EOL;
// Validate that path exists
$configPath = __DIR__ . '/' . $options['configFile'];
if (!is_file($configPath)) {
exit(sprintf('Error: Config does not exist at path `%s`', $configPath) . PHP_EOL);
}
define('COCKPIT_CONFIG_PATH', $configPath);
}
// Initialize cockpit
$cockpitBootstrap = __DIR__ . '/../../bootstrap.php';
if (!file_exists($cockpitBootstrap)) {
exit('Error: Cockpit not installed in parent directory' . PHP_EOL);
}
require $cockpitBootstrap;
// Start timer
$perf = [
'startAt' => microtime(true),
'endAt' => null,
];
// Resolve collection names
$collectionNames = $options['collectionName']
? [$collectionName]
: array_column($cockpit->module('collections')->collections(), 'name');
// Run tasks
foreach ($collectionNames as $collectionName) {
if (!$cockpit->module('collections')->exists($collectionName)) {
echo sprintf('Warning: Collection `%s` does not exist', $collectionName) . PHP_EOL;
continue;
}
echo sprintf('- %s', $collectionName) . PHP_EOL;
// Common properties are: _id|_created|_modified|_by|_mby|_o
for ($i = 0; $i < $options['repeatNo']; $i++) {
// Sort descending by created at
$cockpit->module('collections')->find($collectionName, [
['sort' => ['_created' => -1]],
]);
// Filter ids >= 0
$cockpit->module('collections')->find($collectionName, [
'filter' => ['_id' => [
'$gte' => 0
]]
]);
}
}
$perf['endAt'] = microtime(true);
// Summary
echo PHP_EOL . vsprintf('Tasks took in %.6fs (%d×)', [
$perf['endAt'] - $perf['startAt'],
$options['repeatNo'],
]) . PHP_EOL;
exit(0);