-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsTest.php
More file actions
297 lines (238 loc) · 10.9 KB
/
Copy pathMetricsTest.php
File metadata and controls
297 lines (238 loc) · 10.9 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Laravel\Horizon\Tests\Feature;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Queue;
use Laravel\Horizon\Contracts\MetricsRepository;
use Laravel\Horizon\Repositories\RedisMetricsRepository;
use Laravel\Horizon\Stopwatch;
use Laravel\Horizon\Tests\IntegrationTest;
use Mockery;
class MetricsTest extends IntegrationTest
{
public function test_total_throughput_is_stored()
{
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
$this->work();
$this->work();
$this->assertSame(2, resolve(MetricsRepository::class)->throughput());
}
public function test_throughput_is_stored_per_job_class()
{
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\ConditionallyFailingJob);
$this->work();
$this->work();
$this->work();
$this->work();
$this->assertSame(4, resolve(MetricsRepository::class)->throughput());
$this->assertSame(3, resolve(MetricsRepository::class)->throughputForJob(Jobs\BasicJob::class));
$this->assertSame(1, resolve(MetricsRepository::class)->throughputForJob(Jobs\ConditionallyFailingJob::class));
}
public function test_throughput_is_stored_per_queue()
{
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\ConditionallyFailingJob);
$this->work();
$this->work();
$this->work();
$this->work();
$this->assertSame(4, resolve(MetricsRepository::class)->throughput());
$this->assertSame(4, resolve(MetricsRepository::class)->throughputForQueue('default'));
}
public function test_average_runtime_is_stored_per_job_class_in_milliseconds()
{
$stopwatch = Mockery::mock(Stopwatch::class);
$stopwatch->shouldReceive('start');
$stopwatch->shouldReceive('check')->andReturn(1, 2);
$this->app->instance(Stopwatch::class, $stopwatch);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
$this->work();
$this->work();
$this->assertSame(1.5, resolve(MetricsRepository::class)->runtimeForJob(Jobs\BasicJob::class));
}
public function test_average_runtime_is_stored_per_queue_in_milliseconds()
{
$stopwatch = Mockery::mock(Stopwatch::class);
$stopwatch->shouldReceive('start');
$stopwatch->shouldReceive('check')->andReturn(1, 2);
$this->app->instance(Stopwatch::class, $stopwatch);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
$this->work();
$this->work();
$this->assertSame(1.5, resolve(MetricsRepository::class)->runtimeForQueue('default'));
}
public function test_list_of_all_jobs_with_metric_information_is_maintained()
{
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\ConditionallyFailingJob);
$this->work();
$this->work();
$jobs = resolve(MetricsRepository::class)->measuredJobs();
$this->assertCount(2, $jobs);
$this->assertContains(Jobs\ConditionallyFailingJob::class, $jobs);
$this->assertContains(Jobs\BasicJob::class, $jobs);
}
public function test_snapshot_of_metrics_performance_can_be_stored()
{
$stopwatch = Mockery::mock(Stopwatch::class);
$stopwatch->shouldReceive('start');
$stopwatch->shouldReceive('check')->andReturn(1, 2, 3);
$this->app->instance(Stopwatch::class, $stopwatch);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
// Run first two jobs...
$this->work();
$this->work();
// Take initial snapshot and set initial timestamp...
CarbonImmutable::setTestNow($firstTimestamp = CarbonImmutable::now());
resolve(MetricsRepository::class)->snapshot();
// Work another job and take another snapshot...
Queue::push(new Jobs\BasicJob);
$this->work();
CarbonImmutable::setTestNow(CarbonImmutable::now()->addSeconds(1));
resolve(MetricsRepository::class)->snapshot();
$snapshots = resolve(MetricsRepository::class)->snapshotsForJob(Jobs\BasicJob::class);
// Test job snapshots...
$this->assertEquals([
(object) [
'throughput' => 2,
'runtime' => 1.5,
'time' => $firstTimestamp->getTimestamp(),
],
(object) [
'throughput' => 1,
'runtime' => 3,
'time' => CarbonImmutable::now()->getTimestamp(),
],
], $snapshots);
// Test queue snapshots...
$snapshots = resolve(MetricsRepository::class)->snapshotsForQueue('default');
$this->assertEquals([
(object) [
'throughput' => 2,
'runtime' => 1.5,
'wait' => 0,
'time' => $firstTimestamp->getTimestamp(),
],
(object) [
'throughput' => 1,
'runtime' => 3,
'wait' => 0,
'time' => CarbonImmutable::now()->getTimestamp(),
],
], $snapshots);
}
public function test_jobs_processed_per_minute_since_last_snapshot_is_calculable()
{
$stopwatch = Mockery::mock(Stopwatch::class);
$stopwatch->shouldReceive('start');
$stopwatch->shouldReceive('check')->andReturn(1);
$this->app->instance(Stopwatch::class, $stopwatch);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
// Run first two jobs...
$this->work();
$this->work();
$this->assertSame(
2.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);
// Adjust current time...
CarbonImmutable::setTestNow(CarbonImmutable::now()->addMinutes(2));
$this->assertSame(
1.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);
// take snapshot and ensure count is reset...
resolve(MetricsRepository::class)->snapshot();
$this->assertSame(
0.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);
}
public function test_queue_with_maximum_runtime_and_throughput_compares_latest_snapshot()
{
$repository = resolve(MetricsRepository::class);
$connection = $repository->connection();
// Two measured queues, each with three snapshots (the case where the
// ZRANGE range matters — fewer than three would mask the bug). The most
// recent snapshot is the highest-scored member. "fast" has the greater
// throughput, "slow" the greater runtime, so each card must surface a
// different queue rather than an arbitrary one.
$connection->sadd('measured_queues', 'queue:fast', 'queue:slow');
foreach ([
'fast' => [['throughput' => 10, 'runtime' => 5], ['throughput' => 50, 'runtime' => 10], ['throughput' => 102, 'runtime' => 21]],
'slow' => [['throughput' => 3, 'runtime' => 100], ['throughput' => 7, 'runtime' => 200], ['throughput' => 11, 'runtime' => 338]],
] as $queue => $snapshots) {
foreach ($snapshots as $score => $snapshot) {
$connection->zadd('snapshot:queue:'.$queue, $score, json_encode($snapshot));
}
}
$this->assertSame('fast', $repository->queueWithMaximumThroughput());
$this->assertSame('slow', $repository->queueWithMaximumRuntime());
}
public function test_snapshot_does_not_fail_when_hmget_returns_null()
{
$connection = Mockery::mock();
$connection->shouldReceive('smembers')->with('measured_jobs')->andReturn(['job:Foo']);
$connection->shouldReceive('smembers')->with('measured_queues')->andReturn([]);
$connection->shouldReceive('transaction')->andReturn([null, 0]);
$connection->shouldReceive('zadd');
$connection->shouldReceive('zremrangebyrank');
$connection->shouldReceive('set');
$repository = Mockery::mock(RedisMetricsRepository::class.'[connection]', [app('redis')]);
$repository->shouldReceive('connection')->andReturn($connection);
$repository->snapshot();
$this->assertTrue(true);
}
public function test_only_past_24_snapshots_are_retained()
{
$stopwatch = Mockery::mock(Stopwatch::class);
$stopwatch->shouldReceive('start');
$stopwatch->shouldReceive('check')->andReturn(1);
$this->app->instance(Stopwatch::class, $stopwatch);
CarbonImmutable::setTestNow(CarbonImmutable::now());
// Run the jobs...
for ($i = 0; $i < 30; $i++) {
Queue::push(new Jobs\BasicJob);
$this->work();
resolve(MetricsRepository::class)->snapshot();
CarbonImmutable::setTestNow(CarbonImmutable::now()->addSeconds(1));
}
// Check the job snapshots...
$snapshots = resolve(MetricsRepository::class)->snapshotsForJob(Jobs\BasicJob::class);
$this->assertCount(24, $snapshots);
$this->assertSame(CarbonImmutable::now()->getTimestamp() - 1, $snapshots[23]->time);
// Check the queue snapshots...
$snapshots = resolve(MetricsRepository::class)->snapshotsForQueue('default');
$this->assertCount(24, $snapshots);
$this->assertSame(CarbonImmutable::now()->getTimestamp() - 1, $snapshots[23]->time);
CarbonImmutable::setTestNow();
}
public function test_metrics_can_be_cleared()
{
if (getenv('REDIS_CLUSTER_HOSTS_AND_PORTS')) {
$this->markTestSkipped('Test is for standalone Redis connections.');
}
Queue::push(new Jobs\BasicJob);
$this->work();
resolve(MetricsRepository::class)->snapshot();
// Work another job so live "job:*" and "queue:*" hashes exist alongside the snapshots...
Queue::push(new Jobs\BasicJob);
$this->work();
$this->assertNotEmpty(resolve(MetricsRepository::class)->measuredJobs());
$this->assertNotEmpty(resolve(MetricsRepository::class)->snapshotsForJob(Jobs\BasicJob::class));
$this->assertSame(1, resolve(MetricsRepository::class)->throughputForJob(Jobs\BasicJob::class));
resolve(MetricsRepository::class)->clear();
$this->assertEmpty(resolve(MetricsRepository::class)->measuredJobs());
$this->assertEmpty(resolve(MetricsRepository::class)->measuredQueues());
$this->assertEmpty(resolve(MetricsRepository::class)->snapshotsForJob(Jobs\BasicJob::class));
$this->assertEmpty(resolve(MetricsRepository::class)->snapshotsForQueue('default'));
$this->assertSame(0, resolve(MetricsRepository::class)->throughputForJob(Jobs\BasicJob::class));
$this->assertSame(0, resolve(MetricsRepository::class)->throughputForQueue('default'));
}
}