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/init.rb CHANGED
@@ -7,8 +7,10 @@ require "tmpdir"
7
7
  require "open3"
8
8
  require "optparse"
9
9
  require "pathname"
10
+ require "tempfile"
10
11
  require_relative "../duel"
11
12
  require_relative "comment"
13
+ require_relative "spawn"
12
14
 
13
15
  module Duel
14
16
  # `duel init`: sets Duel up in a gem or an app that isn't a Rails app
@@ -18,7 +20,9 @@ module Duel
18
20
  # a job that keeps a badge), or with
19
21
  # --gitlab, GitLab CI jobs to include, adds a Rake
20
22
  # task to the Rakefile, and adds StackProf to the Gemfile (for profiles
21
- # of regressions, on CRuby). It overwrites nothing without --force, and
23
+ # of regressions, on CRuby). With --reusable, its workflows are short
24
+ # ones that call Duel's reusable workflows (see Reusable), passing on
25
+ # what .duel.rb sets. It overwrites nothing without --force, and
22
26
  # writes nothing with --dry-run, or with --check, which says how the
23
27
  # workflows differ from what it writes.
24
28
  # @api private
@@ -45,6 +49,10 @@ module Duel
45
49
  # yellow, a warning's.
46
50
  LABEL_COLOR = "FBCA04"
47
51
 
52
+ # Where Duel's reusable workflows are (see Reusable), which the
53
+ # workflows it writes with --reusable call, at this release's tag.
54
+ REUSABLE = "sferik/duel-ruby/.github/workflows"
55
+
48
56
  def initialize(argv, out: $stdout, err: $stderr, input: $stdin, root: Dir.pwd)
49
57
  @argv = argv
50
58
  @out = out
@@ -59,6 +67,7 @@ module Duel
59
67
  def run
60
68
  rest = catch(:help) { parser.parse(@argv) } or return 0
61
69
  raise OptionParser::NeedlessArgument, rest.first unless rest.empty?
70
+ raise OptionParser::InvalidOption, "--reusable with --gitlab (GitLab CI has no reusable workflows)" if @reusable && @gitlab
62
71
  return rails if File.exist?(File.join(@root, "config", "application.rb"))
63
72
 
64
73
  return check if @check
@@ -82,8 +91,10 @@ module Duel
82
91
  return written.merge(top.join(gitlab_file).to_s => render("duel.gitlab-ci.yml")) if @gitlab
83
92
 
84
93
  workflows = top.join(".github", "workflows")
85
- written.merge(workflows.join(workflow_name).to_s => render("perf.yml"),
86
- workflows.join(workflow_name.sub(/\.yml\z/, "-comment.yml")).to_s => render("perf-comment.yml"))
94
+ # (With --reusable, ones that call Duel's.)
95
+ perf, comment = @reusable ? %w[perf-caller.yml perf-comment-caller.yml] : %w[perf.yml perf-comment.yml]
96
+ written.merge(workflows.join(workflow_name).to_s => render(perf),
97
+ workflows.join(workflow_name.sub(/\.yml\z/, "-comment.yml")).to_s => render(comment))
87
98
  end
88
99
 
89
100
  private
@@ -96,6 +107,7 @@ module Duel
96
107
  o.on("--check", "Say how the workflows differ from what it writes (with the same options), and write nothing") { @check = true }
97
108
  o.on("--badge", "Write a job that keeps a badge for the README (it needs contents: write)") { @badge = true }
98
109
  o.on("--gitlab", "Write GitLab CI jobs (to include from .gitlab-ci.yml), not GitHub Actions workflows") { @gitlab = true }
110
+ o.on("--reusable", "Write workflows that call Duel's reusable ones (at this release), not whole workflows") { @reusable = true }
99
111
  o.on("-h", "--help", "Print this help") do
100
112
  @out.puts o
101
113
  throw :help
@@ -120,7 +132,9 @@ module Duel
120
132
  end
121
133
 
122
134
  # Whether the file at +path+ is +content+, saying so, or how it
123
- # differs (as diff -u has it), or that it's missing.
135
+ # differs (as diff -u has it), or that it's missing. Its lines are
136
+ # compared, whatever they end with: git can check a file out with
137
+ # CRLF line endings, on Windows.
124
138
  def same?(path, content)
125
139
  full = File.join(@root, path)
126
140
  unless File.file?(full)
@@ -128,7 +142,9 @@ module Duel
128
142
  return false
129
143
  end
130
144
 
131
- differences, = Open3.capture2("diff", "-u", "-L", path, "-L", "#{path} (duel init)", full, "-", stdin_data: content)
145
+ lines = File.binread(full).gsub("\r\n", "\n")
146
+ # (Nothing, where they're the same.)
147
+ differences = diff(path, lines, content)
132
148
  said(differences.empty? ? "same" : "differs", path)
133
149
  @out.print differences
134
150
  differences.empty?
