blib/lib/Scientist.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 100% 70 / 70 CC 7 SCAR 9.6
CC
7
Cov
100%
run
5.0
__ANON__
2.0
__ANON__
2.0
CRAP 2.6
↑ Summary
1 package Scientist;
2 8 $Scientist::VERSION = '0.006';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Test::Deep::NoTest 'eq_deeply';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Perl module inspired by https://github.com/github/scientist
11 # https://github.com/lancew/Scientist
12  
13 # TODO: Should limit to a hashref
14 8 has 'context' => ( is => 'rw' );
15  
16 8 has 'enabled' => ( is => 'rw', default => 1 );
17  
18 8 has 'experiment' => ( is => 'rw' );
19  
20 8 has 'use' => ( is => 'rw' );
21  
22 8 has 'result' => ( is => 'rw', );
23  
24 8 has 'try' => ( is => 'rw' );
25  
26 sub publish {
27 1008 ▶ my $self = shift;
Subroutine publish: called CC=1 SCAR=0.0
Pod documented
28 # Stub publish sub, extend this to enable your own own
29 # unique publishing requirements
30 1008 return;
31 }
32  
33 sub run {
34 1012 ▶ my $self = shift;
Subroutine run: called CC=5 SCAR=16.1
Pod documented
35  
36 # If experiement not enabled just return the control code results.
37 1012 ▶ return $self->use->() unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 3 1009 1012
38  
39 1009 my %result = (
40 context => $self->context,
41 experiment => $self->experiment,
42 );
43  
44 1009 my $wantarray = wantarray;
45  
46 1009 my ( @control, @candidate );
47 my $run_control = sub {
48 1009 ▶ my $start = Time::HiRes::time;
Subroutine __ANON__: called CC=2 SCAR=6.9
49 1009 ▶ @control = $wantarray ? $self->use->() : scalar $self->use->();
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1008 1009
50 1009 $result{control}{duration} = Time::HiRes::time - $start;
51 1009 };
52  
53 my $run_candidate = sub {
54 1009 ▶ my $start = Time::HiRes::time;
Subroutine __ANON__: called CC=2 SCAR=6.9
55 1009 ▶ @candidate = $wantarray ? $self->try->() : scalar $self->try->();
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1008 1009
56 1009 $result{candidate}{duration} = Time::HiRes::time - $start;
57 1009 };
58  
59 1009 ▶ if ( rand > 0.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand > 0.5) { } 523 486 1009
60 523 $run_control->();
61 523 $run_candidate->();
62 }
63 else {
64 486 $run_candidate->();
65 486 $run_control->();
66 }
67  
68 1009 ▶ $result{mismatched} = !eq_deeply( \@candidate, \@control ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!eq_deeply(\@candidate, \@control) ? : 5 1004 1009
69  
70 1009 $self->result( \%result );
71 1009 $self->publish;
72  
73 1008 ▶ return $wantarray ? @control : $control[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 1007 1008
74 }
75  
76 8 1;
77  
78 =head1 LICENSE
79  
80 This software is Copyright (c) 2016 by Lance Wicks.
81  
82 This is free software, licensed under:
83  
84 The MIT (X11) License
85  
86 The MIT License
87  
88 Permission is hereby granted, free of charge, to any person
89 obtaining a copy of this software and associated
90 documentation files (the "Software"), to deal in the Software
91 without restriction, including without limitation the rights to
92 use, copy, modify, merge, publish, distribute, sublicense,
93 and/or sell copies of the Software, and to permit persons to
94 whom the Software is furnished to do so, subject to the
95 following conditions:
96  
97 The above copyright notice and this permission notice shall
98 be included in all copies or substantial portions of the
99 Software.
100  
101 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
102 WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
103 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
104 MERCHANTABILITY, FITNESS FOR A PARTICULAR
105 PURPOSE AND NONINFRINGEMENT. IN NO EVENT
106 SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
107 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108 LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
109 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
110 CONNECTION WITH THE SOFTWARE OR THE USE OR
111 OTHER DEALINGS IN THE SOFTWARE.
↑ Summary