diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
| commit | 7076343338ae3439f3c86f01144818abe8c31978 (patch) | |
| tree | 794abb1e6b8f821091fd095341885496b6dad51d /tests/compare | |
| parent | 47cbb529f5723b246125ae083a193e11481b89ef (diff) | |
| download | classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.gz classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.bz2 classroom-7076343338ae3439f3c86f01144818abe8c31978.zip | |
add container helpers; constuct public directory-tree
Diffstat (limited to 'tests/compare')
| -rw-r--r-- | tests/compare/compress.php | 80 | ||||
| -rw-r--r-- | tests/compare/hashcost.php | 54 | ||||
| -rw-r--r-- | tests/compare/md5-v-sha1.php | 18 | ||||
| -rw-r--r-- | tests/compare/memcached-v-filecache.php | 35 | ||||
| -rw-r--r-- | tests/compare/redis-v-filecache.php | 35 | ||||
| -rw-r--r-- | tests/compare/serialize-v-jsonencode.php | 35 | ||||
| -rw-r--r-- | tests/compare/sha1-v-sha256.php | 22 |
7 files changed, 279 insertions, 0 deletions
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 @@ +<?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'); +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 @@ +<?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 100644 index 0000000..44b5148 --- /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'); +Benchmark::render_report('code'); + +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 100644 index 0000000..539b0c5 --- /dev/null +++ b/tests/compare/memcached-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('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 @@ +<?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'); +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 @@ +<?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'); +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 @@ +<?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'); +Benchmark::render_report('code'); + +echo "test completed!"; +die(); +
\ No newline at end of file |
