-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexWriteInstrumentation.php
More file actions
48 lines (42 loc) · 1.33 KB
/
Copy pathIndexWriteInstrumentation.php
File metadata and controls
48 lines (42 loc) · 1.33 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
<?php
declare(strict_types=1);
namespace Crustum\Explorator;
use Cake\Event\EventManager;
use Crustum\Explorator\Engine\Engine;
use Crustum\Explorator\Event\IndexWritePerformed;
use Throwable;
/**
* Times a Explorator index write callback and dispatches IndexWritePerformed.
*/
final class IndexWriteInstrumentation
{
/**
* Run an index write callback, measure duration, and fire IndexWritePerformed.
*
* @param iterable<\Cake\Datasource\EntityInterface> $entities Entities being written
* @param \Crustum\Explorator\Engine\Engine $engine Engine instance
* @param callable(): void $callback Write callback
* @param string $operation Operation name (`update` or `delete`)
* @return void
*/
public static function run(
iterable $entities,
Engine $engine,
callable $callback,
string $operation,
): void {
if (EventManager::instance()->listeners(IndexWritePerformed::NAME) === []) {
$callback();
return;
}
$started = hrtime(true);
$callback();
$durationMs = (hrtime(true) - $started) / 1e6;
try {
EventManager::instance()->dispatch(
IndexWritePerformed::fromWrite($entities, $engine, $durationMs, $operation),
);
} catch (Throwable) {
}
}
}