duel 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -1
  3. data/README.md +35 -0
  4. data/docs/ci.md +96 -3
  5. data/docs/configuration.md +35 -1
  6. data/docs/how-it-works.md +14 -8
  7. data/docs/plugins.md +16 -0
  8. data/lib/duel/across.rb +1 -6
  9. data/lib/duel/baseline.rb +8 -7
  10. data/lib/duel/cli.rb +45 -19
  11. data/lib/duel/comment.rb +71 -10
  12. data/lib/duel/config.rb +113 -2
  13. data/lib/duel/context.rb +33 -34
  14. data/lib/duel/control.rb +52 -0
  15. data/lib/duel/init.rb +142 -18
  16. data/lib/duel/lane.rb +734 -0
  17. data/lib/duel/lock.rb +34 -4
  18. data/lib/duel/memory.rb +14 -5
  19. data/lib/duel/metric.rb +97 -26
  20. data/lib/duel/native.rb +199 -32
  21. data/lib/duel/platform.rb +31 -0
  22. data/lib/duel/process_sample.rb +74 -49
  23. data/lib/duel/resource.rb +71 -42
  24. data/lib/duel/result.rb +165 -0
  25. data/lib/duel/rubies.rb +21 -3
  26. data/lib/duel/runner.rb +13 -1039
  27. data/lib/duel/spawn.rb +327 -0
  28. data/lib/duel/summary.rb +10 -10
  29. data/lib/duel/templates/perf-caller.yml.erb +43 -0
  30. data/lib/duel/templates/perf-comment-caller.yml.erb +42 -0
  31. data/lib/duel/templates/perf-comment.yml.erb +45 -3
  32. data/lib/duel/templates/perf.yml.erb +143 -7
  33. data/lib/duel/testing.rb +3 -0
  34. data/lib/duel/version.rb +1 -1
  35. data/lib/duel/worker.rb +13 -4
  36. data/lib/duel/worker_process.rb +142 -0
  37. data/lib/duel.rb +27 -3
  38. data/sig/duel/cli.rbs +8 -0
  39. data/sig/duel/comment.rbs +16 -1
  40. data/sig/duel/config.rbs +52 -2
  41. data/sig/duel/context.rbs +4 -31
  42. data/sig/duel/control.rbs +27 -0
  43. data/sig/duel/init.rbs +56 -0
  44. data/sig/duel/lane.rbs +202 -0
  45. data/sig/duel/lock.rbs +8 -1
  46. data/sig/duel/memory.rbs +1 -1
  47. data/sig/duel/metric.rbs +53 -10
  48. data/sig/duel/native.rbs +109 -9
  49. data/sig/duel/platform.rbs +13 -0
  50. data/sig/duel/process_sample.rbs +7 -9
  51. data/sig/duel/resource.rbs +28 -9
  52. data/sig/duel/result.rbs +164 -0
  53. data/sig/duel/rubies.rbs +4 -0
  54. data/sig/duel/runner.rbs +3 -460
  55. data/sig/duel/spawn.rbs +111 -0
  56. data/sig/duel/summary.rbs +3 -2
  57. data/sig/duel/worker_process.rbs +90 -0
  58. data/sig/duel.rbs +17 -3
  59. metadata +121 -107
