in reply to Re^4: Fast coverage module or hack
in thread Fast coverage module or hack
I will probably put this on CPAN soonish, but here's the gist of it. Below is the contents of "Devel::TraceCalls". Most of its dependencies are in core, except for Hook::AfterRuntime and match::simple which are on CPAN.
You need to use Devel::TraceCalls in each module.
Devel::TraceCalls does three things:
Checks the PERL_TRACE_CALLS environment variable is true. If it's false, it doesn't do the next two things.
Wraps every sub in the caller module with tracking code to count how many times the sub has been called. This wrapper uses goto to call the original sub, so it should be invisible to caller. It does wrap imported subs, so if you don't want them wrapped, clean then with namespace::autoclean. It does wrap generated subs, like Moose attributes. It doesn't wrap inherited subs. (Though of course the class you're inheriting from can use Devel::TraceCalls). Note that Moose constructors are inherited if your class is mutable and generated if your class is immutable.
In an END { ... } block, it uses FindBin to find the name of the test script, and creates a JSON file with the same name but a ".t.map" file extension. In this JSON file, it dumps a hashref where the keys are the modules which have been called.
The end result is that if you run:
PERL_TRACE_CALLS=1 prove -l t/sometest.t
You'll get a file called "t/sometest.t.map" containing something like this:
{ "Local::Example" : { "quux" : 1 }, "Local::Example::Module1" : { "bar" : 1 }, "Local::Example::Module2" : { "bar" : 1, "foo" : 1 } }
use strict; use warnings; package Devel::TraceCalls; our $AUTHORITY = 'cpan:TOBYINK'; our $VERSION = '0.001'; use constant ACTIVE => $ENV{'PERL_TRACE_CALLS'}; BEGIN { eval q{ use match::simple (); use Carp (); use File::Spec (); use FindBin (); use Hook::AfterRuntime (); use JSON::PP (); use Sub::Util (); 1; } || die($@) if ACTIVE; }; our $JSON; our %CALL; $JSON = 'JSON::PP'->new->pretty(1)->canonical(1) if ACTIVE; sub import { my $me = shift; my $caller = caller; my (%opts) = @_; &Hook::AfterRuntime::after_runtime( sub { $me->setup_for($caller, %opts) }, ) if ACTIVE; } sub setup_for { my $me = shift; my ($caller, %opts) = @_; $opts{match} = sub { local $_ = shift; !/^_/ and /\p{Ll}/; } unless exists $opts{match}; no strict 'refs'; my @names = grep match::simple::match($_, $opts{match}), grep !/::$/, sort keys %{"$caller\::"}; $me->wrap_sub($caller, $_) for @names; } sub wrap_sub { my $me = shift; no strict 'refs'; no warnings 'redefine'; my ($package, $sub) = @_; ($package, $sub) = (/^(.+)::([^:]+)$/ =~ $package) if !defined $sub; my $code = \&{"$package\::$sub"}; my $subname = Sub::Util::subname($code); my $newcode = Sub::Util::set_prototype prototype($code), Sub::Util::set_subname $subname, sub { ++$CALL{$package}{$sub}; goto $code }; *{"$package\::$sub"} = $newcode; } END { if (ACTIVE) { my $map = $JSON->encode(\%CALL); my $outfile = 'File::Spec'->catfile( $FindBin::RealBin, $FindBin::RealScript . ".map", ); my $already = 0; if (-f $outfile) { my $slurped = do { local $/; my $fh; open($fh, '<', $outfile) ? <$fh> : undef; }; $already++ if $slurped eq $map; } if (!$already) { open my $outfh, '>', $outfile or Carp::croak("Cannot open $outfile for output: $!"); print {$outfh} $map or Carp::croak("Cannot write to $outfile: $!"); close $outfh or Carp::croak("Cannot close $outfile: $!"); } }; } 1;
There are some deliberate optimizations like loading dependencies in a stringy eval, so even if you do use Devel::TraceCalls in production code, it shouldn't waste time or memory.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Fast coverage module or hack
by vsespb (Chaplain) on Dec 02, 2019 at 16:50 UTC | |
by tobyink (Canon) on Dec 02, 2019 at 20:22 UTC | |
by vsespb (Chaplain) on Dec 02, 2019 at 20:45 UTC | |
by tobyink (Canon) on Dec 02, 2019 at 18:53 UTC | |
Re^6: Fast coverage module or hack
by vsespb (Chaplain) on Dec 02, 2019 at 16:19 UTC | |
by tobyink (Canon) on Dec 02, 2019 at 16:47 UTC |