Debian Codegraph @

lib/Test2/Harness/Runner/Resource/JobCount.pm

raw
1 package Test2::Harness::Runner::Resource::JobCount;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use parent 'Test2::Harness::Runner::Resource';
8 use Test2::Harness::Util::HashBase qw/<settings <job_count <used <free/;
9 use Time::HiRes qw/time/;
10 use List::Util qw/min/;
11
12 sub job_limiter { 1 }
13
14 sub new {
15 my $class = shift;
16 my $self = bless {@_}, $class;
17 $self->init();
18 return $self;
19 }
20
21 sub init {
22 my $self = shift;
23 my $settings = $self->{+SETTINGS};
24 $self->{+JOB_COUNT} //= $settings ? $settings->runner->job_count // 1 : 1;
25 $self->{+USED} //= {};
26 $self->{+FREE} //= [1 .. $self->{+JOB_COUNT}];
27 }
28
29 sub job_limiter_max {
30 my $self = shift;
31 return $self->{+JOB_COUNT};
32 }
33
34 sub job_limiter_at_max {
35 my $self = shift;
36 return 0 if @{$self->{+FREE}};
37 return 1;
38 }
39
40 sub available {
41 my $self = shift;
42 my ($task) = @_;
43
44 my $rmin = $self->settings->runner->slots_per_job;
45 my $tmin = $task->{min_slots} // 1;
46 my $tmax = $task->{max_slots} // $tmin;
47
48 return -1 if $self->{+JOB_COUNT} < $tmin;
49 return -1 if $rmin < $tmin;
50
51 my $concurrency = min(grep { $_ } $tmax, $rmin);
52 $concurrency ||= 1;
53
54 return 1 if @{$self->{+FREE}} >= $concurrency;
55 return 0;
56 }
57
58 sub assign {
59 my $self = shift;
60 my ($task, $state) = @_;
61
62 my $rmin = $self->settings->runner->slots_per_job;
63 my $tmin = $task->{min_slots} // 1;
64 my $tmax = $task->{max_slots} // $tmin;
65 my $concurrency = min(grep { $_ } $tmax, $rmin);
66 $concurrency ||= 1;
67
68 $state->{record} = {
69 count => $concurrency,
70 file => $task->{rel_file},
71 stamp => time,
72 };
73
74 $state->{env_vars}->{T2_HARNESS_MY_JOB_CONCURRENCY} = $concurrency;
75 }
76
77 sub record {
78 my $self = shift;
79 my ($job_id, $info) = @_;
80
81 my $count = $info->{count};
82 my @use = splice @{$self->{+FREE}}, 0, $count;
83 $info->{slots} = \@use;
84
85 $self->{+USED}->{$job_id} = $info;
86 }
87
88 sub release {
89 my $self = shift;
90 my ($job_id) = @_;
91
92 # Could be a free with no used slot.
93 my $info = delete $self->{+USED}->{$job_id} or return;
94 my $slots = $info->{slots};
95
96 push @{$self->{+FREE}} => @$slots;
97 }
98
99 sub status_data {
100 my $self = shift;
101
102 my @rows;
103
104 my $time = time;
105
106 for my $info (sort { $a->{stamp} <=> $b->{stamp} } values %{$self->{+USED}}) {
107 my $count = @{$info->{slots} || []};
108 push @rows => [$time - $info->{stamp}, $count, $info->{file}];
109 }
110
111 push @rows => [undef, scalar(@{$self->{+FREE}}), '** FREE **'];
112
113 return [
114 {
115 tables => [
116 {
117 headers => [qw/Runtime Slots Name/],
118 format => ['duration'],
119 rows => \@rows,
120 },
121 ],
122 },
123 ],
124 }
125
126 1;
127
128 __END__
129
130 =pod
131
132 =encoding UTF-8
133
134 =head1 NAME
135
136 Test2::Harness::Runner::Resource::JobCount - limit the job count (-j)
137
138 =head1 SOURCE
139
140 The source code repository for Test2-Harness can be found at
141 F<http://github.com/Test-More/Test2-Harness/>.
142
143 =head1 MAINTAINERS
144
145 =over 4
146
147 =item Chad Granum E<lt>[email protected]<gt>
148
149 =back
150
151 =head1 AUTHORS
152
153 =over 4
154
155 =item Chad Granum E<lt>[email protected]<gt>
156
157 =back
158
159 =head1 COPYRIGHT
160
161 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
162
163 This program is free software; you can redistribute it and/or
164 modify it under the same terms as Perl itself.
165
166 See F<http://dev.perl.org/licenses/>
167
168 =cut