query((new SqlFieldsQuery('SELECT cvalue FROM concurrency_cache WHERE ckey = ?'))->setSchema('PUBLIC')->setArgs($key))->getAll(); if(count($res) > 0) { return $res[0][0]; } else { return null; } } function put($cache, $key, $value) { $cache->query((new SqlFieldsQuery('MERGE INTO concurrency_cache(ckey,cvalue) VALUES(?,?)'))->setSchema('PUBLIC')->setArgs($key, $value))->getAll(); } function add($cache, $key, $delta) { $cache->query((new SqlFieldsQuery('UPDATE concurrency_cache SET cvalue = cvalue + ? WHERE ckey = ?'))->setSchema('PUBLIC')->setArgs($delta, $key))->getAll(); } define('NTHREADS', 10); // 100 does not work define('NOPS', 10000); function test1() { $cli = new Client(); $cli->connect(new ClientConfiguration('localhost:10800')); $cache = $cli->getOrCreateCache('dummy', (new CacheConfiguration())); $cache->query((new SqlFieldsQuery('CREATE TABLE IF NOT EXISTS concurrency_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))'))->setSchema('PUBLIC'))->getAll(); for($i = 0; $i < NOPS/NTHREADS; $i++) { $hot = get($cache, 'hot'); $hot = $hot + 1; put($cache, 'hot', $hot); } $cli->disconnect(); } function test6() { $cli = new Client(); $cli->connect(new ClientConfiguration('localhost:10800')); $cache = $cli->getOrCreateCache('dummy', (new CacheConfiguration())); $cache->query((new SqlFieldsQuery('CREATE TABLE IF NOT EXISTS concurrency_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))'))->setSchema('PUBLIC'))->getAll(); for($i = 0; $i < NOPS/NTHREADS; $i++) { $done = FALSE; while(!$done) { try { add($cache, 'hot', 1); $done = TRUE; } catch(Exception $ex) { // retry } } } $cli->disconnect(); } function dispatch($me, $f) { $cmd = sprintf('%s -d include_path=%s %s %s', PHP_BINARY, get_include_path(), $me, $f); shell_exec($cmd); } function test($lbl, $me, $f) { $cli = new Client(); $cli->connect(new ClientConfiguration('localhost:10800')); $cache = $cli->getOrCreateCache('dummy', (new CacheConfiguration())); $cache->query((new SqlFieldsQuery('CREATE TABLE IF NOT EXISTS concurrency_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))'))->setSchema('PUBLIC'))->getAll(); $t1 = gettimeofday(TRUE); put($cache, 'hot', 0); $t = array(); for($i = 0; $i < NTHREADS; $i++) { $t[] = new Thread(); $t[$i]->addFunctionTask('dispatch', $me, $f); } for($i = 0; $i < NTHREADS; $i++) { $t[$i]->start(); } for($i = 0; $i < NTHREADS; $i++) { $t[$i]->join(); } $hot = get($cache, 'hot'); $t2 = gettimeofday(TRUE); echo sprintf("%s : %d = %d (%d ms)\r\n", $lbl, $hot, NOPS, (int)(($t2 - $t1) * 1000)); $cli->disconnect(); } if(count($argv) < 2) { test('No handling', $argv[0], 'test1'); // transaction with retry not available test('Update with retry', $argv[0], 'test6'); } else { $argv[1](); } ?>