| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Algorithm::Classifier::IsolationForest::App::Command::bench; |
|
2
|
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
6260
|
use strict; |
|
|
9
|
|
|
|
|
14
|
|
|
|
9
|
|
|
|
|
263
|
|
|
4
|
9
|
|
|
9
|
|
32
|
use warnings; |
|
|
9
|
|
|
|
|
14
|
|
|
|
9
|
|
|
|
|
331
|
|
|
5
|
9
|
|
|
9
|
|
37
|
use Algorithm::Classifier::IsolationForest (); |
|
|
9
|
|
|
|
|
15
|
|
|
|
9
|
|
|
|
|
175
|
|
|
6
|
9
|
|
|
9
|
|
30
|
use Algorithm::Classifier::IsolationForest::App -command; |
|
|
9
|
|
|
|
|
15
|
|
|
|
9
|
|
|
|
|
62
|
|
|
7
|
9
|
|
|
9
|
|
2503
|
use File::Slurp qw(read_file); |
|
|
9
|
|
|
|
|
15
|
|
|
|
9
|
|
|
|
|
423
|
|
|
8
|
9
|
|
|
9
|
|
57
|
use Scalar::Util qw(looks_like_number); |
|
|
9
|
|
|
|
|
15
|
|
|
|
9
|
|
|
|
|
353
|
|
|
9
|
9
|
|
|
9
|
|
4207
|
use Time::HiRes qw(time); |
|
|
9
|
|
|
|
|
9998
|
|
|
|
9
|
|
|
|
|
64
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub opt_spec { |
|
12
|
|
|
|
|
|
|
return ( |
|
13
|
|
|
|
|
|
|
[ |
|
14
|
1
|
|
|
1
|
1
|
24999
|
'm=s', |
|
15
|
|
|
|
|
|
|
'Input model JSON file path/name.', |
|
16
|
|
|
|
|
|
|
{ 'default' => 'iforest_model.json', 'completion' => 'files' } |
|
17
|
|
|
|
|
|
|
], |
|
18
|
|
|
|
|
|
|
[ 'i=s', 'Input CSV (rows of features to score).', { 'completion' => 'files' } ], |
|
19
|
|
|
|
|
|
|
[ 'secs|s=f', 'Seconds per measurement (after a 0.3s warm-up).', { 'default' => 2 } ], |
|
20
|
|
|
|
|
|
|
[ 't=f', 'Threshold to use for predict / score_predict_*.', { 'default' => 0.5 } ], |
|
21
|
|
|
|
|
|
|
); |
|
22
|
|
|
|
|
|
|
} ## end sub opt_spec |
|
23
|
|
|
|
|
|
|
|
|
24
|
0
|
|
|
0
|
1
|
0
|
sub abstract { 'Measure scoring throughput of a saved model on a CSV dataset' } |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub description { |
|
27
|
0
|
|
|
0
|
1
|
0
|
'Loads a model and a CSV dataset, then times each of the |
|
28
|
|
|
|
|
|
|
public scoring methods over the configured wall-clock budget. Reports |
|
29
|
|
|
|
|
|
|
ops-per-second for each. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
When the Inline::C backend is active the bench also runs pack_data once |
|
32
|
|
|
|
|
|
|
up front and times the *_packed variants so users can see how much |
|
33
|
|
|
|
|
|
|
pre-packing saves on their workload. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Use this to answer: |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
* is my Inline::C / OpenMP / SIMD build actually faster than the |
|
38
|
|
|
|
|
|
|
pure-Perl fallback? |
|
39
|
|
|
|
|
|
|
* how much does pack_data help on my data shape? |
|
40
|
|
|
|
|
|
|
* what is the per-call throughput I can expect at production-typical |
|
41
|
|
|
|
|
|
|
query-set sizes? |
|
42
|
|
|
|
|
|
|
'; |
|
43
|
|
|
|
|
|
|
} ## end sub description |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub validate { |
|
46
|
1
|
|
|
1
|
0
|
4
|
my ( $self, $opt, $args ) = @_; |
|
47
|
|
|
|
|
|
|
|
|
48
|
1
|
50
|
|
|
|
69
|
if ( !defined $opt->{'i'} ) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
$self->usage_error('-i has not been specified'); |
|
50
|
|
|
|
|
|
|
} elsif ( !-f $opt->{'i'} ) { |
|
51
|
0
|
|
|
|
|
0
|
$self->usage_error( '-i, "' . $opt->{'i'} . '", is not a file or does not exist' ); |
|
52
|
|
|
|
|
|
|
} elsif ( !-r $opt->{'i'} ) { |
|
53
|
0
|
|
|
|
|
0
|
$self->usage_error( '-i, "' . $opt->{'i'} . '", is not readable' ); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
1
|
50
|
|
|
|
16
|
if ( !-f $opt->{'m'} ) { |
|
|
|
50
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
0
|
$self->usage_error( '-m, "' . $opt->{'m'} . '", is not a file or does not exist' ); |
|
58
|
|
|
|
|
|
|
} elsif ( !-r $opt->{'m'} ) { |
|
59
|
0
|
|
|
|
|
0
|
$self->usage_error( '-m, "' . $opt->{'m'} . '", is not readable' ); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
8
|
if ( $opt->{'secs'} <= 0 ) { |
|
63
|
0
|
|
|
|
|
0
|
$self->usage_error('--secs must be > 0'); |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
1
|
50
|
33
|
|
|
5
|
if ( $opt->{'t'} <= 0 || $opt->{'t'} >= 1 ) { |
|
67
|
0
|
|
|
|
|
0
|
$self->usage_error('-t must satisfy 0 < t < 1'); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
4
|
return 1; |
|
71
|
|
|
|
|
|
|
} ## end sub validate |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Standard bench helper: warm up briefly, then time exactly $secs of |
|
74
|
|
|
|
|
|
|
# back-to-back calls. Returns ops/second. |
|
75
|
|
|
|
|
|
|
sub _bench { |
|
76
|
8
|
|
|
8
|
|
27
|
my ( $code, $secs ) = @_; |
|
77
|
8
|
|
|
|
|
23
|
my $t0 = time(); |
|
78
|
8
|
|
|
|
|
38
|
$code->() while time() - $t0 < 0.3; |
|
79
|
8
|
|
|
|
|
29
|
$t0 = time(); |
|
80
|
8
|
|
|
|
|
18
|
my $n = 0; |
|
81
|
8
|
|
|
|
|
41
|
$code->(), $n++ while time() - $t0 < $secs; |
|
82
|
8
|
|
|
|
|
45
|
return $n / ( time() - $t0 ); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _read_csv { |
|
86
|
1
|
|
|
1
|
|
3
|
my ($path) = @_; |
|
87
|
1
|
|
|
|
|
45
|
my @data; |
|
88
|
|
|
|
|
|
|
my $expected; |
|
89
|
1
|
|
|
|
|
4
|
my $line = 0; |
|
90
|
1
|
|
|
|
|
7
|
for my $row ( read_file($path) ) { |
|
91
|
7
|
|
|
|
|
233
|
$line++; |
|
92
|
7
|
|
|
|
|
10
|
chomp $row; |
|
93
|
7
|
50
|
|
|
|
20
|
next if $row =~ /^\s*$/; |
|
94
|
7
|
|
|
|
|
12
|
my @f = split /,/, $row, -1; |
|
95
|
7
|
|
100
|
|
|
17
|
$expected //= scalar @f; |
|
96
|
7
|
50
|
|
|
|
9
|
die "line $line of '$path' has $row but expected $expected columns\n" |
|
97
|
|
|
|
|
|
|
if scalar @f != $expected; |
|
98
|
7
|
|
|
|
|
9
|
for my $v (@f) { |
|
99
|
21
|
50
|
|
|
|
64
|
die "line $line of '$path' value '$v' is not numeric\n" |
|
100
|
|
|
|
|
|
|
unless looks_like_number($v); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
7
|
|
|
|
|
10
|
push @data, \@f; |
|
103
|
|
|
|
|
|
|
} ## end for my $row ( read_file($path) ) |
|
104
|
1
|
|
|
|
|
5
|
return ( \@data, $expected ); |
|
105
|
|
|
|
|
|
|
} ## end sub _read_csv |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub execute { |
|
108
|
1
|
|
|
1
|
1
|
7
|
my ( $self, $opt, $args ) = @_; |
|
109
|
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
9
|
my $model = Algorithm::Classifier::IsolationForest->load( $opt->{'m'} ); |
|
111
|
|
|
|
|
|
|
|
|
112
|
1
|
|
|
|
|
6
|
my ( $data, $cols ) = _read_csv( $opt->{'i'} ); |
|
113
|
|
|
|
|
|
|
die "input CSV has $cols feature columns but model expects " . $model->{n_features} . "\n" |
|
114
|
1
|
50
|
|
|
|
3
|
if $cols != $model->{n_features}; |
|
115
|
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
3
|
my $n_pts = scalar @$data; |
|
117
|
1
|
|
|
|
|
2
|
my $secs = $opt->{'secs'}; |
|
118
|
1
|
|
|
|
|
2
|
my $thresh = $opt->{'t'}; |
|
119
|
1
|
50
|
|
|
|
4
|
my $has_c = $Algorithm::Classifier::IsolationForest::HAS_C ? 1 : 0; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
printf "Model: %s (n_trees=%d, mode=%s, n_features=%d)\n", |
|
122
|
1
|
|
|
|
|
11
|
$opt->{'m'}, scalar @{ $model->{trees} }, |
|
123
|
1
|
|
|
|
|
2
|
$model->{mode}, $model->{n_features}; |
|
124
|
1
|
|
|
|
|
3
|
printf "Input: %s (%d rows)\n", $opt->{'i'}, $n_pts; |
|
125
|
1
|
|
|
|
|
10
|
printf "Budget: %.1fs per measurement (0.3s warm-up)\n", $secs; |
|
126
|
1
|
50
|
|
|
|
4
|
printf "Backend: HAS_C=%d HAS_OPENMP=%d HAS_SIMD=%d\n\n", |
|
|
|
50
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$has_c, |
|
128
|
|
|
|
|
|
|
$Algorithm::Classifier::IsolationForest::HAS_OPENMP ? 1 : 0, |
|
129
|
|
|
|
|
|
|
$Algorithm::Classifier::IsolationForest::HAS_SIMD ? 1 : 0; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Pre-pack once (when C is available) so the *_packed rows measure |
|
132
|
|
|
|
|
|
|
# scoring in isolation, without the per-call pack_input_xs cost. |
|
133
|
1
|
50
|
|
|
|
18
|
my $packed = $has_c ? $model->pack_data($data) : undef; |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my @bench = ( |
|
136
|
5454
|
|
|
5454
|
|
12763
|
[ 'score_samples' => sub { $model->score_samples($data) } ], |
|
137
|
6028
|
|
|
6028
|
|
14406
|
[ 'predict' => sub { $model->predict( $data, $thresh ) } ], |
|
138
|
5096
|
|
|
5096
|
|
12505
|
[ 'score_predict_samples' => sub { $model->score_predict_samples( $data, $thresh ) } ], |
|
139
|
5931
|
|
|
5931
|
|
14269
|
[ 'score_predict_split' => sub { my @r = $model->score_predict_split( $data, $thresh ); } ], |
|
140
|
1
|
|
|
7502
|
|
27
|
[ 'path_lengths' => sub { $model->path_lengths($data) } ], |
|
|
7502
|
|
|
|
|
17178
|
|
|
141
|
|
|
|
|
|
|
); |
|
142
|
|
|
|
|
|
|
|
|
143
|
1
|
50
|
|
|
|
4
|
if ( defined $packed ) { |
|
144
|
|
|
|
|
|
|
push @bench, |
|
145
|
|
|
|
|
|
|
( |
|
146
|
8687
|
|
|
8687
|
|
21191
|
[ 'score_samples (packed)' => sub { $model->score_samples($packed) } ], |
|
147
|
8943
|
|
|
8943
|
|
19301
|
[ 'predict (packed)' => sub { $model->predict( $packed, $thresh ) } ], |
|
148
|
1
|
|
|
8177
|
|
18
|
[ 'score_predict_split (packed)' => sub { my @r = $model->score_predict_split( $packed, $thresh ); } ], |
|
|
8177
|
|
|
|
|
19157
|
|
|
149
|
|
|
|
|
|
|
); |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
1
|
|
|
|
|
5
|
printf " %-30s %14s %14s\n", 'method', 'ops/s', 'ms/call'; |
|
153
|
1
|
|
|
|
|
7
|
printf " %-30s %14s %14s\n", '-' x 30, '-' x 14, '-' x 14; |
|
154
|
1
|
|
|
|
|
3
|
for my $row (@bench) { |
|
155
|
8
|
|
|
|
|
45
|
my ( $label, $code ) = @$row; |
|
156
|
8
|
|
|
|
|
29
|
my $rate = _bench( $code, $secs ); |
|
157
|
8
|
50
|
|
|
|
312
|
printf " %-30s %14.1f %14.2f\n", $label, $rate, $rate > 0 ? 1000 / $rate : 0; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
1
|
|
|
|
|
388
|
return 1; |
|
160
|
|
|
|
|
|
|
} ## end sub execute |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
return 1; |