summaryrefslogtreecommitdiff
path: root/tests/compare
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
commit059e0d95d0c28bc5060e87e146eaf7411f51bf90 (patch)
tree7c21ac432f16dde2329a0d5954d70711eceb48e6 /tests/compare
parent26cd8ee99659ef1926c96a049c93645ffc9b169d (diff)
downloadgyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.gz
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.bz2
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.zip
skeleton commit; based on an anom project
Diffstat (limited to 'tests/compare')
-rwxr-xr-xtests/compare/compress.php80
-rwxr-xr-xtests/compare/hashcost.php54
-rwxr-xr-xtests/compare/md5-v-sha1.php18
-rwxr-xr-xtests/compare/memcached-v-filecache.php35
-rwxr-xr-xtests/compare/redis-v-filecache.php35
-rwxr-xr-xtests/compare/serialize-v-jsonencode.php35
-rwxr-xr-xtests/compare/sha1-v-sha256.php22
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..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 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..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 100755
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 100755
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 100755
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 100755
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