-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
148 lines (117 loc) · 5.31 KB
/
index.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
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
<?php
require './vendor/autoload.php';
require './cli-config.php';
if (!empty($optimize)) {
switch ($driver) {
case 'pdo_sqlite':
break;
case 'pdo_mysql':
$result = $dbConn->executeQuery('OPTIMIZE TABLE phpcr_nodes');
$result->fetchAll();
break;
case 'pdo_pgsql':
$result = $dbConn->executeQuery('ANALYZE phpcr_nodes');
$result->fetchAll();
break;
}
}
$session = getSession($factory, $credentials, $parameters, $workspace);
if (!$session instanceof \PHPCR\SessionInterface) {
exit("Failed to connect properly. If you add parameters, the first one needs to be 'benchmark', ie. 'php index.php benchmark --append' \n");
}
$rootPath = '/benchmark';
if (!$append && $session->nodeExists($rootPath)) {
$root = $session->getNode($rootPath);
$root->remove();
}
$session->save();
$session->refresh(false);
$sectionStart = 1;
if ($append) {
$sectionStart+= $sections;
$sections++;
}
$nodeName = ceil($count/2);
$path = $rootPath.'/1/'.$nodeName;
$stopWatch = new \Symfony\Component\Stopwatch\Stopwatch();
if (empty($disableQuery)) {
$qm = $session->getWorkspace()->getQueryManager();
$sql = "SELECT * FROM [nt:unstructured] WHERE count = '$nodeName' AND section = '1'";
$query = $qm->createQuery($sql, \PHPCR\Query\QueryInterface::JCR_SQL2);
$sql2 = "SELECT * FROM [nt:unstructured] WHERE count = '$nodeName' AND ISDESCENDANTNODE('$rootPath/1')";
$query2 = $qm->createQuery($sql2, \PHPCR\Query\QueryInterface::JCR_SQL2);
$sql = "SELECT * FROM [nt:unstructured] WHERE CONTAINS([nt:unstructured].md5, '".md5($nodeName)."')";
$query3 = $qm->createQuery($sql, \PHPCR\Query\QueryInterface::JCR_SQL2);
$sql2 = "SELECT * FROM [nt:unstructured] WHERE CONTAINS([nt:unstructured].md5, '".md5($nodeName)."') AND ISDESCENDANTNODE('$rootPath/1')";
$query4 = $qm->createQuery($sql2, \PHPCR\Query\QueryInterface::JCR_SQL2);
}
gc_enable();
$total = ($sectionStart - 1) * $count;
for ($i = $sectionStart; $i <= $sections; $i++) {
print_r("Current memory use is '".memory_get_usage()."' bytes \n");
$root = \PHPCR\Util\NodeHelper::createPath($session, "$rootPath/$i");
$stopWatch->start("insert nodes");
insertNodes($session, $root, $count, $i);
$event = $stopWatch->stop("insert nodes");
$total+= $count;
print_r("Inserting $count nodes (total $total) took '" . $event->getDuration(). "' ms.\n");
unset($session);
gc_collect_cycles();
$session = getSession($factory, $credentials, $parameters, $workspace);
$stopWatch->start("get a node");
$node = $session->getNode($path);
$event = $stopWatch->stop("get a node");
print_r("Getting a node by path took '" . $event->getDuration(). "' ms.\n");
validateNode($node, $path);
if (empty($disableQuery)) {
$stopWatch->start("search a node");
$result = $query->execute();
$event = $stopWatch->stop("search a node");
print_r("Searching a node by property took '" . $event->getDuration(). "' ms.\n");
$node = $result->getNodes()->current();
validateNode($node, $path);
$stopWatch->start("search a node in a subpath");
$result = $query2->execute();
$event = $stopWatch->stop("search a node in a subpath");
print_r("Searching a node by property in a subpath took '" . $event->getDuration(). "' ms.\n");
$node = $result->getNodes()->current();
validateNode($node, $path);
$stopWatch->start("search a node via contains");
$result = $query3->execute();
$event = $stopWatch->stop("search a node via contains");
print_r("Searching a node via contains took '" . $event->getDuration(). "' ms.\n");
$node = $result->getNodes()->current();
validateNode($node, $path);
$stopWatch->start("search a node via contains in a subpath");
$result = $query4->execute();
$event = $stopWatch->stop("search a node via contains in a subpath");
print_r("Searching a node via contains in a subpath took '" . $event->getDuration(). "' ms.\n");
$node = $result->getNodes()->current();
validateNode($node, $path);
}
}
print_r("Current memory use is '".memory_get_usage()."' bytes \n");
function validateNode(\PHPCR\NodeInterface $node = null, $path)
{
if (!$node) {
throw new \RuntimeException('Benchmark failing to read correct data: no node found');
}
if ($node->getPath() != $path) {
throw new \RuntimeException("Benchmark failing to read correct data: '$path' does not match '".$node->getPath()."'");
}
}
function insertNodes(\PHPCR\SessionInterface $session, \PHPCR\NodeInterface $root, $count, $section)
{
for ($i = 1; $i <= $count; $i++) {
$node = $root->addNode($i);
$node->setProperty('foo', 'bar', \PHPCR\PropertyType::STRING);
$node->setProperty('count', $i, \PHPCR\PropertyType::STRING);
$node->setProperty('section', $section, \PHPCR\PropertyType::STRING);
$node->setProperty('md5', md5($i), \PHPCR\PropertyType::STRING);
}
$session->save();
}
function getSession(\PHPCR\RepositoryFactoryInterface $factory, \PHPCR\SimpleCredentials $credentials, array $parameters, $workspace) {
$repository = $factory->getRepository($parameters);
return $repository->login($credentials, $workspace);
}