-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.php
96 lines (72 loc) · 2.29 KB
/
bench.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
<?php
require_once 'vendor/autoload.php';
//Not sure why composer cannot include this via classmap so include it anyway (TODO: Add an issue for Maphper)
require_once 'vendor/tombzombie/maphper/maphper/datasource/mysqladapter.php';
//Firstly create some dummy data and a database table using Maphper
//This is in a function so it can be reused. Maphper has an interal cache which I want to avoid
$pdo = new \PDO('mysql:dbname=maphpertest;host=127.0.0.1', 'u', '');
function getTopics() {
global $pdo;
$topics = new \Maphper\Maphper(new \Maphper\DataSource\Database($pdo, 'topics', 'id', ['editmode' => true]));
return $topics;
}
//Add 50 records to represent 50 forum posts
$topics = getTopics();
//Delete existing records
foreach ($topics as $topic) {
unset($topics[$topic->id]);
}
//Create some new records
for ($i = 0; $i < 50; $i++) {
$topic = new \stdclass;
$topic->title = 'Topic ' . $i;
$topic->author = 'Author ' . $i;
$topics[] = $topic;
}
//Check the data has been stored correctly
if (count($topics) < 50) {
echo 'Could not save data';
die;
}
unset($topics);
function benchmark($closure, $times = 100) {
$result = [];
for ($i = 0; $i < $times; $i++) {
$t1 = microtime(true);
$closure();
$t2 = microtime(true);
$result[] = $t2 - $t1;
}
return array_sum($result) / count($result);
}
echo 'Benchmarking CDS:';
echo benchmark(function() {
//Get a new instance of $topics each time to avoid maphper caching
$topics = getTopics();
$template = new \CDS\Builder(file_get_contents('template.xml'), 'topics.cds', $topics);
$output = $template->output();
});
echo "\n\n";
echo 'Benchmarking tpl:';
echo benchmark(function() {
//Get a new instance of $topics each time to avoid maphper caching
$topics = getTopics();
$data = ['topics' => $topics];
extract($data);
ob_start();
require 'template.php';
$output = ob_get_clean();
});
echo "\n\n";
echo 'Bechmkaring cached output:';
echo benchmark(function() {
//Get a new instance of $topics each time to avoid maphper caching
if (is_file('tmp/template.cache')) $output = file_get_contents('tmp/template.cache');
else {
$topics = getTopics();
$template = new \CDS\Builder(file_get_contents('template.xml'), 'topics.cds', $topics);
$output = $template->output();
file_put_contents('tmp/template.cache', $output);
}
});
echo "\n\n";