Created
August 19, 2018 14:03
-
-
Save MahdiY/3d653c03a692c2363e9c787bb1ce930a to your computer and use it in GitHub Desktop.
CPU & RAM Benchmarking in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Script start | |
$usage_start = getrusage(); | |
echo "PHP RAM Usage 1: " . convert(memory_get_usage()) . "\n"; | |
ob_start(); | |
// Start Code | |
$result = []; | |
for ($i = 0; $i < 10000000; $i++) { | |
switch (true) { | |
case $i == 0: | |
echo 0; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 0 && $i < 5: | |
echo 5; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 5 && $i < 10: | |
echo 10; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 10 && $i < 15: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 15 && $i < 20: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 20 && $i < 25: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 25 && $i < 30: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 30 && $i < 35: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 35 && $i < 40: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 15 && $i < 110: | |
echo 10; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 110 && $i < 115: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 115 && $i < 120: | |
echo 15; | |
break; | |
case $i > 120 && $i < 125: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 125 && $i < 130: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 130 && $i < 135: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
case $i > 135 && $i < 140: | |
echo 15; | |
$result[] = str_repeat("Hello", 4242); | |
break; | |
} | |
} | |
ob_clean(); | |
echo "PHP RAM Usage 2: " . convert(memory_get_usage()) . "\n"; | |
unset($result); | |
echo "PHP RAM Usage 3: " . convert(memory_get_usage()) . "\n"; | |
// Script end | |
function convert($size) | |
{ | |
$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PT'); | |
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; | |
} | |
function check($ru, $rus, $index) | |
{ | |
return ($ru["ru_$index.tv_sec"] * 1000 + intval($ru["ru_$index.tv_usec"] / 1000)) | |
- ($rus["ru_$index.tv_sec"] * 1000 + intval($rus["ru_$index.tv_usec"] / 1000)); | |
} | |
$usage_end = getrusage(); | |
echo "This process used " . check($usage_end, $usage_start, "utime") . | |
" ms for its computations.\n"; | |
echo "It spent " . check($usage_end, $usage_start, "stime") . | |
" ms in system calls.\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Script start | |
$usage_start = getrusage(); | |
echo "PHP RAM Usage - Start: " . convert(memory_get_usage()) . "\n"; | |
ob_start(); | |
// Start Code | |
/* Your Code */ | |
ob_clean(); | |
echo "PHP RAM Usage - End: " . convert(memory_get_usage()) . "\n"; | |
// Script end | |
function convert($size) | |
{ | |
$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PT'); | |
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; | |
} | |
function check($ru, $rus, $index) | |
{ | |
return ($ru["ru_$index.tv_sec"] * 1000 + intval($ru["ru_$index.tv_usec"] / 1000)) | |
- ($rus["ru_$index.tv_sec"] * 1000 + intval($rus["ru_$index.tv_usec"] / 1000)); | |
} | |
$usage_end = getrusage(); | |
echo "This process used " . check($usage_end, $usage_start, "utime") . | |
" ms for its computations.\n"; | |
echo "It spent " . check($usage_end, $usage_start, "stime") . | |
" ms in system calls.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment