Repository navigation
Expand file tree
/
Copy pathTestRunner.php
More file actions
203 lines (158 loc) · 4.22 KB
/
Copy pathTestRunner.php
File metadata and controls
203 lines (158 loc) · 4.22 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace SOB;
class TestRunner
{
/**
* @var array<string,mixed>
*/
private array $currentResults = [];
/**
* @var array<int>
*/
private array $random = [];
/**
* @var array<array<string,mixed>>
*/
private array $results = [];
private int $updateOffset = 0;
public function __construct(private \SOB\Configurations $configurations)
{
$iterations = $configurations->getIterations();
$this->updateOffset = \random_int($iterations + 1, $iterations * 3000);
$count = (int)($iterations / 10);
for ($i = 0; $i < $count; ++$i)
{
$this->random[] = \random_int(1, $iterations);
}
}
/**
* @param array<string,mixed> $row
*/
public function addResults(array $row) : static
{
$this->results[] = $row;
return $this;
}
/**
* @return array<array<string,mixed>>
*/
public function getResults() : array
{
return $this->results;
}
/**
* @param array<int> $indexedTestsToRun indexes of specific tests to run
*/
public function runTests(array $indexedTestsToRun = []) : bool
{
$index = -1;
foreach ($this->configurations as $config)
{
++$index;
if ($indexedTestsToRun && ! \in_array($index, $indexedTestsToRun))
{
continue;
}
echo "Running test {$index}. ";
$namespace = $config->getNameSpace();
$class = "\\SOB\\{$namespace}\\Tests";
$tester = new $class();
if ($tester->dbSupported($config))
{
$this->test($tester, $config);
$this->addResults($this->currentResults);
$tester->closeConnection();
}
else
{
echo "{$namespace} does not support {$config->getDriver()}\n";
}
$tester = null;
}
return true;
}
private function setResult(string $test, \SOB\BaseLine $timer) : static
{
$duration = $timer->stop();
$memory = $timer->getMemory();
$this->currentResults[$test . ' Time'] = $duration;
$this->currentResults[$test . ' Memory'] = $memory;
return $this;
}
private function test(\SOB\Test $tester, \SOB\Configuration $config) : bool
{
// remove machine name
$system = \php_uname();
$host = \strtoupper(\gethostname());
$system = \str_replace($host . ' ', '', $system);
$this->currentResults = ['Date/Time' => \date('Y-m-d H:i:s'), 'System' => $system, 'PHP' => PHP_VERSION, 'Test' => $config->getNamespace(), 'Description' => $config->getDescription(), ];
echo "{$config->getNamespace()} -> {$config->getDescription()}\n";
$lines = $tester->getSchemaLines($config);
$runTime = new \SOB\BaseLine();
$start = new \SOB\BaseLine();
$tester->init($config, $lines, $runTime);
$this->setResult('Init', $start);
$iterations = $config->getIterations();
$timer = new \SOB\BaseLine();
echo "Executing insert test\n";
$id = 0;
for ($i = 1; $i <= $iterations; ++$i)
{
$newId = $tester->insert($i);
if ($newId <= $id)
{
throw new \Exception('Failed to insert ' . $newId);
}
$id = $newId;
}
$tester->flush();
$this->setResult('Insert', $timer);
$timer = new \SOB\BaseLine();
echo "Executing update test\n";
for ($i = 1; $i <= $iterations; ++$i)
{
$tester->testUpdate($tester->read($i), "Last {$i}");
}
$tester->flush();
$this->setResult('Read', $timer);
$timer = new \SOB\BaseLine();
for ($i = 1; $i <= $iterations; ++$i)
{
$tester->update($i, $i + $this->updateOffset);
}
$tester->flush();
$this->setResult('Update', $timer);
$timer = new \SOB\BaseLine();
for ($i = 1; $i <= $iterations; ++$i)
{
$expected = $i + $this->updateOffset;
$tester->testUpdate($tester->read($i), "Updated {$expected}");
}
$this->setResult('Update Test', $timer);
$timer = new \SOB\BaseLine();
echo "Executing random read test\n";
foreach ($this->random as $i)
{
$expected = $i + $this->updateOffset;
$tester->testUpdate($tester->read($i), "Updated {$expected}");
}
$this->setResult('Random Read', $timer);
$timer = new \SOB\BaseLine();
echo "Executing delete test\n";
for ($i = 1; $i <= $iterations; ++$i)
{
$tester->delete($i);
}
$tester->flush();
for ($i = 1; $i <= $iterations; ++$i)
{
if ($tester->read($i))
{
throw new \Exception('Failed to delete ' . $i);
}
}
$this->setResult('Delete', $timer);
$this->setResult('Total Runtime', $runTime);
return true;
}
}