blib/lib/Scientist.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 100% 88 / 88 CC 9 SCAR 12.3
CC
9
Cov
100%
run
7.0
__ANON__
2.0
__ANON__
2.0
CRAP 3.4
↑ Summary
1 ## no critic (Miscellanea::ProhibitUnrestrictedNoCritic, ValuesAndExpressions::ProhibitVersionStrings)
2 package Scientist;
3  
4 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Test2::Compare v0.0.121 qw/compare strict_convert/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Test2::Compare::Delta;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Types::Standard qw/Bool Str CodeRef HashRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 our $VERSION = '0.015'; # VERSION
11  
12 # ABSTRACT: Perl module inspired by https://github.com/github/scientist
13 # https://github.com/lancew/Scientist
14  
15 9 has 'context' => (
16 is => 'rw',
17 isa => HashRef,
18 );
19  
20 9 has 'enabled' => (
21 is => 'rw',
22 isa => Bool,
23 default => 1,
24 );
25  
26 9 has 'experiment' => (
27 is => 'ro',
28 isa => Str,
29 builder => 'name',
30 );
31  
32 9 has 'use' => (
33 is => 'rw',
34 isa => CodeRef,
35 );
36  
37 9 has 'result' => (
38 is => 'rw',
39 isa => HashRef,
40 );
41  
42 9 has 'try' => (
43 is => 'rw',
44 isa => CodeRef,
45 );
46  
47 13 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
48 13 return 'experiment';
49 }
50  
51 1009 ▶sub publish {
Subroutine publish: called CC=1 SCAR=0.0
Pod documented
52 1009 my $self = shift;
53 # Stub publish sub, extend this to enable your own own
54 # unique publishing requirements
55 1009 return;
56 }
57  
58 1014 ▶sub run {
Subroutine run: called CC=7 SCAR=19.5
Pod documented
59 1014 my $self = shift;
60  
61 # If experiment not enabled just return the control code results.
62 1014 ▶ return $self->use->() unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 3 1011 1014
63  
64 1011 my %result = (
65 context => $self->context,
66 experiment => $self->experiment,
67 );
68  
69 1011 my $wantarray = wantarray;
70  
71 1011 my ( @control, @candidate );
72 1011 ▶ my $run_control = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
73 1011 my $start = time;
74 1011 ▶ @control = $wantarray ? $self->use->() : scalar $self->use->();
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1010 1011
75 1010 $result{control}{duration} = time - $start;
76 1011 };
77  
78 1010 ▶ my $run_candidate = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
79 1010 my $start = time;
80 @candidate = $wantarray
81 1 ? eval { $self->try->() }
82 1010 ▶ : eval { scalar $self->try->() };
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1009 1010
83 1010 $result{candidate}{duration} = time - $start;
84 1011 };
85  
86 1011 ▶ if ( rand > 0.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand > 0.5) { } 497 514 1011
87 497 $run_control->();
88 496 $run_candidate->();
89 }
90 else {
91 514 $run_candidate->();
92 514 $run_control->();
93 }
94  
95 1010 my $delta = compare(\@candidate, \@control, \&strict_convert);
96 1010 ▶ my $diag = $delta ? $delta->table->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
$delta ? : 7 1003 1010
97  
98 1010 $result{matched} = $diag eq '';
99 1010 $result{mismatched} = $diag ne '';
100  
101 $result{observation} = {
102 1010 ▶ candidate => $wantarray ? @candidate : $candidate[0],
Branch: 2 branches
BranchTrueFalseTotal
$wantarray ? : 1 1009 1010
$wantarray ? : 1 1009 1010
103 control => $wantarray ? @control : $control[0],
104 diagnostic => $diag,
105 };
106  
107 1010 $self->result( \%result );
108 1010 $self->publish;
109  
110 1009 ▶ return $wantarray ? @control : $control[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1008 1009
111 }
112  
113 # Use better column header names in the observation diagnostic table.
114 15 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
115 15 Test2::Compare::Delta->set_column_alias(GOT => 'CONTROL');
116 15 Test2::Compare::Delta->set_column_alias(CHECK => 'CANDIDATE');
117 15 return;
118 }
119  
120 9 1;
121  
122 =head1 LICENSE
123  
124 This software is Copyright (c) 2016 by Lance Wicks.
125  
126 This is free software, licensed under:
127  
128 The MIT (X11) License
129  
130 The MIT License
131  
132 Permission is hereby granted, free of charge, to any person
133 obtaining a copy of this software and associated
134 documentation files (the "Software"), to deal in the Software
135 without restriction, including without limitation the rights to
136 use, copy, modify, merge, publish, distribute, sublicense,
137 and/or sell copies of the Software, and to permit persons to
138 whom the Software is furnished to do so, subject to the
139 following conditions:
140  
141 The above copyright notice and this permission notice shall
142 be included in all copies or substantial portions of the
143 Software.
144  
145 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
146 WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
147 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
148 MERCHANTABILITY, FITNESS FOR A PARTICULAR
149 PURPOSE AND NONINFRINGEMENT. IN NO EVENT
150 SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
151 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
152 LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
153 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
154 CONNECTION WITH THE SOFTWARE OR THE USE OR
155 OTHER DEALINGS IN THE SOFTWARE.
↑ Summary