Skip to content

Commit c6cd31d

Browse files
Merge pull request #2162 from redis/DOC-5678-php-prob-dt-examples
DOC-5678 php prob dt examples
2 parents 2f8764d + e6ca32b commit c6cd31d

File tree

6 files changed

+415
-0
lines changed

6 files changed

+415
-0
lines changed

local_examples/php/DtBloomTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// EXAMPLE: bf_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtBloomTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtBloom() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START bloom
26+
$res1 = $r->bfreserve('bikes:models', 0.01, 1000);
27+
echo $res1 . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->bfadd('bikes:models', 'Smoky Mountain Striker');
31+
echo $res2 . PHP_EOL;
32+
// >>> 1
33+
34+
$res3 = $r->bfexists('bikes:models', 'Smoky Mountain Striker');
35+
echo $res3 . PHP_EOL;
36+
// >>> 1
37+
38+
$res4 = $r->bfmadd(
39+
'bikes:models',
40+
'Rocky Mountain Racer',
41+
'Cloudy City Cruiser',
42+
'Windy City Wippet'
43+
);
44+
echo json_encode($res4) . PHP_EOL;
45+
// >>> [1,1,1]
46+
47+
$res5 = $r->bfmexists(
48+
'bikes:models',
49+
'Rocky Mountain Racer',
50+
'Cloudy City Cruiser',
51+
'Windy City Wippet'
52+
);
53+
echo json_encode($res5) . PHP_EOL;
54+
// >>> [1,1,1]
55+
// STEP_END
56+
// REMOVE_START
57+
$this->assertEquals('OK', $res1);
58+
$this->assertEquals(1, $res2);
59+
$this->assertEquals(1, $res3);
60+
$this->assertEquals([1,1,1], $res4);
61+
$this->assertEquals([1,1,1], $res5);
62+
// REMOVE_END
63+
}
64+
}
65+

local_examples/php/DtCmsTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// EXAMPLE: cms_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtCmsTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtCms() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START cms
26+
$res1 = $r->cmsinitbyprob('bikes:profit', 0.001, 0.002);
27+
echo $res1 . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->cmsincrby('bikes:profit', 'Smoky Mountain Striker', 100);
31+
echo json_encode($res2) . PHP_EOL;
32+
// >>> [100]
33+
34+
$res3 = $r->cmsincrby(
35+
'bikes:profit',
36+
'Rocky Mountain Racer', 200,
37+
'Cloudy City Cruiser', 150
38+
);
39+
echo json_encode($res3) . PHP_EOL;
40+
// >>> [200,150]
41+
42+
$res4 = $r->cmsquery('bikes:profit', 'Smoky Mountain Striker');
43+
echo json_encode($res4) . PHP_EOL;
44+
// >>> [100]
45+
46+
$res5 = $r->cmsinfo('bikes:profit');
47+
echo $res5['width'] . ' ' . $res5['depth'] . ' ' . $res5['count'] . PHP_EOL;
48+
// >>> 2000 9 450
49+
// STEP_END
50+
// REMOVE_START
51+
$this->assertEquals('OK', $res1);
52+
$this->assertEquals([100], $res2);
53+
$this->assertEquals([200, 150], $res3);
54+
$this->assertEquals([100], $res4);
55+
$this->assertEquals(2000, $res5['width']);
56+
$this->assertEquals(9, $res5['depth']);
57+
$this->assertEquals(450, $res5['count']);
58+
// REMOVE_END
59+
}
60+
}
61+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// EXAMPLE: cuckoo_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtCuckooTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtCuckoo() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START cuckoo
26+
$res1 = $r->cfreserve('bikes:models', 1000000);
27+
echo $res1 . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->cfadd('bikes:models', 'Smoky Mountain Striker');
31+
echo $res2 . PHP_EOL;
32+
// >>> 1
33+
34+
$res3 = $r->cfexists('bikes:models', 'Smoky Mountain Striker');
35+
echo $res3 . PHP_EOL;
36+
// >>> 1
37+
38+
$res4 = $r->cfexists('bikes:models', 'Terrible Bike Name');
39+
echo $res4 . PHP_EOL;
40+
// >>> 0
41+
42+
$res5 = $r->cfdel('bikes:models', 'Smoky Mountain Striker');
43+
echo $res5 . PHP_EOL;
44+
// >>> 1
45+
// STEP_END
46+
// REMOVE_START
47+
$this->assertEquals('OK', $res1);
48+
$this->assertEquals(1, $res2);
49+
$this->assertEquals(1, $res3);
50+
$this->assertEquals(0, $res4);
51+
$this->assertEquals(1, $res5);
52+
// REMOVE_END
53+
}
54+
}
55+