@@ -195,7 +211,7 @@ module Duel
195
211
  # What +program+ (gh, or glab) prints, run in the project, or nil where
196
212
  # it fails (which it says) or isn't there.
197
213
  def cli(program, *args)
198
- out, err, status = Open3.capture3(program, *args, chdir: @root)
214
+ out, err, status = Spawn.open3(:capture3, program, *args, chdir: @root)
199
215
  return out if status.success?
200
216
 
201
217
  @err.puts "duel init: #{program} #{args.first(2).join(" ")}: #{err.strip}"
@@ -203,6 +219,20 @@ module Duel
203
219
  nil
204
220
  end
205
221
 
222
+ # How +lines+ (the file at +path+'s) differ from +content+, as diff -u
223
+ # has it.
224
+ # mutant:disable -- the temporary file's name is never shown (diff
225
+ # labels it), and binmode only matters on Windows (where text mode
226
+ # would write CRLF), which mutant doesn't run on
227
+ def diff(path, lines, content)
228
+ Tempfile.create("duel-init") do |file|
229
+ file.binmode
230
+ file.write(lines)
231
+ file.close
232
+ Spawn.open3(:capture2, "diff", "-u", "-L", path, "-L", "#{path} (duel init)", file.path, "-", stdin_data: content).first
233
+ end
234
+ end
235
+
206
236
  # What Duel compares: a pull request, or on GitLab, a merge request.
207
237
  def request = @gitlab ? "merge request" : "pull request"
208
238
 
@@ -217,7 +247,8 @@ module Duel
217
247
  return if @dry_run
218
248
 
219
249
  FileUtils.mkdir_p(File.dirname(full))
220
- File.write(full, content)
250
+ # (Its lines end with LF, as Duel's templates do, on Windows too.)
251
+ File.binwrite(full, content)
221
252
  end
222
253
 
223
254
  # Adds Duel's Rake task to the Rakefile, if there is one without it.
@@ -288,7 +319,52 @@ module Duel
288
319
  "\nNext:\n#{steps.each_with_index.map { |step, i| " #{i + 1}. #{step}\n" }.join}"
289
320
  end
290
321
 
291
- def render(name) = ERB.new(File.read(File.join(TEMPLATES, "#{name}.erb")), trim_mode: "-").result(binding)
322
+ # The template +name+, rendered. (Read with LF line endings, as Git
323
+ # may check it out with CRLF on Windows, where CRuby reads text that
324
+ # way, but JRuby doesn't.)
325
+ def render(name) = ERB.new(File.read(File.join(TEMPLATES, "#{name}.erb")).gsub("\r\n", "\n"), trim_mode: "-").result(binding)
326
+
327
+ # Whether the templates are rendered as Duel's reusable workflows (see
328
+ # Reusable).
329
+ # mutant:disable -- nil (a mutation's) renders as false does
330
+ def reusable? = false
331
+
332
+ # Whether the workflow's jobs write to the repository: to mark a push
333
+ # passed, comment on one, or keep the badge.
334
+ def writes? = mark_passed? || comment_on_pushes? || badge?
335
+
336
+ # What the workflow it writes with --reusable passes on to Duel's
337
+ # Perf workflow, as its inputs (see Reusable): what .duel.rb sets that
338
+ # isn't the input's default, each as YAML has it.
339
+ def perf_inputs
340
+ {
341
+ "ruby-version" => ruby_version,
342
+ "version-file" => (true if version_file),
343
+ "working-directory" => working_directory,
344
+ "accept-label" => accept_label,
345
+ "release-tags" => (set_release_tags unless set_release_tags == "v*"),
346
+ "comment-on-pushes" => (true if comment_on_pushes?),
347
+ "mark-passed" => (true if mark_passed?),
348
+ "badge" => (true if badge?),
349
+ "passed-branch" => (passed_branch if working_directory),
350
+ "badge-branch" => (badge_branch if working_directory && badge?),
351
+ "marker" => (marker if working_directory)
352
+ }.compact.transform_values(&:to_json)
353
+ end
354
+
355
+ # The glob release tags match, as .duel.rb sets it (c.release_tags),
356
+ # or nil where it doesn't load. (The whole workflows take the
357
+ # conventions', and say to set RELEASE_TAGS to match.)
358
+ def set_release_tags = settings&.release_tags
359
+
360
+ # The same, for Duel's Perf comment workflow.
361
+ def comment_inputs
362
+ {
363
+ "accept-label" => accept_label,
364
+ "perf-workflow" => ("Perf (#{working_directory})" if working_directory),
365
+ "marker" => (marker if working_directory)
366
+ }.compact.transform_values(&:to_json)
367
+ end
292
368
 
293
369
  # The project's configuration, as the conventions have it.
294
370
  def config = Config.new(@root)
@@ -373,10 +449,12 @@ module Duel
373
449
  def ruby_version = (RUBY_VERSION[/\A\d+\.\d+/] unless version_file)
374
450
 
