-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-benchmark.php
51 lines (41 loc) · 1.18 KB
/
cache-benchmark.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
<?php
$cacheStores = ['array', 'memcached', 'redis', 'database', 'file'];
$readToWriteRatio = 20;
$numItems = 100;
function benchmarkCacheStore($cacheStore, $readToWriteRatio, $dataSet)
{
$tStart = microtime(true);
foreach ($dataSet as $cacheKey => $value) {
cache()->store($cacheStore)->put($cacheKey, $value, 1000);
for ($j = 0; $j < $readToWriteRatio; $j++) {
cache()->store($cacheStore)->get($cacheKey);
}
}
return microtime(true) - $tStart;
}
/**
* @param int $items
* @return array
*/
function prepareData(int $items)
{
$data = [];
for ($i = 0; $i < $items; $i++) {
$cacheKey = 'some-cache-key-' . $i;
$value = str_repeat(md5($i), 5);
$data[$cacheKey] = $value;
}
return $data;
}
$dataSet = prepareData($numItems);
$numCacheQueries = $numItems * (1 + $readToWriteRatio);
foreach ($cacheStores as $cacheStore) {
echo $cacheStore . ': ';
try {
$time = benchmarkCacheStore($cacheStore, $readToWriteRatio, $dataSet);
echo(round($time, 3) . " seconds, " . round($numCacheQueries / $time) . " queries/sec\n");
} catch (Exception $e) {
echo " n/a\n";
}
}
exit;