Please note that Sahi's development and support teams do not actively monitor this. This is more a community forum. If you are a Sahi Pro user looking for commercial support, please email support @ sahipro.com with your query.

It is possible to display with Sahi the run time for a specific script

mihaimacariemihaimacarie Members
edited 9:11PM in Sahi - Open Source
Hello there,

It is possible to display with Sahi the run time for a specific script ? I mean ... to define a timer.

e.g. (PHP code)

$start_time = microtime(true);

// source code ...
// source code ...

$end_time = microtime(true);

$run_time = $end_time - $start_time;
echo "Run Time = ".$run_time;

Thanks,
Mihai

Comments

  • pankaj.nithpankaj.nith Members
    edited January 2009
    Hi,

    Don't know if there is some way to get script execution time. Here is what I do:
    function currentTime(){
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
    
        if(hours > 11){
            return hours + ":" + minutes + " PM";
        } 
        else {
            return hours + ":" + minutes + " AM";
        }
    }
    
    And the idea is to call the function(within log statement) at the very begining of the script and after the last step of the script. The difference of time will give you the time of execution. :)
    You can also add seconds to it. Same logic can be extended further to do time calculations for finding difference between the two times.

    Regards,
    Pankaj.
  • narayannarayan Administrators
    You could do:
    $start_time = new Date();
    
       // source code ... 
       // source code ... 
    
    $end_time = new Date();
    
    $run_time = $end_time - $start_time;
    _log("Run Time = " + $run_time);
    
  • Great! I never tried getting the difference :)
  • Thanks a lot Pankaj and Narayan for the reply.

    Pankaj, can you explain to me, if is possible, how I integrate currentTime() javascript function into a Sahi script? I don't know how to call the function into a _log() statement.

    Narayan, your code is working fine, thank you again for your answer.

    Regards,
    Mihai
  • Hi,

    Sure. Use this function instead:
    function currentTime(){
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        
        if (seconds < 10){
            seconds = "0" + seconds;
        }
    
    
        if(hours > 11){
            return hours + ":" + minutes + ":" + seconds + " PM";
        } 
        else {
            return hours + ":" + minutes + ":" + seconds + " AM";
        }
    }
    
    And this is how i use it:
    _log("==== Script Execution Started At " + currentTime() + " ====", "success");
      
       // source code ... 
       // source code ... 
    
    _log("==== Script Execution Completed At " + currentTime() + " ====", "success");
    
    I use an older version of sahi (without rhino) and it works fine there. Let me know if this function has some problems with the newer version.

    Regards,
    Pankaj.
  • It seems that I have some problems with Sahi; I'm using the build version 2008-10-26.

    When I try to run the script using currentTime() function, Sahi Controller returns the following error:

    _log("==== Script Execution Started At " + currentTime() + " ====", "success"); ReferenceError: currentTime is not defined
    ...

    - but there is not a big problem, I modified Narayan's function for my environment and everything working fine. Here is the function:
    function Timer ($start_time) {
    
      // Note: at beginning of every Sahi script define the variable:
      // $start_time = new Date(); 
       
      $end_time = new Date();
      $run_time = $end_time - $start_time;
      
      $final_time = ($run_time / 1000); 
      
      
        if ($final_time > 60 ) {
        
         $final_time = ($final_time/60);
         
         $minutes = Math.floor ($final_time); // extract the integer
         $seconds = ($final_time - $minutes)*60;
        
        _log ("Run Time = " + $minutes + " minutes " + $seconds + " seconds");
        
        } else 
              {
                _log("Run Time = " + $final_time + " seconds");
              }
    }
    
    Function Call:
    $start_time = new Date(); 
    
    // Script ... 
    // Script ... 
    
    Timer ($start_time);
    
    Thanks a lot for the support :)

    Regards,
    Mihai
  • Ya there is some problem. Looking into it. will revert as soon as i get the solution. May be we need to tweak how we define the function.
  • Hi,
    Below is a working demo code that worked on sahi rhino version:
    function currentTime(){
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        
        if (seconds < 10){
            seconds = "0" + seconds;
        }
    
    
        if(hours > 11){
            return hours + ":" + minutes + ":" + seconds + " PM";
        } 
        else {
            return hours + ":" + minutes + ":" + seconds + " AM";
        }
    }
    
    var $start_Time = currentTime();
    
    _log($start_Time, "custom1");
    
    for (var $i = 0; $i < 100; $i++)
        _log($i, "custom2");
        
    var $end_Time = currentTime();
    
    _log($end_Time, "custom1");
    
    It took 20 seconds to execute on my machine.
    Hope this helps you :)

    Regards,
    Pankaj.
  • Hey, nice work, Sahi forum available on my mobile device! I'll try the new function! Regards, Mihai
  • Hello Pankaj,

    The last function display correctly the time.

    Thank you,
    Mihai
  • pankaj.nithpankaj.nith Members
    edited February 2009
    Hi Friends,

    Thought of sharing a function ( timeDifference() ) that I just jotted down to calculate the difference of two times to calculate the execution time of a script. this we can use in conjunction with the above mentioned function currentTime().
    Will explain with an example:
    function currentTime(){
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        
        if (seconds < 10){
            seconds = "0" + seconds;
        }
    
    
        if(hours > 11){
            return hours + ":" + minutes + ":" + seconds + " PM";
        } 
        else {
            return hours + ":" + minutes + ":" + seconds + " AM";
        }
    }
    
    function timeDifference($t2, $t1){
        var $time1 = $t1.split(":");
    
        var $time2 = $t2.split(":");
    
        var $time = (parseInt($time2[0])*3600 + parseInt($time2[1])*60 + parseInt($time2[2]))  - (parseInt($time1[0])*3600 + parseInt($time1[1])*60 + parseInt($time1[2]));
    
        var $timeDiff = new Array(3);
        $timeDiff[0] = parseInt($time/3600);//hours
        $timeDiff[1] = parseInt(($time%3600)/60);//minutes
        $timeDiff[2] = ($time%3600)%60;//seconds
    
        return $timeDiff;
    }
    
    
    var $start_time = currentTime();
    _log("==== Script Execution Started At " + $start_time + " ====", "success");
    
    for (var $i = 0; $i < 100; $i++)
        _log($i, "success");
    
    var $end_time = currentTime();
    _log("==== Script Execution Completed at: " + $end_time + " ====", "success");
    var $execution_time = new Array(3);
    $execution_time = timeDifference($end_time, $start_time);
    _log("Time taken for script execution = " + $execution_time[0] + "Hrs " + $execution_time[1] + "Mins " + $execution_time[2] + "Secs", "success");
    
    You can use/modify it as per your requirements :)

    Regards,
    Pankaj.
  • Hello Pankaj,

    The last function posted, was tested and is working fine.

    Regards,
    Mihai
  • Dear All,
    I tried the above fn but getting this error
    ReferenceError: "_log" is not defined.
    can some one please explain , what am I missing here ?
    Regards,
    Abdul
  • I think I am missing the fn call for the above function. Can come one please tell me the function call for the above function
Sign In or Register to comment.