| 1 | package Test::DBIx::Class::Stats; | ||||||||||
| 2 | 1 | ▶ | use parent 'Test::Builder::Module'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Test::DBIx::Class::Stats::Profiler; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = 0.02; | |||||||||
| 6 | 1 | our @EXPORT = qw( with_stats ); | |||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Test::DBIx::Class::Stats - test statistics about your DBIx::Class calls | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | Run a subtest with a debugging object (L<Test::DBIx::Class::Stats::Profiler>) set to capture | ||||||||||
| 15 | the number of calls that have been made to the database. This may be useful to | ||||||||||
| 16 | check your assumptions about prefetching, etc. | ||||||||||
| 17 | |||||||||||
| 18 | use Test::More; | ||||||||||
| 19 | use Test::DBIx::Class::Stats; | ||||||||||
| 20 | |||||||||||
| 21 | # if you are using Test::DBIx::Class or similar, we can get the | ||||||||||
| 22 | # database handle from the `Schema` method | ||||||||||
| 23 | use Test::DBIx::Class; | ||||||||||
| 24 | |||||||||||
| 25 | with_stats 'test 1', sub { | ||||||||||
| 26 | my $stats = shift; | ||||||||||
| 27 | |||||||||||
| 28 | my $rs = Schema->resultset('Foo')->search(); | ||||||||||
| 29 | is $stats->call_count, 0, 'No calls on preparing RS'; | ||||||||||
| 30 | |||||||||||
| 31 | my @foo = $rs->all; | ||||||||||
| 32 | is $stats->call_count, 1, '1 call after preparing RS'; | ||||||||||
| 33 | }; | ||||||||||
| 34 | |||||||||||
| 35 | # alternatively, we can pass it in explicitly: | ||||||||||
| 36 | |||||||||||
| 37 | my $db = Schema | ||||||||||
| 38 | with_stats 'test 2', $db, sub { | ||||||||||
| 39 | ... | ||||||||||
| 40 | }; | ||||||||||
| 41 | |||||||||||
| 42 | =head1 EXPORTED FUNCTIONS | ||||||||||
| 43 | |||||||||||
| 44 | =over 4 | ||||||||||
| 45 | |||||||||||
| 46 | =item C<with_stats $name, [$db], $code> | ||||||||||
| 47 | |||||||||||
| 48 | The L<Test::DBIx::Class::Stats::Profiler> object is created for the database | ||||||||||
| 49 | and is passed to your code reference as its first and only argument. | ||||||||||
| 50 | |||||||||||
| 51 | If C<$db> is not passed, the caller's C<Schema> function will be called. This | ||||||||||
| 52 | is designed to work with L<Test::DBIx::Class>. | ||||||||||
| 53 | |||||||||||
| 54 | =back | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 2 | ▶ | sub with_stats { | ||||||||
|
Subroutine
with_stats: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | 2 | my ($name, @args) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 2 | my $subtest = pop @args; | |||||||||
| 62 | 2 | ▶ | my $db = @args ? shift @args : caller->Schema; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 2 | my $storage = $db->storage; | |||||||||
| 65 | 2 | my %old = ( | |||||||||
| 66 | debug => $storage->debug, | ||||||||||
| 67 | debugobj => $storage->debugobj, | ||||||||||
| 68 | ); | ||||||||||
| 69 | 2 | my $stats = Test::DBIx::Class::Stats::Profiler->new(); | |||||||||
| 70 | 2 | $storage->debug(1); | |||||||||
| 71 | 2 | $storage->debugobj( $stats ); | |||||||||
| 72 | |||||||||||
| 73 | 2 | my $tb = __PACKAGE__->builder; | |||||||||
| 74 | 2 | $tb->subtest( "With stats: $name", $subtest, $stats ); | |||||||||
| 75 | |||||||||||
| 76 | 2 | $storage->debug( $old{debug} ); | |||||||||
| 77 | 2 | $storage->debugobj( $old{debugobj} ); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | =head1 AUTHOR | ||||||||||
| 81 | |||||||||||
| 82 | osfameron <osfameron@cpan.org> 2014-2017 | ||||||||||
| 83 | Alexander Hartmaier <abraxxa@cpan.org> 2017 | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 1 | 1; | |||||||||