test_library.php
1.57 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
require 'vendor/autoload.php';
// vagrant ip
$host = '192.168.33.10';
function randFloat($min, $max)
{
$range = $max-$min;
$num = $min + $range * mt_rand(0, 32767)/32767;
$num = round($num, 4);
return ((float) $num);
}
$client = new \InfluxDB\Client($host);
$database = $client->selectDB('test');
if ($database->exists()) {
$database->drop();
}
$database->create(new \InfluxDB\Database\RetentionPolicy('test', '12w', 1, true));
$start = microtime(true);
$countries = ['nl', 'gb', 'us', 'be', 'th', 'jp', 'nl', 'sa'];
$colors = ['orange', 'black', 'yellow', 'white', 'red', 'purple'];
$points = [];
for ($i=0; $i < 1000; $i++) {
$points[] = new \InfluxDB\Point(
'flags',
randFloat(1, 999),
['country' => $countries[array_rand($countries)]],
['color' => $colors[array_rand($colors)]],
(int) shell_exec('date +%s%N')+mt_rand(1,1000)
);
};
// insert the points
$database->writePoints($points);
$end = microtime(true);
$country = $countries[array_rand($countries)];
$color = $colors[array_rand($colors)];
$results = $database->query("SELECT * FROM flags WHERE country = '$country' LIMIT 5")->getPoints();
$results2 = $database->query("SELECT * FROM flags WHERE color = '$color' LIMIT 5")->getPoints();
echo "Showing top 5 flags from country $country:" . PHP_EOL;
print_r($results);
echo PHP_EOL;
echo "Showing top 5 flags with color $color:" . PHP_EOL;
print_r($results2);
echo PHP_EOL;
echo sprintf('Executed 1000 inserts in %.2f seconds', $end - $start);