data/lib/duel/spawn.rb ADDED
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require_relative "native"
5
+ require_relative "platform"
6
+
7
+ module Duel
8
+ # Starts a process whose output goes to a log, or whose input and
9
+ # output are pipes, and gives the Child to wait for it and stop it by:
10
+ # with Process.spawn's redirects (Directly), or where spawn can't
11
+ # redirect or wait (JRuby on Windows), through Open3 (ThroughOpen3),
12
+ # the output copied to the log on a thread of its own. STARTER is the
13
+ # one that works here. (JRuby's Open3 there does what it's asked, but
14
+ # with the JVM's own environment, not ENV, and only a program that's an
15
+ # .exe found by name: see .open3.)
16
+ # @api private
17
+ module Spawn
18
+ # Whether Process.spawn redirects a process's input and output: not on
19
+ # JRuby on Windows, whose spawn writes to neither a file nor a pipe
20
+ # it's given (https://github.com/jruby/jruby/issues/3116).
21
+ REDIRECTS = !Platform::JRUBY_ON_WINDOWS
22
+
23
+ # Whether Process.wait waits for a process spawned: not on JRuby on
24
+ # Windows, where it returns at once, and so does Process.detach's
25
+ # thread (https://github.com/jruby/jruby/issues/7515).
26
+ WAITS = !Platform::JRUBY_ON_WINDOWS
27
+
28
+ # A lone String, as Process.spawn takes one, that a shell runs.
29
+ SHELL = /[\s"'*?<>|&;$`()]/
30
+
31
+ # Seconds the copy of a process's output to its log may go on once
32
+ # the process has ended: no longer, where a process it started (a
33
+ # server's daemon, say) keeps the output open.
34
+ COPY_WAIT = 5
35
+
36
+ # The most of a process's output copied to its log at a time.
37
+ CHUNK = 65_536
38
+
39
+ # The lines of a process's log a failure shows (see .tail).
40
+ LOG_LINES = 10
41
+
42
+ # A process started, to wait for and stop.
43
+ class Child
44
+ # What Process.kill raises for a process that's gone: ESRCH, or on
45
+ # JRuby on Windows, EPERM.
46
+ GONE = [Errno::ESRCH, Errno::EPERM].freeze
47
+
48
+ attr_reader :pid
49
+
50
+ # +pid+'s, waited for by +waiter+, a thread whose value is how it
51
+ # ended (a Process::Status), or for one spawned, by Process.detach's,
52
+ # once it's asked for (see #waiter and #detach). +group+: whether it
53
+ # was started in a group of its own (see Spawn.group), which #kill
54
+ # kills with it. (+windows+: whether it's on Windows, where it can
55
+ # only be killed: see #signal.)
56
+ def initialize(pid, waiter = nil, group: false, windows: Platform::WINDOWS)
57
+ @pid = pid
58
+ @waiter = waiter
59
+ @group = group
60
+ @windows = windows
61
+ end
62
+
63
+ # The thread whose value is how it ended (a Process::Status). For one
64
+ # spawned, Process.detach's, made once it's asked for: until then,
65
+ # #wait waits for it itself.
66
+ def waiter = @waiter ||= Process.detach(@pid)
67
+
68
+ # Waits for it on a thread from now on (see #waiter), so that it's
69
+ # reaped as it ends: returns itself.
70
+ def detach = tap { waiter }
71
+
72
+ def alive? = waiter.alive?
73
+
74
+ # Waits for it, and returns how it ended, as Native::Child.wait says:
75
+ # its exit status (or nil), the signal that killed it (or nil), and
76
+ # its peak memory, where it was spawned and nothing else waits for it
77
+ # (see #waiter), and that can be told (or nil).
78
+ def wait
79
+ waiter = @waiter
80
+ waiter ? Native::Child.ended(waiter.value) : Native::Child.wait(@pid)
81
+ end
82
+
83
+ # Sends it +signal+. On Windows, which can send another process no
84
+ # signal but KILL, kills it, with what it started (taskkill /T): a
85
+ # process it started would go on otherwise (JRuby's JVM, started by
86
+ # its jruby.exe, say). One that's gone is let be.
87
+ def signal(signal)
88
+ @windows ? Spawn.kill_tree(@pid, windows: true) : Process.kill(signal, @pid)
89
+ rescue *GONE
90
+ nil
91
+ end
92
+
93
+ # Kills it, with what it started where that can be told (its group,
94
+ # or on Windows, its tree: see Spawn.kill_tree), and waits for it to
95
+ # end. One that's gone is let be.
96
+ def kill
97
+ (@group || @windows) ? Spawn.kill_tree(@pid, windows: @windows) : Process.kill("KILL", @pid)
98
+ waiter.join
99
+ rescue *GONE
100
+ nil
101
+ end
102
+
103
+ # Waits up to +seconds+ for it to end, then kills it (see #kill).
104
+ def stop(seconds) = (kill unless waiter.join(seconds))
105
+ end
106
+
107
+ # Starts a process with Process.spawn, given its redirects and pipes.
108
+ module Directly
109
+ # Starts +command+ (as Process.spawn takes it, an environment first),
110
+ # its input nothing and its output and errors in +log+, opened with
111
+ # +mode+ ("a" or "w"), with +options+ (Process.spawn's: chdir:, say).
112
+ # Returns its Child, not yet waited for, so that Child#wait can wait
113
+ # for it as any child, with wait4 (or Child#detach, on a thread).
114
+ def self.logged(env, command, log:, mode:, **options)
115
+ out = Spawn.log(log, mode)
116
+ # (At its end: on Windows, a child given a file opened to append
117
+ # writes from where the file's at, which is its start.)
118
+ out.seek(0, IO::SEEK_END)
119
+ begin
120
+ Child.new(Process.spawn(env, *command, in: File::NULL, out: out, err: out, **options))
121
+ ensure
122
+ out.close
123
+ end
124
+ end
125
+
126
+ # Starts +command+ (as Process.spawn takes it, an environment first)
127
+ # with a pipe each way, and +options+, in a group of its own if
128
+ # +group+ (see Spawn.group). Returns its Child, its input and its
129
+ # output.
130
+ def self.piped(env, command, group: false, **options)
131
+ options = options.merge(Spawn.group) if group
132
+ input, requests = IO.pipe
133
+ replies, output = IO.pipe
134
+ pid = begin
135
+ Process.spawn(env, *command, in: input, out: output, **options)
136
+ ensure
137
+ input.close
138
+ output.close
139
+ end
140
+ [Child.new(pid, Process.detach(pid), group: group), requests, replies]
141
+ end
142
+
143
+ # Starts +command+ (as Process.spawn takes it, an environment first),
144
+ # its input nothing, and its output and errors this process's, with
145
+ # +options+. Returns its Child.
146
+ def self.started(env, command, **options)
147
+ pid = Process.spawn(env, *command, in: File::NULL, **options)
148
+ Child.new(pid, Process.detach(pid))
149
+ end
150
+ end
151
+
152
+ # Starts a process through Open3, as Directly does, where
153
+ # Process.spawn can't redirect or wait: what it says is copied to its
154
+ # log (or to this process's output) on a thread of its own, and its
155
+ # Child's thread (Child#waiter) is Open3's, its value once what it
156
+ # said is copied (see Spawn.ended).
157
+ module ThroughOpen3
158
+ def self.logged(env, command, log:, mode:, **options)
159
+ out = Spawn.log(log, mode)
160
+ input, output, waiter = begin
161
+ Spawn.open3(:popen2e, env, *command, **options)
162
+ rescue SystemCallError
163
+ out.close
164
+ raise
165
+ end
166
+ input.close
167
+ Child.new(waiter.pid, Spawn.ended(waiter, Spawn.copy(output, out), output))
168
+ end
169
+
170
+ def self.piped(env, command, group: false, **options)
171
+ options = options.merge(Spawn.group) if group
172
+ input, output, waiter = Spawn.open3(:popen2, env, *command, **options)
173
+ [Child.new(waiter.pid, waiter, group: group), input, output]
174
+ end
175
+
176
+ # (Its output and errors copied to this one's, so what it said as it
177
+ # failed is there to see.)
178
+ def self.started(env, command, **options)
179
+ input, output, errors, waiter = Spawn.open3(:popen3, env, *command, **options)
180
+ input.close
181
+ copies = [Spawn.copy(output, $stdout, close: false), Spawn.copy(errors, $stderr, close: false)]
182
+ Child.new(waiter.pid, Spawn.ended(waiter, Thread.new { copies.each(&:join) }, output, errors))
183
+ end
184
+ end
185
+
186
+ # How a process starts here: Directly, but where Process.spawn can't
187
+ # redirect or wait, ThroughOpen3.
188
+ # simplecov:disable ThroughOpen3 is JRuby on Windows's (its test run
189
+ # covers it).
190
+ STARTER = (REDIRECTS && WAITS) ? Directly : ThroughOpen3
191
+ # simplecov:enable
192
+
193
+ # How a process that failed ended: its exit status +code+, or the
194
+ # +signal+ that killed it (as Child#wait says).
195
+ def self.why(code, signal)
196
+ return "exited with #{code}" if code
197
+
198
+ killed = signal #: Integer
199
+ name = begin
200
+ Signal.signame(killed)
201
+ # (JRuby's, for a number it has no name for, where CRuby's is nil,
202
+ # and on Windows for KILL's: https://github.com/jruby/jruby/issues/10080.)
203
+ rescue ArgumentError
204
+ nil
205
+ end
206
+ name ? "was killed by SIG#{name}" : "was killed by signal #{killed}"
207
+ end
208
+
209
+ # The end of the log at +path+, for a failure to show: its last
210
+ # LOG_LINES lines, without CRLF's CR (a Windows program's).
211
+ def self.tail(path) = File.readlines(path, chomp: true).last(LOG_LINES).map { |line| "#{line}\n" }.join
212
+
213
+ # The log at +path+, opened with +mode+, for a process's output: here,
214
+ # so it's there for a failure to show, and binary, so what's copied to
215
+ # it is as the process wrote it (on Windows, CRuby's text mode would
216
+ # make its CRLF CRCRLF).
217
+ def self.log(path, mode) = File.new(path, mode, binmode: true)
218
+
219
+ # Process.spawn's options (and Open3's) that start a process in a
220
+ # group of its own, for .kill_tree to kill it with what it starts: a
221
+ # process group, or on Windows, which has none, a console's (and
222
+ # Ctrl-C at the runner's doesn't reach it, as a process group's
223
+ # doesn't).
224
+ def self.group(windows: Platform::WINDOWS) = windows ? {new_pgroup: true} : {pgroup: true}
225
+
226
+ # Kills +pid+ and what it started: its process group (see .group), or
227
+ # on Windows, where no group can be killed, its tree of children
228
+ # (taskkill /T).
229
+ def self.kill_tree(pid, windows: Platform::WINDOWS)
230
+ return Process.kill("KILL", -pid) unless windows
231
+
232
+ # (Its output read, not shown, through Open3: JRuby's system on
233
+ # Windows ignores out: and err:,
234
+ # https://github.com/jruby/jruby/issues/10081. Where there's no
235
+ # taskkill, the process alone is killed.)
236
+ begin
237
+ open3(:capture2e, "taskkill", "/T", "/F", "/PID", pid.to_s).last.success?
238
+ rescue Errno::ENOENT
239
+ Process.kill("KILL", pid)
240
+ end
241
+ end
242
+
243
+ # Open3's +name+d method (capture3, popen2e...), given +args+ (an
244
+ # environment first, or none) and +options+. On JRuby on Windows
245
+ # (+jruby_on_windows+), whose Open3 gives a process the JVM's own
246
+ # environment (https://github.com/jruby/jruby/issues/8680) and ignores
247
+ # unsetenv_others, with ENV under the environment given (or with
248
+ # unsetenv_others, every one of ENV's variables unset but those
249
+ # given), each by ENV's name for it, and the program found, and its
250
+ # arguments quoted, as Windows takes them (see .program).
251
+ def self.open3(name, *args, jruby_on_windows: Platform::JRUBY_ON_WINDOWS, **options, &)
252
+ return Open3.public_send(name, *args, **options, &) unless jruby_on_windows
253
+
254
+ none = {} #: Hash[String, String?]
255
+ env, *command = args.first.is_a?(Hash) ? args : [none, *args]
256
+ current = ENV.to_h
257
+ under = options[:unsetenv_others] ? current.keys.product([nil]).to_h : current #: Hash[String, String?]
258
+ # (Each given by ENV's name for it, whatever its case: Windows's
259
+ # names are one, but JRuby's PATH given doesn't replace ENV's Path,
260
+ # https://github.com/jruby/jruby/issues/8951.)
261
+ given = env.transform_keys { |var| current.each_key.find { |name| name.casecmp?(var) } || var }
262
+ Open3.public_send(name, under.merge(given), *program(command), **options, &)
263
+ end
264
+
265
+ # +command+ as Windows runs it: a lone String that a shell runs (with
266
+ # a space in it, say) as cmd.exe's, and otherwise with its program
267
+ # found (by name, in +path+'s directories), as the program it is, or
268
+ # with each of +exts+ added where it has no extension (a .cmd file's,
269
+ # say), and its arguments quoted (see .quote). Errno::ENOENT if
270
+ # there's no such program.
271
+ def self.program(command, path: ENV.fetch("PATH", ""), exts: ENV.fetch("PATHEXT", ".EXE").split(";"))
272
+ program = command.fetch(0)
273
+ args = command.drop(1)
274
+ return ["cmd.exe", "/c", program] if args.empty? && program.match?(SHELL)
275
+
276
+ places = program.match?(%r{[/\\]}) ? [program] : path.split(File::PATH_SEPARATOR).map { |dir| File.join(dir, program) }
277
+ found = places.flat_map { |place| File.extname(place).empty? ? exts.map { |ext| place + ext } : place }.find { |file| File.file?(file) }
278
+ raise Errno::ENOENT, program unless found
279
+
280
+ [found, *args.map { |arg| quote(arg) }]
281
+ end
282
+
283
+ # +arg+ as a Windows program's command line has it, where it's empty or
284
+ # has a space or a quote in it: in quotes, a quote in it after a
285
+ # backslash, and the backslashes before a quote, or before its end,
286
+ # doubled. (Java quotes one with a space alone, and passes its quotes
287
+ # on as they are, which the program takes out:
288
+ # https://github.com/jruby/jruby/issues/8945.)
289
+ def self.quote(arg)
290
+ return arg unless arg.empty? || arg.match?(/[\s"]/)
291
+
292
+ %("#{arg.gsub(/(\\*)"/) { "#{$1}#{$1}\\\"" }.sub(/(\\+)\z/) { "#{$1}#{$1}" }}")
293
+ end
294
+
295
+ # A thread copying +output+ to +out+, until it ends (or is closed),
296
+ # then closing both (but +out+, unless +close+: this process's
297
+ # $stdout, say).
298
+ def self.copy(output, out, close: true)
299
+ Thread.new do
300
+ # (A read at a time, not IO.copy_stream, which on Linux closing
301
+ # the pipe under it doesn't stop, and each in the log at once, for
302
+ # what waits on it to see.)
303
+ loop do
304
+ out.write(output.readpartial(CHUNK))
305
+ out.flush
306
+ end
307
+ # (At its end, EOFError, or closed under it, COPY_WAIT seconds after
308
+ # its process ended: IOError, or EBADF.)
309
+ rescue IOError, Errno::EBADF
310
+ nil
311
+ ensure
312
+ output.close
313
+ out.close if close
314
+ end
315
+ end
316
+
317
+ # A thread whose value is +waiter+'s, once +copy+ is done copying
318
+ # +outputs+, or COPY_WAIT seconds later, when they're closed under it.
319
+ def self.ended(waiter, copy, *outputs)
320
+ Thread.new do
321
+ status = waiter.value
322
+ outputs.each(&:close) unless copy.join(COPY_WAIT)
323
+ status
324
+ end
325
+ end
326
+ end
327
+ end
data/lib/duel/summary.rb CHANGED
@@ -157,10 +157,7 @@ module Duel
157
157
  def failures
158
158
  return if @failed.empty?
159
159
 
160
- header = ["Benchmark", "Failure"]
161
- header << "Most extra CPU in" unless @failed.all? { |r| top(r).empty? }
162
- rows = @failed.map { |r| row([Summary.code(cell(r.name)), failure(r), top(r)].first(header.size)) }
163
- ([row(header), "|#{"---|" * header.size}"] + rows).join("\n")
160
+ grid(["Benchmark", "Failure", "Most extra CPU in"], @failed.map { |r| [Summary.code(cell(r.name)), failure(r), top(r)] })
164
161
  end
165
162
 
166
163
  # What got worse, or the error's first line.
@@ -178,7 +175,7 @@ module Duel
178
175
  else
179
176
  # (Peak memory, which grew too much.)
180
177
  memory = result.memory #: Hash[String, Integer]
181
- "#{metric} #{mb(memory.fetch("baseline"))} → #{mb(memory.fetch("candidate"))}"
178
+ "#{metric} #{Runner.mb(memory.fetch("baseline"))} → #{Runner.mb(memory.fetch("candidate"))}"
182
179
  end
183
180
  end
184
181
  worse.join(result.mixed? ? " or " : ", ")
@@ -225,8 +222,13 @@ module Duel
225
222
  shown = Summary.verdict(r)
226
223
  [Summary.code(cell(r.name)), FAILED.include?(shown) ? "**#{shown}**" : shown, timings(r), counts(r)]
227
224
  end
228
- header = ["Benchmark", "Verdict", "Timings (ratio [interval])", "Counts an iteration"]
229
- columns = (rows.any? { |cells| !cells.last.empty? }) ? 4 : 3
225
+ grid(["Benchmark", "Verdict", "Timings (ratio [interval])", "Counts an iteration"], rows)
226
+ end
227
+
228
+ # A table of +rows+ under +header+: without its last column, where
229
+ # none of the rows has anything in it.
230
+ def grid(header, rows)
231
+ columns = (rows.any? { |cells| !cells.last.empty? }) ? header.size : header.size - 1
230
232
  ([row(header.first(columns)), "|#{"---|" * columns}"] + rows.map { |cells| row(cells.first(columns)) }).join("\n")
231
233
  end
232
234
 
@@ -257,13 +259,11 @@ module Duel
257
259
  end
258
260
  if result.more_memory?
259
261
  memory = result.memory #: Hash[String, Integer]
260
- counted << "peak_memory #{mb(memory.fetch("baseline"))} → #{mb(memory.fetch("candidate"))}"
262
+ counted << "peak_memory #{Runner.mb(memory.fetch("baseline"))} → #{Runner.mb(memory.fetch("candidate"))}"
261
263
  end
262
264
  counted.join("<br>")
263
265
  end
264
266
 
265
- def mb(bytes) = format("%.1fMB", bytes / 1_048_576.0)
266
-
267
267
  # The first +explained+ regressions, explained as the terminal report
268
268
  # explains them (and the rest named).
269
269
  def regressions(explained)
@@ -0,0 +1,43 @@
1
+ name: Perf<%= " (#{working_directory})" if working_directory %>
2
+
3
+ # Duel's performance regression guards, run by Duel's reusable workflow
4
+ # (see Duel's docs/ci.md), which this one calls with what .duel.rb sets
5
+ # that it needs: it compares a pull request with the branch it targets,
6
+ # a merge queue's group with the queue's base, a push with what was there
7
+ # before (or as c.push_base in .duel.rb says, the last commit Duel
8
+ # passed), and on a schedule, the last release (slow creep).
9
+ on:
10
+ pull_request:
11
+ merge_group: # (a merge queue's)
12
+ push:
13
+ branches: [main] # (and any other branch to guard, a stable one, say)
14
+ schedule:
15
+ - cron: "0 4 * * *"
16
+ workflow_dispatch:
17
+
18
+ # A pull request's newer push cancels its run, if that's still going, as
19
+ # only its newest commit's result counts. Every other run (a push's, say)
20
+ # runs to the end, so that each push is compared.
21
+ concurrency:
22
+ group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }}
23
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
24
+
25
+ permissions: {}
26
+
27
+ jobs:
28
+ duel:
29
+ uses: <%= REUSABLE %>/duel.yml@v<%= Duel::VERSION %>
30
+ # (Its Duel job, which runs the branch's code, only reads.)
31
+ permissions:
32
+ <% if writes? -%>
33
+ contents: write # to mark a push passed, comment on it, or keep the badge
34
+ <% else -%>
35
+ contents: read
36
+ <% end -%>
37
+ pull-requests: read # for the pull request's labels (see c.accept_label)
38
+ # (And with setup:, shell commands that set up whatever else your
39
+ # tests need: packages, services.)
40
+ with:
41
+ <% perf_inputs.each do |name, value| -%>
42
+ <%= name %>: <%= value %>
43
+ <% end -%>
@@ -0,0 +1,42 @@
1
+ name: Perf comment<%= " (#{working_directory})" if working_directory %>
2
+
3
+ # Duel's report, as a new comment on the pull request, when Duel says
4
+ # to, by Duel's reusable workflow (see Duel's docs/ci.md), which this
5
+ # one calls once the Perf workflow has run, with a token that can write,
6
+ # so a pull request from a fork gets a comment too. It checks nothing
7
+ # out and runs none of the pull request's code, only `duel comment`.
8
+ <% if accept_label -%>
9
+ #
10
+ # And when the label that accepts a regression (c.accept_label in
11
+ # .duel.rb) is added to a pull request, or taken off, it runs the Duel
12
+ # job again.
13
+ <% end -%>
14
+ #
15
+ # (The workflow must be on the default branch before it runs at all.)
16
+ on:
17
+ workflow_run:
18
+ workflows: ["Perf<%= " (#{working_directory})" if working_directory %>"]
19
+ types: [completed]
20
+ <% if accept_label -%>
21
+ pull_request_target:
22
+ types: [labeled, unlabeled]
23
+ <% end -%>
24
+
25
+ permissions: {}
26
+
27
+ jobs:
28
+ comment:
29
+ uses: <%= REUSABLE %>/duel-comment.yml@v<%= Duel::VERSION %>
30
+ permissions:
31
+ <% if accept_label -%>
32
+ actions: write # to get the report from the Perf run, and run its Duel job again
33
+ <% else -%>
34
+ actions: read # to get the report from the Perf run
35
+ <% end -%>
36
+ pull-requests: write # to comment on the pull request with the report
37
+ <% unless comment_inputs.empty? -%>
38
+ with:
39
+ <% comment_inputs.each do |name, value| -%>
40
+ <%= name %>: <%= value %>
41
+ <% end -%>
42
+ <% end -%>
@@ -1,3 +1,32 @@
1
+ <% if reusable? -%>
2
+ name: Duel comment
3
+
4
+ # Duel's Perf comment workflow, reusable: a project's own Perf comment
5
+ # workflow (as `duel init --reusable` writes it) calls it, triggered as
6
+ # that workflow is (once the Perf workflow has run, and where there's a
7
+ # label that accepts a regression, when it's added or taken off), and
8
+ # passes on what .duel.rb sets that it needs as its inputs. Its jobs run
9
+ # as the Perf comment workflow's that `duel init` writes do, and the
10
+ # caller grants what they need (actions: read, or write to run the Duel
11
+ # job again, and pull-requests: write), which each job narrows. See
12
+ # Duel's docs/ci.md.
13
+ on:
14
+ workflow_call:
15
+ inputs:
16
+ accept-label:
17
+ description: The label that accepts a regression (c.accept_label), or none
18
+ type: string
19
+ default: ""
20
+ perf-workflow:
21
+ description: The name of the Perf workflow, whose runs it comments on
22
+ type: string
23
+ default: Perf
24
+ marker:
25
+ description: What marks Duel's comments
26
+ type: string
27
+ default: duel
28
+
29
+ <% else -%>
1
30
  name: Perf comment<%= " (#{working_directory})" if working_directory %>
2
31
 
3
32
  # Duel's report, as a new comment on the pull request, when Duel says to:
@@ -33,6 +62,7 @@ on:
33
62
  types: [labeled, unlabeled]
34
63
  <% end -%>
35
64
 
65
+ <% end -%>
36
66
  permissions: {}
37
67
 
38
68
  jobs:
@@ -49,7 +79,13 @@ jobs:
49
79
  continue-on-error: true # there's none when the Duel job failed before comparing
50
80
  uses: actions/download-artifact@v8
51
81
  with:
82
+ <% if reusable? -%>
83
+ # (Every call's, where the Perf workflow called Duel's more than
84
+ # once, each in a directory of its own, which make one comment.)
85
+ pattern: duel-report*
86
+ <% else -%>
52
87
  name: duel-report
88
+ <% end -%>
53
89
  path: ${{ runner.temp }}/report
54
90
  run-id: ${{ github.event.workflow_run.id }}
55
91
  github-token: ${{ github.token }}
@@ -64,15 +100,18 @@ jobs:
64
100
  env:
65
101
  GH_TOKEN: ${{ github.token }}
66
102
  HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
103
+ <% if reusable? -%>
104
+ MARKER: ${{ inputs.marker }}
105
+ <% end -%>
67
106
  run: |
68
107
  gem install duel --version '~> <%= Duel::Comment::SINCE %>' --no-document
69
- duel comment --report "$RUNNER_TEMP/report" --head "$HEAD_SHA" --marker '<%= marker.gsub("'") { "'\\''" } %>'
108
+ duel comment --report "$RUNNER_TEMP/report" --head "$HEAD_SHA" --marker <%= reusable? ? '"$MARKER"' : "'#{marker.gsub("'") { "'\\''" }}'" %>
70
109
  <% if accept_label -%>
71
110
 
72
111
  again:
73
112
  name: Run Duel again
74
113
  if: >-
75
- github.event_name == 'pull_request_target' && github.event.label.name == '<%= accept_label.gsub("'", "''") %>'
114
+ github.event_name == 'pull_request_target' && <%= reusable? ? "inputs.accept-label != '' && github.event.label.name == inputs.accept-label" : "github.event.label.name == '#{accept_label.gsub("'", "''")}'" %>
76
115
  runs-on: ubuntu-latest
77
116
  permissions:
78
117
  actions: write # to run the Perf workflow's jobs again
@@ -83,10 +122,13 @@ jobs:
83
122
  GH_REPO: ${{ github.repository }}
84
123
  HEAD_SHA: ${{ github.event.pull_request.head.sha }}
85
124
  ACTION: ${{ github.event.action }}
125
+ <% if reusable? -%>
126
+ PERF_WORKFLOW: ${{ inputs.perf-workflow }}
127
+ <% end -%>
86
128
  run: |
87
129
  # The newest Perf run of the pull request's head commit, once it's
88
130
  # done (one that's running may have read the labels already).
89
- id=$(gh run list --workflow "Perf<%= " (#{working_directory})" if working_directory %>" --event pull_request --commit "$HEAD_SHA" --limit 1 \
131
+ id=$(gh run list --workflow "<%= reusable? ? "$PERF_WORKFLOW" : "Perf#{" (#{working_directory})" if working_directory}" %>" --event pull_request --commit "$HEAD_SHA" --limit 1 \
90
132
  --json databaseId --jq '.[0].databaseId // empty')
91
133
  [ -n "$id" ] || exit 0
92
134
  gh run watch "$id" --interval 30 > /dev/null || true