From 7076343338ae3439f3c86f01144818abe8c31978 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Sun, 12 Mar 2023 07:16:23 +0200 Subject: add container helpers; constuct public directory-tree --- tests/compare/compress.php | 80 ++++++++++++++++++++++++++++++++ tests/compare/hashcost.php | 54 +++++++++++++++++++++ tests/compare/md5-v-sha1.php | 18 +++++++ tests/compare/memcached-v-filecache.php | 35 ++++++++++++++ tests/compare/redis-v-filecache.php | 35 ++++++++++++++ tests/compare/serialize-v-jsonencode.php | 35 ++++++++++++++ tests/compare/sha1-v-sha256.php | 22 +++++++++ 7 files changed, 279 insertions(+) create mode 100644 tests/compare/compress.php create mode 100644 tests/compare/hashcost.php create mode 100644 tests/compare/md5-v-sha1.php create mode 100644 tests/compare/memcached-v-filecache.php create mode 100644 tests/compare/redis-v-filecache.php create mode 100644 tests/compare/serialize-v-jsonencode.php create mode 100644 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 100644 index 0000000..74fdea1 --- /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');
+Benchmark::render_report('code');
+
+echo "test completed!";
+die();
diff --git a/tests/compare/hashcost.php b/tests/compare/hashcost.php
new file mode 100644
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 100644 index 0000000..44b5148 --- /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'); +Benchmark::render_report('code'); + +echo "test completed!"; +die(); + + diff --git a/tests/compare/redis-v-filecache.php b/tests/compare/redis-v-filecache.php new file mode 100644 index 0000000..e59c390 --- /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'); +Benchmark::render_report('code'); + +echo "test completed!"; +die(); + + diff --git a/tests/compare/serialize-v-jsonencode.php b/tests/compare/serialize-v-jsonencode.php new file mode 100644 index 0000000..ba9ccc9 --- /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'); +Benchmark::render_report('code'); + +echo "test completed!"; +die(); \ No newline at end of file diff --git a/tests/compare/sha1-v-sha256.php b/tests/compare/sha1-v-sha256.php new file mode 100644 index 0000000..4d10e7f --- /dev/null +++ b/tests/compare/sha1-v-sha256.php @@ -0,0 +1,22 @@ +'; +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'); +Benchmark::render_report('code'); + +echo "test completed!"; +die(); + \ No newline at end of file -- cgit v1.2.3