My $client recently required me to find out where Template was spending
its time.  Here's what I hacked out.  Still missing: sum total.  But
view this as a WorkInProgress...

    BEGIN {
      package My::Template::Context;
      use base qw(Template::Context);

      my @stack;
      my %totals;

      sub process {
        my $self = shift;

        my $template = $_[0];
        if (UNIVERSAL::isa($template, "Template::Document")) {
          $template = $template->name;
        }

        push @stack, [time, times];

        my @return = wantarray ? $self->SUPER::process(@_) : scalar 
$self->SUPER::process(@_);

        my @delta_times = @{pop @stack};
        @delta_times = map { $_ - shift @delta_times } time, times;
        for (0..$#delta_times) {
          $totals{$template}[$_] += $delta_times[$_];
          for my $parent (@stack) {
            $parent->[$_] += $delta_times[$_] if @stack; # parent adjust
          }
        }
        $totals{$template}[5] ++;   # count of calls

        wantarray ? @return : $return[0];

      }

      END {
        for my $template (sort keys %totals) {
          my @values = @{$totals{$template}};
          printf STDERR "%3d %3d %6.2f %6.2f %6.2f %6.2f %s\n",
            $values[5], @values[0..4], $template;
        }
      }

      $Template::Config::CONTEXT = __PACKAGE__;
      $INC{"My/Template/Context.pm"} = "loaded";
    }

This dumps a list of templates, with their "profiled" count of calls,
cumulative user cpu, system cpu, child user cpu, and child system cpu.

Using this, I found a place where I was spending about 50% of the time
on a hit, and was able to clean it up to speed everything up.  Yeay.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to