375
451
  # The root of the project's git repository: the project's own
376
- # directory, outside one.
452
+ # directory, outside one. (Asked once: the templates ask often.)
377
453
  def repository
378
- top, _, status = Open3.capture3("git", "-C", @root, "rev-parse", "--show-toplevel")
379
- status.success? ? top.chomp : @root
454
+ @repository ||= begin
455
+ top, _, status = Spawn.open3(:capture3, "git", "-C", @root, "rev-parse", "--show-toplevel")
456
+ status.success? ? top.chomp : @root
457
+ end
380
458
  end
381
459
 
382
460
  # The project's directory, from its repository's root ("gems/parser"),
@@ -386,10 +464,13 @@ module Duel
386
464
  path unless path == "."
387
465
  end
388
466
 
389
- # perf.yml, or for a project in a subdirectory of its repository,
390
- # perf-<its path>.yml (perf-gems-parser.yml), so that each of a
391
- # monorepo's projects can have its own.
392
- def workflow_name = working_directory&.then { |dir| "perf-#{dir.tr("/", "-")}.yml" } || "perf.yml"
467
+ # +name+, or for a project in a subdirectory of its repository,
468
+ # +name+-<its path> (perf-gems-parser), so that each of a monorepo's
469
+ # projects has its own.
470
+ def suffixed(name) = [name, working_directory&.tr("/", "-")].compact.join("-")
471
+
472
+ # perf.yml, or for a project in a subdirectory, perf-<its path>.yml.
473
+ def workflow_name = "#{suffixed("perf")}.yml"
393
474
 
394
475
  # What marks the workflow's comment on a pull request.
395
476
  def marker = ["duel", working_directory].compact.join(" ")
@@ -397,7 +478,7 @@ module Duel
397
478
  # Where the GitLab CI jobs are, from the repository's root:
398
479
  # .gitlab/duel.gitlab-ci.yml, or for a project in a subdirectory,
399
480
  # .gitlab/duel-<its path>.gitlab-ci.yml.
400
- def gitlab_file = File.join(".gitlab", "#{["duel", working_directory&.tr("/", "-")].compact.join("-")}.gitlab-ci.yml")
481
+ def gitlab_file = File.join(".gitlab", "#{suffixed("duel")}.gitlab-ci.yml")
401
482
 
402
483
  # The GitLab CI job's name: duel, or for a project in a subdirectory,
403
484
  # duel:<its path>, so each of a monorepo's projects has its own.
@@ -413,10 +494,53 @@ module Duel
413
494
 
414
495
  # What names the branches that mark the last commit Duel passed in a
415
496
  # push, before the branch pushed's name (see Baseline#passed_branch).
416
- def passed_branch = ["duel/passed", working_directory&.tr("/", "-")].compact.join("-")
497
+ def passed_branch = suffixed("duel/passed")
417
498
 
418
499
  # The branch that holds the workflow's badge: duel/badge, or for a
419
500
  # project in a subdirectory, duel/badge-<its path>.
420
- def badge_branch = ["duel/badge", working_directory&.tr("/", "-")].compact.join("-")
501
+ def badge_branch = suffixed("duel/badge")
502
+
503
+ # Duel's reusable workflows, which the workflows `duel init --reusable`
504
+ # writes call: .github/workflows/duel.yml (the Perf workflow's jobs)
505
+ # and duel-comment.yml (the Perf comment workflow's), in Duel's
506
+ # repository. They're the templates `duel init` writes from, rendered
507
+ # with each setting of .duel.rb they need as an input (and a job or a
508
+ # step that would be left out run only where its input says to).
509
+ # `rake workflows` writes them, and a test says they're what's there.
510
+ # @api private
511
+ # (Made with no arguments: Reusable.new([]).)
512
+ class Reusable < Init
513
+ # The files, from the repository's root, to what's in them.
514
+ def files
515
+ workflows = File.join(".github", "workflows")
516
+ {File.join(workflows, "duel.yml") => render("perf.yml"), File.join(workflows, "duel-comment.yml") => render("perf-comment.yml")}
517
+ end
518
+
519
+ private
520
+
521
+ def reusable? = true
522
+
523
+ # Each an input's (see the templates' workflow_call).
524
+ def accept_label = "${{ inputs.accept-label }}"
525
+
526
+ def release_tags = "${{ inputs.release-tags }}"
527
+
528
+ def ruby_version = "${{ inputs.ruby-version }}"
529
+
530
+ def working_directory = "${{ inputs.working-directory }}"
531
+
532
+ def passed_branch = "${{ inputs.passed-branch }}"
533
+
534
+ def badge_branch = "${{ inputs.badge-branch }}"
535
+
536
+ # Each in, run only where its input says to.
537
+ def version_file = true
538
+
539
+ def comment_on_pushes? = true
540
+
541
+ def mark_passed? = true
542
+
543
+ def badge? = true
544
+ end
421
545
  end
422
546
  end