-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin_example.php
50 lines (45 loc) · 1.92 KB
/
plugin_example.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
<?php
// This is a plugin example which points out the functions that have to be implemented and
// demonstrated the expected behavior
class plugin {
private $sphinxql = false;
/* this function will be called when each parallel worker starts,
it's expected that you initialize a conneciton here etc.*/
public function init() {
// If you want you can parse additional params passed to test.php
$options = getopt('', array('port::'));
$port = isset($options['port']) ? $options['port'] : 9315;
// Let's make a SphinxQL connection which will be reused later
$this->sphinxql = new mysqli('127.0.0.1', '', '', '', $port);
}
/* this function will be called when a parallel worker is done with preparing a batch (of 1 or multiple documents)
Accepts: array(document_id => document)
Returns: array(
document_id (as appeared in the input) => array('latency' => value_in_seconds[, optionally smth else about the query])
)
*/
public function query($queries) {
$out = array();
foreach ($queries as $id=>$query) {
$t = microtime(true);
$res = $this->sphinxql->query("select * from idx where match('".$this->sphinxql->escape_string($query)."') limit 100000 option max_matches=100000");
$out[$id] = array('latency' => microtime(true) - $t, 'num_rows' => $res->num_rows);
}
return $out;
}
/* this static function is called after the test.
Here the plugin is expected to do some additional analysis based on the primary query results.
The output will be merged into the output of the main script.
Accepts: array(document_id => array returned by query())
Returns: array('name' => 'value'[, 'name2' => 'value2', ...])
*/
public static function report($queriesInfo) {
$totalMatches = 0;
foreach($queriesInfo as $id => $info) {
$totalMatches += $info['num_rows'];
}
return array(
'Total matches' => $totalMatches,
'Count' => count($queriesInfo));
}
}