diff options
| author | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2026-07-12 15:51:52 +0300 |
|---|---|---|
| committer | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2026-07-12 15:51:52 +0300 |
| commit | 02608271bb2a9f3a65ed536984d306ee14b48e34 (patch) | |
| tree | 3f23ec48a694ec014e845c4f70d9b5f1c065403c /tests/compare | |
| download | kalassa-02608271bb2a9f3a65ed536984d306ee14b48e34.tar.gz kalassa-02608271bb2a9f3a65ed536984d306ee14b48e34.tar.bz2 kalassa-02608271bb2a9f3a65ed536984d306ee14b48e34.zip | |
Diffstat (limited to 'tests/compare')
| -rwxr-xr-x | tests/compare/compress.php | 80 | ||||
| -rwxr-xr-x | tests/compare/hashcost.php | 54 | ||||
| -rwxr-xr-x | tests/compare/md5-v-sha1.php | 18 | ||||
| -rwxr-xr-x | tests/compare/memcached-v-filecache.php | 34 | ||||
| -rwxr-xr-x | tests/compare/redis-v-filecache.php | 35 | ||||
| -rwxr-xr-x | tests/compare/serialize-v-jsonencode.php | 35 | ||||
| -rwxr-xr-x | tests/compare/sha1-v-sha256.php | 23 |
7 files changed, 279 insertions, 0 deletions
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 @@ +<?php + +echo "test started... "; +ob_flush(); + +Benchmark::add_spot('query-10000-products'); +$db = Registry::use('database'); +$products = $db->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 "<pre>gzcompress(1) +compressed {$originalLength} to {$compressLength} or "; +echo ($originalLength-$compressLength)*100/$originalLength ."%\n</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 "<pre>gzcompress(4) +compressed {$originalLength} to {$compressLength} or "; +echo ($originalLength-$compressLength)*100/$originalLength ."%\n</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 "<pre>gzcompress(6) +compressed {$originalLength} to {$compressLength} or "; +echo ($originalLength-$compressLength)*100/$originalLength ."%\n</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 @@ +<?php +/** + * Password Hash Cost Calculator + * + * Set the ideal time that you want a password_hash() call to take and this + * script will keep testing until it finds the ideal cost value and let you + * know what to set it to when it has finished + * + * credit: https://gist.github.com/Antnee/a072b7a3c59334bf1872 + */ + +// Milliseconds that a hash should take (ideally) +$mSec = 100; + +$password = 'MyT3ST_P4$$w0rD'; + +echo '<pre>'; +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 "</pre>"; + +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 @@ +<?php + +echo "test started... "; +ob_flush(); + +Benchmark::add_spot('100.000 x md5'); +for($i = 0 ; $i<100000 ; $i++) $x = md5($i); + + +Benchmark::add_spot('100.000 x sha'); +for($i = 0 ; $i<100000 ; $i++) $x = sha1($i); + +Benchmark::add_spot('end'); +echo(Benchmark::report(BENCH_REPORT_COMMENT)); +echo "test completed!"; + +die(); +
\ No newline at end of file diff --git a/tests/compare/memcached-v-filecache.php b/tests/compare/memcached-v-filecache.php new file mode 100755 index 0000000..4ffdfed --- /dev/null +++ b/tests/compare/memcached-v-filecache.php @@ -0,0 +1,34 @@ +<?php +echo "test started... "; +ob_flush(); + +Benchmark::add_spot('query-10000-products'); +$db = Registry::use('database'); +$products = $db->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 @@ +<?php +echo "test started... "; +ob_flush(); + +Benchmark::add_spot('query-10000-products'); +$db = Registry::use('database'); +$products = $db->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 @@ +<?php +echo "test started... "; +ob_flush(); + +Benchmark::add_spot('query-10000-products'); +$db = Registry::use('database'); +$products = $db->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 @@ +<?php + +echo "test started... "; +echo '<pre>'; +echo "\nsha1: ". sha1('hello'); +echo "\nsha256: ". hash('sha256', 'hello'); +echo '</pre>'; +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 |
