Skip to content

Commit c1380f1

Browse files
Anton Shaboutazloyuser
authored andcommitted
Refactoring && benchmark infrastructure
1 parent ee7cc43 commit c1380f1

File tree

128 files changed

+6750
-1823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+6750
-1823
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea
22
/vendor
33
/build
4+
.phpunit.result.cache

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Loop::run(function () {
4444
foreach ($result as $row) {
4545
printf("The keyspace %s has a table called %s\n", $row['keyspace_name'], $row['columnfamily_name']);
4646
}
47+
48+
yield $cluster->disconnect();
4749
});
4850

4951
```

benchmark/shared.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
function random_string(int $length): string
4+
{
5+
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
6+
$count = \strlen($chars);
7+
8+
$string = '';
9+
10+
for ($i = 0; $i < $length; $i++) {
11+
$string .= $chars[\rand(0, $count - 1)];
12+
}
13+
14+
return $string;
15+
}
16+
17+
function random_date(): \DateTimeInterface
18+
{
19+
/** @noinspection PhpUnhandledExceptionInspection */
20+
return new \DateTimeImmutable;
21+
}
22+
23+
function random_tags(int $count, int $length): array
24+
{
25+
$tags = [];
26+
27+
for ($i = 0; $i < $count; $i++) {
28+
$tags[] = random_string($length);
29+
}
30+
31+
return $tags;
32+
}
33+
34+
return [
35+
"CREATE KEYSPACE IF NOT EXISTS blogs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };",
36+
"USE blogs;",
37+
"CREATE TYPE IF NOT EXISTS user (id int, name text, enabled boolean);",
38+
"CREATE TABLE IF NOT EXISTS posts_by_user (
39+
author frozen<user>,
40+
post_id timeuuid,
41+
text text,
42+
date timestamp,
43+
tags set<text>,
44+
PRIMARY KEY ((author), post_id)
45+
) WITH CLUSTERING ORDER BY (post_id DESC);",
46+
];

benchmark/simple.php

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

benchmark/write.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Amp\Loop;
4+
use PHPinnacle\Cassis\Session;
5+
use PHPinnacle\Cassis\Cluster;
6+
use PHPinnacle\Cassis\Value;
7+
use Ramsey\Uuid\Uuid;
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
10+
11+
Loop::run(function () use ($argv) {
12+
if (!$dsn = \getenv('CASSIS_BENCHMARK_DSN')) {
13+
echo 'No benchmark dsn! Please set CASSIS_BENCHMARK_DSN environment variable.', \PHP_EOL;
14+
15+
Loop::stop();
16+
}
17+
18+
$cluster = Cluster::build(\getenv('CASSIS_EXAMPLE_DSN'));
19+
/** @var Session $session */
20+
$session = yield $cluster->connect();
21+
$setup = require __DIR__ . '/shared.php';
22+
23+
$watcher = Loop::onSignal(SIGTERM, function () use ($cluster) {
24+
yield $cluster->disconnect();
25+
});
26+
27+
try {
28+
foreach ($setup as $query) {
29+
yield $session->query($query);
30+
}
31+
32+
$time = \microtime(true);
33+
$count = $argv[1] ?? 1000;
34+
$promises = [];
35+
36+
for ($i = 1; $i <= $count; $i++) {
37+
$author = new Value\UserDefined([
38+
'id' => $i,
39+
'name' => "User $i",
40+
'enabled' => (bool) ($i % 2),
41+
]);
42+
43+
$arguments = [
44+
'author' => $author,
45+
'post_id' => Uuid::uuid1(),
46+
'text' => random_string(500),
47+
'date' => Value\Timestamp::fromDateTime(random_date()),
48+
'tags' => Value\Collection::set(random_tags(\rand(1, 10), 5)),
49+
];
50+
51+
$fields = \implode(',', \array_keys($arguments));
52+
$values = \implode(',', \array_fill(0, \count($arguments), '?'));
53+
54+
$promises[] = $session->query("INSERT INTO posts_by_user ($fields) VALUES ($values)", $arguments);
55+
}
56+
57+
yield $promises;
58+
59+
echo \sprintf("Done %d inserts in %f seconds.\n", $count, \microtime(true) - $time);
60+
} catch (\Throwable $error) {
61+
echo "Got error: {$error->getMessage()}.\n";
62+
} finally {
63+
yield $session->query("DROP KEYSPACE IF EXISTS blogs;");
64+
}
65+
66+
yield $cluster->disconnect();
67+
68+
Loop::cancel($watcher);
69+
});

composer.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@
1818
"require": {
1919
"php": "^7.2",
2020
"amphp/amp": "^2.0",
21-
"amphp/socket": "^0.10.11",
22-
"phpinnacle/buffer": "^0.1.1",
23-
"ramsey/uuid": "^3.8"
21+
"amphp/socket": "^0.10",
22+
"phpinnacle/buffer": "^0.1"
2423
},
2524
"require-dev": {
26-
"phpunit/phpunit": "^6.0",
27-
"amphp/phpunit-util": "^1.0"
25+
"phpunit/phpunit": "^8.0",
26+
"ramsey/uuid": "^3.8"
27+
},
28+
"suggest": {
29+
"ramsey/uuid": "For use Uuid and Timeuuid Cassandra types",
30+
"ext-gmp": "For use Varint and Decimal Cassandra types",
31+
"ext-snappy": "For use Google Snappy compression mechanism",
32+
"ext-lz4": "For use LZ4 compression mechanism"
2833
},
2934
"autoload": {
3035
"psr-4": {
3136
"PHPinnacle\\Cassis\\": "src"
32-
}
37+
},
38+
"files": [
39+
"src/functions.php"
40+
]
3341
},
3442
"autoload-dev": {
3543
"psr-4": {

0 commit comments

Comments
 (0)