local_examples/php/DtHllTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// EXAMPLE: hll_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtHllTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtHll() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START pfadd
26+
$res1 = $r->pfadd('bikes', ['Hyperion', 'Deimos', 'Phoebe', 'Quaoar']);
27+
echo $res1 . PHP_EOL;
28+
// >>> 1
29+
30+
$res2 = $r->pfcount('bikes');
31+
echo $res2 . PHP_EOL;
32+
// >>> 4
33+
34+
$res3 = $r->pfadd('commuter_bikes', ['Salacia', 'Mimas', 'Quaoar']);
35+
echo $res3 . PHP_EOL;
36+
// >>> 1
37+
38+
$res4 = $r->pfmerge('all_bikes', 'bikes', 'commuter_bikes');
39+
echo $res4 . PHP_EOL;
40+
// >>> OK
41+
42+
$res5 = $r->pfcount('all_bikes');
43+
echo $res5 . PHP_EOL;
44+
// >>> 6
45+
// STEP_END
46+
// REMOVE_START
47+
$this->assertEquals(1, $res1);
48+
$this->assertEquals(4, $res2);
49+
$this->assertEquals(1, $res3);
50+
$this->assertEquals('OK', $res4);
51+
$this->assertEquals(6, $res5);
52+
// REMOVE_END
53+
}
54+
}
55+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// EXAMPLE: tdigest_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtTdigestTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtTdigest() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START tdig_start
26+
$res1 = $r->tdigestcreate('bikes:sales', 100);
27+
echo $res1 . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->tdigestadd('bikes:sales', 21);
31+
echo $res2 . PHP_EOL;
32+
// >>> OK
33+
34+
$res3 = $r->tdigestadd('bikes:sales', 150, 95, 75, 34);
35+
echo $res3 . PHP_EOL;
36+
// >>> OK
37+
// STEP_END
38+
// REMOVE_START
39+
$this->assertEquals('OK', $res1);
40+
$this->assertEquals('OK', $res2);
41+
$this->assertEquals('OK', $res3);
42+
// REMOVE_END
43+
44+
// STEP_START tdig_cdf
45+
$res4 = $r->tdigestcreate('racer_ages');
46+
echo $res4 . PHP_EOL;
47+
// >>> OK
48+
49+
$res5 = $r->tdigestadd(
50+
'racer_ages',
51+
45.88,
52+
44.2,
53+
58.03,
54+
19.76,
55+
39.84,
56+
69.28,
57+
50.97,
58+
25.41,
59+
19.27,
60+
85.71,
61+
42.63
62+
);
63+
echo $res5 . PHP_EOL;
64+
// >>> OK
65+
66+
$res6 = $r->tdigestrank('racer_ages', 50);
67+
echo json_encode($res6) . PHP_EOL;
68+
// >>> [7]
69+
70+
$res7 = $r->tdigestrank('racer_ages', 50, 40);
71+
echo json_encode($res7) . PHP_EOL;
72+
// >>> [7,4]
73+
// STEP_END
74+
// REMOVE_START
75+
$this->assertEquals('OK', $res4);
76+
$this->assertEquals('OK', $res5);
77+
$this->assertEquals([7], $res6);
78+
$this->assertEquals([7, 4], $res7);
79+
// REMOVE_END
80+
81+
// STEP_START tdig_quant
82+
$res8 = $r->tdigestquantile('racer_ages', 0.5);
83+
echo json_encode($res8) . PHP_EOL;
84+
// >>> ["44.2"]
85+
86+
$res9 = $r->tdigestbyrank('racer_ages', 4);
87+
echo json_encode($res9) . PHP_EOL;
88+
// >>> ["42.63"]
89+
// STEP_END
90+
// REMOVE_START
91+
$this->assertEquals(['44.2'], $res8);
92+
$this->assertEquals(['42.63'], $res9);
93+
// REMOVE_END
94+
95+
// STEP_START tdig_min
96+
$res10 = $r->tdigestmin('racer_ages');
97+
echo $res10 . PHP_EOL;
98+
// >>> 19.27
99+
100+
$res11 = $r->tdigestmax('racer_ages');
101+
echo $res11 . PHP_EOL;
102+
// >>> 85.71
103+
// STEP_END
104+
// REMOVE_START
105+
$this->assertEquals('19.27', $res10);
106+
$this->assertEquals('85.71', $res11);
107+
// REMOVE_END
108+
109+
// STEP_START tdig_reset
110+
$res12 = $r->tdigestreset('racer_ages');
111+
echo $res12 . PHP_EOL;
112+
// >>> OK
113+
// STEP_END
114+
// REMOVE_START
115+
$this->assertEquals('OK', $res12);
116+
// REMOVE_END
117+
}
118+
}
119+

0 commit comments

Comments
 (0)