/
Scientist.pm
52 lines (42 loc) · 1.02 KB
/
Scientist.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
unit class Scientist;
has %.context is rw;
has Bool $.enabled is rw = True;
has Str $.experiment is rw;
has %!result;
has &.try is rw;
has &.use is rw is required;
method publish {
# Requires populating to be useful.
}
method result { Map.new(%!result) }
method run {
return &.use.() unless $!enabled;
%!result = (
context => %!context,
experiment => $!experiment,
);
my ($candidate, $control);
my $run_control = sub {
my $start = now;
$control = &!use.();
%!result<control><duration> = now - $start;
};
my $run_candidate = sub {
my $start = now;
try {
$candidate = &!try.();
}
%!result<candidate><duration> = now - $start;
};
if ( rand > 0.5 ) {
$run_control.();
$run_candidate.();
}
else {
$run_candidate.();
$run_control.();
}
%!result<mismatched> = $control !eqv $candidate;
$.publish();
return $control;
}