Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total refactoring && benchmark infrastructure #1

Merged
merged 1 commit into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/vendor
/build
.phpunit.result.cache
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Loop::run(function () {
foreach ($result as $row) {
printf("The keyspace %s has a table called %s\n", $row['keyspace_name'], $row['columnfamily_name']);
}

yield $cluster->disconnect();
});

```
Expand Down
46 changes: 46 additions & 0 deletions benchmark/shared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

function random_string(int $length): string
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$count = \strlen($chars);

$string = '';

for ($i = 0; $i < $length; $i++) {
$string .= $chars[\rand(0, $count - 1)];
}

return $string;
}

function random_date(): \DateTimeInterface
{
/** @noinspection PhpUnhandledExceptionInspection */
return new \DateTimeImmutable;
}

function random_tags(int $count, int $length): array
{
$tags = [];

for ($i = 0; $i < $count; $i++) {
$tags[] = random_string($length);
}

return $tags;
}

return [
"CREATE KEYSPACE IF NOT EXISTS blogs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 };",
"USE blogs;",
"CREATE TYPE IF NOT EXISTS user (id int, name text, enabled boolean);",
"CREATE TABLE IF NOT EXISTS posts_by_user (
author frozen<user>,
post_id timeuuid,
text text,
date timestamp,
tags set<text>,
PRIMARY KEY ((author), post_id)
) WITH CLUSTERING ORDER BY (post_id DESC);",
];
19 changes: 0 additions & 19 deletions benchmark/simple.php

This file was deleted.

69 changes: 69 additions & 0 deletions benchmark/write.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Amp\Loop;
use PHPinnacle\Cassis\Session;
use PHPinnacle\Cassis\Cluster;
use PHPinnacle\Cassis\Value;
use Ramsey\Uuid\Uuid;

require_once __DIR__ . '/../vendor/autoload.php';

Loop::run(function () use ($argv) {
if (!$dsn = \getenv('CASSIS_BENCHMARK_DSN')) {
echo 'No benchmark dsn! Please set CASSIS_BENCHMARK_DSN environment variable.', \PHP_EOL;

Loop::stop();
}

$cluster = Cluster::build(\getenv('CASSIS_EXAMPLE_DSN'));
/** @var Session $session */
$session = yield $cluster->connect();
$setup = require __DIR__ . '/shared.php';

$watcher = Loop::onSignal(SIGTERM, function () use ($cluster) {
yield $cluster->disconnect();
});

try {
foreach ($setup as $query) {
yield $session->query($query);
}

$time = \microtime(true);
$count = $argv[1] ?? 1000;
$promises = [];

for ($i = 1; $i <= $count; $i++) {
$author = new Value\UserDefined([
'id' => $i,
'name' => "User $i",
'enabled' => (bool) ($i % 2),
]);

$arguments = [
'author' => $author,
'post_id' => Uuid::uuid1(),
'text' => random_string(500),
'date' => Value\Timestamp::fromDateTime(random_date()),
'tags' => Value\Collection::set(random_tags(\rand(1, 10), 5)),
];

$fields = \implode(',', \array_keys($arguments));
$values = \implode(',', \array_fill(0, \count($arguments), '?'));

$promises[] = $session->query("INSERT INTO posts_by_user ($fields) VALUES ($values)", $arguments);
}

yield $promises;

echo \sprintf("Done %d inserts in %f seconds.\n", $count, \microtime(true) - $time);
} catch (\Throwable $error) {
echo "Got error: {$error->getMessage()}.\n";
} finally {
yield $session->query("DROP KEYSPACE IF EXISTS blogs;");
}

yield $cluster->disconnect();

Loop::cancel($watcher);
});
20 changes: 14 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@
"require": {
"php": "^7.2",
"amphp/amp": "^2.0",
"amphp/socket": "^0.10.11",
"phpinnacle/buffer": "^0.1.1",
"ramsey/uuid": "^3.8"
"amphp/socket": "^0.10",
"phpinnacle/buffer": "^0.1"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"amphp/phpunit-util": "^1.0"
"phpunit/phpunit": "^8.0",
"ramsey/uuid": "^3.8"
},
"suggest": {
"ramsey/uuid": "For use Uuid and Timeuuid Cassandra types",
"ext-gmp": "For use Varint and Decimal Cassandra types",
"ext-snappy": "For use Google Snappy compression mechanism",
"ext-lz4": "For use LZ4 compression mechanism"
},
"autoload": {
"psr-4": {
"PHPinnacle\\Cassis\\": "src"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
Loading