Created
January 19, 2020 05:49
-
-
Save kmuthukk/f93a5373dbaddd4e49427cc7b4130427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ip = '127.0.0.1'; | |
$cluster = Cassandra::cluster() | |
->withDefaultConsistency(Cassandra::CONSISTENCY_QUORUM) | |
->withContactPoints($ip) | |
->build(); | |
$session = $cluster->connect(); | |
$session->execute("CREATE KEYSPACE IF NOT EXISTS k"); | |
$session->execute("USE k"); | |
$session->execute("DROP TABLE IF EXISTS users"); | |
print("Dropped users table (if exists)\n"); | |
$session->execute(" | |
CREATE TABLE IF NOT EXISTS users( | |
id text, | |
name text, | |
age int, | |
PRIMARY KEY(id) | |
) WITH TRANSACTIONS = {'enabled' : false}"); | |
print("Created users table\n"); | |
print("-------\n"); | |
$insert_statement = new Cassandra\SimpleStatement('INSERT INTO users (id, name, age) VALUES (?, ?, ?)'); | |
$delete_statement = new Cassandra\SimpleStatement('DELETE FROM users WHERE id = ?'); | |
$row_cnt=4000; | |
$iters=25; // perform these many overwrites of each key | |
for($iter=1; $iter <= $iters; $iter++) { | |
$start = microtime(true); | |
for($idx=0; $idx<$row_cnt; $idx++) { | |
$options = array('arguments' => array('user-' . $idx)); | |
$rows = $session->execute($delete_statement, $options); | |
$options = array('arguments' => array('user-' . $idx, 'name-'.$idx.'-iter-'.$iter, 20+($idx%50))); | |
$rows = $session->execute($insert_statement, $options); | |
} | |
$taken = round(microtime(true)-$start,2); | |
print("Iteration: " . $iter . "\n"); | |
print("Delete+Insert ops per sec: " . (($row_cnt * 2) / $taken) ."\n"); | |
print("Avg delete+insert latency (ms): " . round($taken*1000/$row_cnt, 2) . "\n"); | |
print("-------\n"); | |
} | |
$statement = new Cassandra\SimpleStatement('SELECT * FROM k.users where id = ?'); | |
$start = microtime(true); | |
for($idx=0;$idx<$row_cnt;$idx++) { | |
$options = array('arguments' => array('user-' . $idx)); | |
$rows = $session->execute($statement, $options); | |
} | |
$taken = round(microtime(true)-$start,2); | |
print_r($rows->first()); // print the final row, just for sanity. | |
echo 'Read ops per sec: '.$row_cnt/$taken."\n"; | |
echo 'Avg read latency (ms): ' . round($taken*1000/$row_cnt, 2) . "\n"; | |
print("-------\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment