Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
5 posts Page 1 of 1
UPDATE 2020:
For individual self-closed tags or tags with blocks of code I developed an addon that measures time of execution and RAM used via parameter get_time='1' which automatically works for 98% of tags.

get-time.gif (11.58 KiB) Viewed 147 times

Ok, this is the undocumented feature I like to discover and try. I give it to you as a present for the new year :lol: (with duly respect to KK, of course, who prepared this fun)

k-test-machine.png (3.5 KiB) Viewed 147 times

I have tried to measure speed of loading and generating pages without knowing this trick. So, if you are looking for some more fun with Couch - this is for you. As a bonus, you'd see the number of queries, perfomed while showing the php template.

1st. Check cms.php in /couchdir. Scroll to the bottom, line 340.
Check this part of code and change it this way:
Code: Select all
            if( defined('K_IS_MY_TEST_MACHINE') ){
       //         $html .= "\n<!-- in: ".k_timer_stop()." -->\n";
                $html .= "\n<p> in: ".k_timer_stop()." </p>\n";
       //         $html .= "\n<!-- Queries: ".$DB->queries." -->\n";
                $html .= "\n<p> Queries: ".$DB->queries." </p>\n";

Now, to be able to turn this on and off at will, leaving the core couch file untouched,
let's add an option to config.php, to the bottom:

Code: Select all
//Show seconds and queries: 
   //cms.php, line 340.
    define( 'K_IS_MY_TEST_MACHINE', 1 );

SO, to disable this - comment out the last line. It is not enough to put 0 there, the whole thing should be commented with //.

With this, you'd have every page with something like this:

in: 1.124 sec

Queries: 16

Happy celebrations :)
// :mrgreen:
Another more suitable way to measure speeds of PARTS OF CODE.

Add this to /couch/addons/kfunctions.php
Code: Select all

// ... block of code ...
//<cms:php>time_elapsed('DONE WITH THAT');</cms:php>
function time_elapsed($comment)

        static $time_elapsed_last = null;
        static $time_elapsed_start = null;

         $unit="s"; $scale=1000000; // output in seconds
        // $unit="ms"; $scale=1000; // uncomment to have output in milliseconds
        // $unit="ms"; $scale=1000; // uncomment to have output in microseconds

        $now = microtime(true);

        if ($time_elapsed_last != null) {
         echo "\n";
            echo '<!-- ';
            echo "$comment: Time elapsed: ";
            echo round(($now - $time_elapsed_last)*1000000)/$scale;
            echo " $unit, total time: ";
            echo round(($now - $time_elapsed_start)*1000000)/$scale;
            echo " $unit -->";
            echo "\n";
         if( $comment ){
            $comment=$comment.': ';
            echo "<p><b>$comment</b> Time elapsed: ";
            echo round(($now - $time_elapsed_last)*1000000)/$scale;
            echo " $unit </p>";
        } else {

        $time_elapsed_last = $now;

This makes available the function time_elapsed. It must be placed two times: somewhere before the piece of code, so some variables get created for the first time to store current time. Then, after the piece of code it can be placed again and now it will show time of running of what's was between.. Output can be customized for the units and a message shown.
Have fun!

How to use:
Code: Select all
<cms:php>time_elapsed('');</cms:php> <cms:ignore>RUN FOR THE FIRST TIME</cms:ignore>
... block of code ...
<cms:php>time_elapsed('1st checkpoint');</cms:php>
... block of code ...
<cms:php>time_elapsed('2nd checkpoint');</cms:php>
... block of code ...
<cms:php>time_elapsed('3d checkpoint');</cms:php>
Another way, which I have recently found in my collection :D
Only result will be visible :)

Code: Select all

<cms:set start="<cms:php>echo $time_start = microtime(true); </cms:php>" />

                ...some code...or maybe the whole page..

<p>Page generated in: <cms:sub "<cms:php>echo $time_start = microtime(true); </cms:php>" start /></p>

This is v useful thanks trendoman.

Have used this to identify slow functions and have reduced page load times by a factor of 2
Yes it really is useful, thank you trendoman
5 posts Page 1 of 1

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 1 guest