From 02608271bb2a9f3a65ed536984d306ee14b48e34 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Sun, 12 Jul 2026 15:51:52 +0300 Subject: initialize repository; add docker config; migrate configuration to new specs --- tests/compare/compress.php | 80 ++++++++++++++++++++++++++++++++ tests/compare/hashcost.php | 54 +++++++++++++++++++++ tests/compare/md5-v-sha1.php | 18 +++++++ tests/compare/memcached-v-filecache.php | 34 ++++++++++++++ tests/compare/redis-v-filecache.php | 35 ++++++++++++++ tests/compare/serialize-v-jsonencode.php | 35 ++++++++++++++ tests/compare/sha1-v-sha256.php | 23 +++++++++ 7 files changed, 279 insertions(+) create mode 100755 tests/compare/compress.php create mode 100755 tests/compare/hashcost.php create mode 100755 tests/compare/md5-v-sha1.php create mode 100755 tests/compare/memcached-v-filecache.php create mode 100755 tests/compare/redis-v-filecache.php create mode 100755 tests/compare/serialize-v-jsonencode.php create mode 100755 tests/compare/sha1-v-sha256.php (limited to 'tests/compare') diff --git a/tests/compare/compress.php b/tests/compare/compress.php new file mode 100755 index 0000000..f36baa2 --- /dev/null +++ b/tests/compare/compress.php @@ -0,0 +1,80 @@ +runQuery("SELECT * FROM products LIMIT 10000",[]); + + + +$originalLength = $compressLength = 0; +Benchmark::add_spot('gzcompress(1)-10000-products'); +foreach($products as $product) { + $original = serialize($product); + $compress = gzcompress($original, 1); + $originalLength = strlen($original); + $compressLength = strlen($compress); +} +echo "
gzcompress(1)
+compressed {$originalLength} to {$compressLength} or ";
+echo ($originalLength-$compressLength)*100/$originalLength ."%\n";
+
+
+
+$originalLength = $compressLength = 0;
+Benchmark::add_spot('gzcompress(4)-10000-products');
+foreach($products as $product) {
+    $original = serialize($product);
+    $compress = gzcompress($original, 4);
+    $originalLength = strlen($original);
+    $compressLength = strlen($compress);
+}
+echo "
gzcompress(4)
+compressed {$originalLength} to {$compressLength} or ";
+echo ($originalLength-$compressLength)*100/$originalLength ."%\n";
+
+
+
+$originalLength = $compressLength = 0;
+Benchmark::add_spot('gzcompress(6)-10000-products');
+foreach($products as $product) {
+    $original = serialize($product);
+    $compress = gzcompress($original, 6);
+    $originalLength = strlen($original);
+    $compressLength = strlen($compress);
+}
+echo "
gzcompress(6)
+compressed {$originalLength} to {$compressLength} or ";
+echo ($originalLength-$compressLength)*100/$originalLength ."%\n";
+
+
+
+$cache = Registry::use('cache');
+
+Benchmark::add_spot('Cache-save-10000-products');
+foreach($products as $product) {
+    $cache->set('cache.sav.'.$product['ID'],  serialize($product), 3600);   
+}
+
+
+
+Benchmark::add_spot('gz(ser(),6)+Save-10000-products');
+foreach($products as $product) {
+    $cache->set('zx.ser.sav.'.$product['ID'],  gzcompress(serialize($product), 6), 3600);   
+}
+
+$maxLen = 0;
+Benchmark::add_spot('sha1(serialize())-10000-products');
+foreach($products as $product) {
+    $original = sha1(serialize($product));
+    $maxlen = $maxLen > strlen($original) ? $maxLen : strlen($original);
+}
+
+
+Benchmark::add_spot('end');
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
+
+echo "test completed!";
+die();
diff --git a/tests/compare/hashcost.php b/tests/compare/hashcost.php
new file mode 100755
index 0000000..7a1401f
--- /dev/null
+++ b/tests/compare/hashcost.php
@@ -0,0 +1,54 @@
+';
+echo "\nPassword Hash Cost Calculator\n\n";
+echo "Testing BCRYPT hashing the password '$password'\n\n";
+echo "We're going to run until the time to generate the hash takes longer than {$mSec}ms\n";
+
+$cost = 3;
+do {
+    $cost++;
+    echo "\nTesting cost value of $cost: ";
+    $time = benchmark($password, $cost);
+    echo "... took $time";
+} while ($time < ($mSec/1000));
+
+echo "\n\nIdeal cost is $cost\n";
+echo "\nRunning 100 times to check the average:\n";
+
+$start = microtime(true);
+$times = [];
+for ($i=1;$i<=100;$i++) {
+    echo "\r$i/100";
+    $times[] = benchmark($password, $cost);
+}
+
+echo "\n\ndone benchmarking in ".(microtime(true)-$start)."\n";
+
+echo "\nSlowest time: ".max($times);
+echo "\nFastest time: ".min($times);
+echo "\nAverage time: ".(array_sum($times)/count($times));
+
+echo "\n\nFinished\n";
+echo "
"; + +function benchmark($password, $cost=4) +{ + $start = microtime(true); + password_hash($password, PASSWORD_BCRYPT, ['cost'=>$cost]); + return microtime(true) - $start; +} diff --git a/tests/compare/md5-v-sha1.php b/tests/compare/md5-v-sha1.php new file mode 100755 index 0000000..74d0371 --- /dev/null +++ b/tests/compare/md5-v-sha1.php @@ -0,0 +1,18 @@ +runQuery("SELECT * FROM products LIMIT 10000",[]); + +Benchmark::add_spot('memcached-set-start'); +foreach($products as $product) { + MemcachedCache::set('prod'.$product['ID'], $product, 600); +} + +Benchmark::add_spot('memcached-get-start'); +foreach($products as $product) { + $x = MemcachedCache::get('prod'.$product['ID']); +} + +Benchmark::add_spot('filecache-set-start'); +foreach($products as $product) { + FileCache::set('prod'.$product['ID'], $product, 600); +} + +Benchmark::add_spot('filecache-get-start'); +foreach($products as $product) { + $x = FileCache::get('prod'.$product['ID']); +} + +Benchmark::add_spot('end'); +echo(Benchmark::report(BENCH_REPORT_COMMENT)); +echo "test completed!"; + +die(); + diff --git a/tests/compare/redis-v-filecache.php b/tests/compare/redis-v-filecache.php new file mode 100755 index 0000000..3cb6536 --- /dev/null +++ b/tests/compare/redis-v-filecache.php @@ -0,0 +1,35 @@ +runQuery("SELECT * FROM products LIMIT 10000",[]); + +Benchmark::add_spot('redis-set-start'); +foreach($products as $product) { + RedisCache::set('prod'.$product['ID'], $product, 600); +} + +Benchmark::add_spot('redis-get-start'); +foreach($products as $product) { + $x = RedisCache::get('prod'.$product['ID']); +} + +Benchmark::add_spot('filecache-set-start'); +foreach($products as $product) { + FileCache::set('prod'.$product['ID'], $product, 600); +} + +Benchmark::add_spot('filecache-get-start'); +foreach($products as $product) { + $x = FileCache::get('prod'.$product['ID']); +} + +Benchmark::add_spot('end'); +echo(Benchmark::report(BENCH_REPORT_COMMENT)); + +echo "test completed!"; +die(); + + diff --git a/tests/compare/serialize-v-jsonencode.php b/tests/compare/serialize-v-jsonencode.php new file mode 100755 index 0000000..c66c21b --- /dev/null +++ b/tests/compare/serialize-v-jsonencode.php @@ -0,0 +1,35 @@ +runQuery("SELECT * FROM products LIMIT 10000",[]); + +Benchmark::add_spot('serialize-start'); +foreach($products as $product) { + $ser = serialize($product); +} + +Benchmark::add_spot('serialize+unserialize-start'); +foreach($products as $product) { + $ser = serialize($product); + $ori = unserialize($ser); +} + +Benchmark::add_spot('jsonencode-start'); +foreach($products as $product) { + $json = json_encode($product); +} + +Benchmark::add_spot('jsonencode+decode-start'); +foreach($products as $product) { + $json = json_encode($product); + $ori = json_decode($json); +} + +Benchmark::add_spot('end'); +echo(Benchmark::report(BENCH_REPORT_COMMENT)); +echo "test completed!"; + +die(); diff --git a/tests/compare/sha1-v-sha256.php b/tests/compare/sha1-v-sha256.php new file mode 100755 index 0000000..d4bf5dc --- /dev/null +++ b/tests/compare/sha1-v-sha256.php @@ -0,0 +1,23 @@ +'; +echo "\nsha1: ". sha1('hello'); +echo "\nsha256: ". hash('sha256', 'hello'); +echo '
'; +ob_flush(); + +Benchmark::add_spot('100.000 x sha1'); +for($i = 0 ; $i<100000 ; $i++) $x = sha1($i); + + +Benchmark::add_spot('100.000 x sha256'); +for($i = 0 ; $i<100000 ; $i++) $x = hash('sha256', $i); + +Benchmark::add_spot('end'); + +echo (Benchmark::report(BENCH_REPORT_COMMENT)); + +echo "test completed!"; +die(); + \ No newline at end of file -- cgit v1.2.3