Login   Register  
PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  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: changed a comment
Date: 2 years ago
Size: 1,403 bytes
 

Contents

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


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


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

unset(
$hugeArray); // free RAM


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

unset(
$hugeString); // free RAM

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

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


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

[
    {
        "EVENT": "GLOBAL TIMER STARTED",
        "DELTA": "0 s",
        "RAM_USAGE": "768 kb"
    },
    {
        "EVENT": "huge array filled with random values",
        "DELTA": "0.090872 s",
        "RAM_USAGE": "8 mb"
    },
    {
        "EVENT": "filled a string with 500,000 'a'",
        "DELTA": "0.131289 s",
        "RAM_USAGE": "1.75 mb"
    },
    {
        "EVENT": "Below is the total time execution of this script.",
        "DELTA": "0.226258 s",
        "RAM_USAGE": "1.25 mb"
    }
]

*/

?>