Skip to content

Commit 697472e

Browse files
Anton Shaboutazloyuser
authored andcommitted
PHPBench integration
1 parent 608bdf0 commit 697472e

16 files changed

+1503
-325
lines changed

benchmark/read.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

benchmark/shared.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

benchmark/write.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

benchmarks/CassisBench.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* This file is part of PHPinnacle/Cassis.
4+
*
5+
* (c) PHPinnacle Team <dev@phpinnacle.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types = 1);
12+
13+
namespace PHPinnacle\Cassis\Benchmarks;
14+
15+
use Amp\Loop;
16+
use function Amp\call, Amp\Promise\wait;
17+
use PHPinnacle\Cassis\Cluster;
18+
use PHPinnacle\Cassis\Session;
19+
use PHPinnacle\Cassis\Value;
20+
21+
/**
22+
* @BeforeMethods({"setUp"})
23+
* @AfterMethods({"tearDown"})
24+
*/
25+
abstract class CassisBench
26+
{
27+
private const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
28+
29+
/**
30+
* @var Cluster
31+
*/
32+
protected $cluster;
33+
34+
/**
35+
* @var Session
36+
*/
37+
protected $session;
38+
39+
/**
40+
* @return void
41+
* @throws \Throwable
42+
*/
43+
public function setUp(): void
44+
{
45+
$this->wait(function () {
46+
if (!$dsn = \getenv('CASSIS_BENCHMARK_DSN')) {
47+
throw new \RuntimeException('No benchmark dsn! Please set CASSIS_BENCHMARK_DSN environment variable.');
48+
}
49+
50+
$this->cluster = Cluster::build($dsn);
51+
$this->session = yield $this->cluster->connect();
52+
53+
$setup = [
54+
"CREATE KEYSPACE IF NOT EXISTS blogs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };",
55+
"USE blogs;",
56+
"CREATE TYPE IF NOT EXISTS user (id int, name text, enabled boolean);",
57+
"CREATE TABLE IF NOT EXISTS posts_by_user (
58+
author frozen<user>,
59+
post_id timeuuid,
60+
text text,
61+
date timestamp,
62+
tags set<text>,
63+
PRIMARY KEY ((author), post_id)
64+
) WITH CLUSTERING ORDER BY (post_id DESC);",
65+
];
66+
67+
foreach ($setup as $query) {
68+
yield $this->session->query($query);
69+
}
70+
});
71+
}
72+
73+
/**
74+
* @return void
75+
* @throws \Throwable
76+
*/
77+
public function tearDown(): void
78+
{
79+
$this->wait(function () {
80+
yield $this->session->query("DROP KEYSPACE IF EXISTS blogs;");
81+
82+
$this->session->close();
83+
});
84+
}
85+
86+
/**
87+
* @param callable $action
88+
* @throws \Throwable
89+
*/
90+
public function wait(callable $action): void
91+
{
92+
wait(call($action));
93+
}
94+
95+
/**
96+
* @param int $length
97+
*
98+
* @return string
99+
*/
100+
public function randomString(int $length): string
101+
{
102+
$count = \strlen(self::ALPHABET);
103+
$string = '';
104+
105+
for ($i = 0; $i < $length; $i++) {
106+
$string .= self::ALPHABET[\rand(0, $count - 1)];
107+
}
108+
109+
return $string;
110+
}
111+
112+
/**
113+
* @return Value\Timestamp
114+
*/
115+
public function randomDate(): Value\Timestamp
116+
{
117+
/** @noinspection PhpUnhandledExceptionInspection */
118+
return Value\Timestamp::fromDateTime(new \DateTimeImmutable);
119+
}
120+
121+
/**
122+
* @param int $count
123+
* @param int $length
124+
*
125+
* @return Value\Collection
126+
*/
127+
public function randomTags(int $count, int $length): Value\Collection
128+
{
129+
$tags = [];
130+
131+
for ($i = 0; $i < $count; $i++) {
132+
$tags[] = $this->randomString($length);
133+
}
134+
135+
return Value\Collection::set($tags);
136+
}
137+
}

0 commit comments

Comments
 (0)