PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of andrea bernardi   Little Benchmark   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Working example on how one can use Little Benchmark
Class: Little Benchmark
Measure RAM amount, total and partial script time.
Author: By
Last change: Changes due to massive refactoring of main class.
Date: 5 years ago
Size: 1,407 bytes
 

Contents

Class file image Download
<?php
// include class
include_once("class.LittleBenchmark.php");

// start global timer when creating an instance
$obj = new LittleBenchmark;

// start partial timer
$timer_1 = $obj->start();
for (
$i=0; $i<500000; $i++) {
   
$hugeArray[$i] = mt_rand(1000,99999);
}
// stop partial timer
$obj->stop($timer_1, "huge array filled with random values");

unset(
$hugeArray); // free RAM

// start partial timer
$timer_2 = $obj->start();
$hugeString='';
for (
$i=0;$i<5000000;$i++) {
   
$hugeString .= 'a';
}
$obj->stop($timer_2, "filled a string with 5,000,000 'a'");

unset(
$hugeString); // free RAM

// stop global timer and save last message
$obj->stopGlobalTimer("Total execution time of this script.");

// output in JSON format
header("Content-type: application/json");
echo
json_encode($obj->events);


// the output is valid JSON like the following:
/*

[{
    "LABEL": "Total execution time of this script.",
    "TIME_START": 1551090327.199855,
    "TIME_END": 1551090327.227312,
    "EXEC_TIME": 0.027457,
    "RAM_USAGE": "2 mb"
}, {
    "LABEL": "huge array filled with random values",
    "TIME_START": 1551090327.199862,
    "TIME_END": 1551090327.203212,
    "EXEC_TIME": 0.00335,
    "RAM_USAGE": "6 mb"
}, {
    "LABEL": "filled a string with 500,000 'a'",
    "TIME_START": 1551090327.203708,
    "TIME_END": 1551090327.227308,
    "EXEC_TIME": 0.0236,
    "RAM_USAGE": "2 mb"
}]

*/