Skip to content

Instantly share code, notes, and snippets.

@adamrefaey
Last active March 24, 2022 01:30
Show Gist options
  • Save adamrefaey/c1988e237f46db90dae17f3097e5bae8 to your computer and use it in GitHub Desktop.
Save adamrefaey/c1988e237f46db90dae17f3097e5bae8 to your computer and use it in GitHub Desktop.
Basic Couchbase Benchmark
<?php
use \Couchbase\ClusterOptions;
use \Couchbase\Cluster;
use \Couchbase\CollectionManager;
use \Couchbase\CollectionSpec;
use \Couchbase\InvalidConfigurationException;
// $port = ;
// $host = '';
// $bucketName = '';
// $username = '';
// $password = '';
$connectionString = sprintf('couchbase://%s:%s', $host, $port);
$options = new ClusterOptions();
$options->credentials($username, $password);
$cluster = new Cluster($connectionString, $options);
// var_dump($cluster);
// $buckets = $cluster->buckets()->getAllBuckets();
// var_dump(array_map(fn ($b) => $b->name(), $buckets));
$bucket = $cluster->bucket($bucketName);
// var_dump($bucket);
// $bucket
// ->collections()
// ->createCollection((new CollectionSpec())->setName('test'));
$collection = $bucket->defaultCollection();
// $collection = $bucket->defaultScope()->collection('test');
// var_dump($collection);
$insertStartTime = microtime(true);
$ids = [];
for ($i=1; $i <= 100; $i++) {
$ids[] = $id = sprintf('%s-%s', microtime(true), $i);
$collection->insert($id, ['xtest' => 'testing value']);
}
$insertEndTime = microtime(true);
echo sprintf('Write (insert) time for 100 documents: %s seconds', round($insertEndTime - $insertStartTime, 2)) . PHP_EOL;
echo sprintf('Average write time for a document: %s seconds', round(($insertEndTime - $insertStartTime) / 100, 2)) . PHP_EOL;
// var_dump($ids);
$documentsIds = [
"1648082485.4075-1",
"1648082485.5852-2",
"1648082486.7496-3",
"1648082486.929-4",
"1648082487.0825-5",
"1648082487.2363-6",
"1648082487.391-7",
"1648082487.5445-8",
"1648082487.698-9",
"1648082487.8517-10",
"1648082488.063-11",
"1648082489.1814-12",
"1648082489.3355-13",
"1648082489.4904-14",
"1648082489.6445-15",
"1648082489.7981-16",
"1648082489.9524-17",
"1648082490.1059-18",
"1648082490.2595-19",
"1648082490.5665-20",
"1648082490.7211-21",
"1648082490.8747-22",
"1648082491.03-23",
"1648082491.184-24",
"1648082491.3386-25",
"1648082491.4934-26",
"1648082491.657-27",
"1648082491.815-28",
"1648082491.97-29",
"1648082492.1241-30",
"1648082492.2798-31",
"1648082492.4362-32",
"1648082492.5897-33",
"1648082492.7436-34",
"1648082492.9004-35",
"1648082493.0646-36",
"1648082493.2216-37",
"1648082493.3757-38",
"1648082493.5302-39",
"1648082493.6843-40",
"1648082493.8385-41",
"1648082493.993-42",
"1648082494.1467-43",
"1648082494.3001-44",
"1648082494.4539-45",
"1648082494.6082-46",
"1648082494.7622-47",
"1648082494.9166-48",
"1648082495.07-49",
"1648082495.225-50",
"1648082495.3792-51",
"1648082495.5649-52",
"1648082495.7217-53",
"1648082495.8753-54",
"1648082496.0294-55",
"1648082496.183-56",
"1648082496.3377-57",
"1648082496.4996-58",
"1648082496.6563-59",
"1648082496.81-60",
"1648082496.9644-61",
"1648082497.1185-62",
"1648082497.281-63",
"1648082497.443-64",
"1648082497.5985-65",
"1648082497.756-66",
"1648082498.0996-67",
"1648082498.3198-68",
"1648082498.4748-69",
"1648082498.6612-70",
"1648082498.8199-71",
"1648082498.9765-72",
"1648082499.1324-73",
"1648082499.2901-74",
"1648082499.4454-75",
"1648082499.5989-76",
"1648082499.7547-77",
"1648082499.9086-78",
"1648082500.0621-79",
"1648082500.2152-80",
"1648082500.3692-81",
"1648082500.5727-82",
"1648082500.727-83",
"1648082500.8808-84",
"1648082501.0356-85",
"1648082501.1907-86",
"1648082501.3457-87",
"1648082501.5001-88",
"1648082501.6562-89",
"1648082501.8094-90",
"1648082501.9653-91",
"1648082502.1198-92",
"1648082502.2788-93",
"1648082502.444-94",
"1648082502.5981-95",
"1648082502.7732-96",
"1648082503.0687-97",
"1648082503.247-98",
"1648082503.4098-99",
"1648082503.576-100"
];
$getStartTime = microtime(true);
foreach ($documentsIds as $id) {
$collection->get($id);
}
$getEndTime = microtime(true);
echo sprintf('Read (get) time for 100 documents: %s seconds', round($getEndTime - $getStartTime, 2)) . PHP_EOL;
echo sprintf('Average read time for a document: %s seconds', round(($getEndTime - $getStartTime) / 100, 2)) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment