duel 0.1.3 → 0.1.5

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.
data/lib/duel/native.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "etc"
4
- require "fiddle"
5
3
  require "rbconfig"
6
4
 
7
5
  module Duel
@@ -16,9 +14,19 @@ module Duel
16
14
  CPU = RbConfig::CONFIG["host_cpu"]
17
15
  UINT64 = 8 # bytes, the size of every count read
18
16
 
17
+ # Loads Fiddle, as the first C function is made (see .function): not
18
+ # as Duel loads, which in a worker is before its version (in fiddle's
19
+ # own repository, fiddle).
20
+ # mutant:disable -- the tests' process has Fiddle loaded from the start,
21
+ # so a require changes nothing there: WorkerTest's
22
+ # test_loads_no_gem_a_version_could_be_before_the_version checks it, in
23
+ # a process of its own
24
+ def self.require_fiddle = require("fiddle")
25
+
19
26
  # A C function of the process's own libraries, by name, taking and
20
27
  # returning Fiddle types (:INT, :LONG, :VOIDP...).
21
28
  def self.function(name, args, returns)
29
+ require_fiddle
22
30
  Fiddle::Function.new(Fiddle.dlopen(nil)[name], args.map { |type| Fiddle.const_get(:"TYPE_#{type}") },
23
31
  Fiddle.const_get(:"TYPE_#{returns}"))
24
32
  end
@@ -145,23 +153,37 @@ module Duel
145
153
 
146
154
  # This process's resident memory in bytes, or nil where it can't tell.
147
155
  module Memory
148
- STATM = "/proc/self/statm"
149
- # (Never false: Steep doesn't see that false || nil is nil.)
150
- MACOS = (FIDDLE && MacOS.open) || nil #: MacOS?
156
+ # (Its VmRSS line, in kB: no page size to know, as /proc/self/statm's
157
+ # pages need, which would take Etc, a gem a worker would load before
158
+ # its version.)
159
+ STATUS = "/proc/self/status"
160
+ # macOS's counter, which reads resident memory there, or nil: opened
161
+ # as resident memory is first read, as opening it loads Fiddle.
162
+ # mutant:disable -- kept only so as not to open it again: it's the
163
+ # same counter however often it's opened
164
+ def self.macos_counter
165
+ return @macos if defined?(@macos)
166
+
167
+ # (Never false: Steep doesn't see that false || nil is nil.)
168
+ @macos = (FIDDLE && MacOS.open) || nil #: MacOS?
169
+ end
151
170
 
152
171
  # From /proc (Linux), else from macOS's counter, else nil.
153
- def self.resident(statm: STATM, macos: MACOS)
154
- return macos&.resident unless File.readable?(statm)
172
+ def self.resident(status: STATUS, macos: macos_counter)
173
+ return macos&.resident unless File.readable?(status)
155
174
 
156
- # (The page size is always known.)
157
- page_size = Etc.sysconf(Etc::SC_PAGESIZE) #: Integer
158
- Integer(File.read(statm).split.fetch(1)) * page_size
175
+ kb = File.read(status)[/^VmRSS:\s+(\d+) kB$/, 1] #: String
176
+ Integer(kb) * 1024
159
177
  end
160
178
 
161
179
  # The memory a benchmark's peak is measured in (see MemorySample),
162
180
  # in bytes, or nil where it can't tell: resident memory, but on
163
181
  # JRuby, the JVM's heap in use.
164
182
  def self.used = resident
183
+
184
+ # Collects every object that's garbage, so that what's in use (see
185
+ # .used) is what's live: a full collection, swept at once.
186
+ def self.collect = GC.start(full_mark: true, immediate_sweep: true)
165
187
  end
166
188
 
167
189
  # simplecov:disable JRuby's heap is the JVM's (JRuby's test run covers
@@ -175,6 +197,11 @@ module Duel
175
197
  HEAP = java.lang.management.ManagementFactory.getMemoryMXBean
176
198
 
177
199
  def self.used = HEAP.getHeapMemoryUsage.getUsed
200
+
201
+ # JRuby's GC.start collects nothing. The JVM's own call does:
202
+ # without it, a peak was measured above garbage that the benchmark's
203
+ # objects then made room for, and came out tens of megabytes short.
204
+ def self.collect = java.lang.System.gc
178
205
  end
179
206
  end
180
207
  # simplecov:enable
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rake"
4
- require_relative "cli"
5
4
 
6
5
  module Duel
7
6
  # A rake task running `duel` with +args+:
@@ -9,12 +8,22 @@ module Duel
9
8
  # require "duel/rake_task"
10
9
  # Duel::RakeTask.new(:perf)
11
10
  # Duel::RakeTask.new(:perf_since_release, "--base", "last-release")
11
+ #
12
+ # Duel itself loads only as the task runs: a Rakefile that defines the
13
+ # task loads nothing more for its other tasks (rake test, say).
12
14
  class RakeTask
13
15
  include Rake::DSL
14
16
 
17
+ # Loads Duel's command, as the task runs.
18
+ # mutant:disable -- the tests' process has Duel loaded from the start, so
19
+ # a require changes nothing there: RakeTaskTest's
20
+ # test_the_task_loads_duel_as_it_runs checks it, in a process of its own
21
+ def self.load_duel = require_relative("cli")
22
+
15
23
  def initialize(name = :duel, *args)
16
24
  desc "Fail if this tree is slower than its baseline at any benchmark (duel)"
17
25
  task(name) do
26
+ RakeTask.load_duel
18
27
  case CLI.new(args).run
19
28
  when 1 then abort "duel: slower than the baseline"
20
29
  when 2 then abort "duel: couldn't compare"
data/lib/duel/runner.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fileutils"
4
- require "json"
5
4
  require "rbconfig"
6
5
  require "socket"
7
6
  require "tmpdir"
@@ -9,6 +8,7 @@ require_relative "../duel"
9
8
  require_relative "stats"
10
9
  require_relative "profile"
11
10
  require_relative "across"
11
+ require_relative "message"
12
12
  require_relative "resource"
13
13
  require_relative "rubies"
14
14
  require_relative "process_sample"
@@ -85,9 +85,10 @@ module Duel
85
85
  attr_reader :label, :benchmarks, :metrics, :memory, :profiler, :yjit
86
86
 
87
87
  # The Ruby it runs on (RUBY_DESCRIPTION), the native extensions it
88
- # loaded (see Worker.extensions), and the directories gems are
89
- # installed in (see Worker.gem_dirs).
90
- attr_reader :description, :extensions, :gem_dirs
88
+ # loaded (see Worker.extensions), those it loaded twice (see
89
+ # Worker.twice), and the directories gems are installed in (see
90
+ # Worker.gem_dirs).
91
+ attr_reader :description, :extensions, :twice, :gem_dirs
91
92
 
92
93
  # Where a worker on +ruby+ loads Duel from, ahead of anything else
93
94
  # (a version's own bundle, which may not have Duel, or an older one):
@@ -128,13 +129,15 @@ module Duel
128
129
  @yjit = hello.fetch("yjit")
129
130
  @description = hello.fetch("ruby")
130
131
  @extensions = hello.fetch("extensions")
132
+ @twice = hello.fetch("twice")
131
133
  @gem_dirs = hello.fetch("gem_dirs")
132
134
  end
133
135
 
134
136
  # Sends a request, and returns the reply, or raises Failed with its
135
137
  # error.
136
138
  def request(op, name, **args)
137
- @socket.puts JSON.generate({"op" => op, "name" => name, **args})
139
+ # (By name: +args+ come by keyword, as Symbols.)
140
+ Message.write(@socket, {"op" => op, "name" => name, **args.transform_keys(&:to_s)})
138
141
  reply = read
139
142
  raise Failed, "#{@label}: #{reply.fetch("error")}\n #{reply.fetch("backtrace").join("\n ")}" if reply.key?("error")
140
143
 
@@ -185,8 +188,7 @@ module Duel
185
188
 
186
189
  def read
187
190
  hung("answer") unless @socket.wait_readable(@timeout)
188
- line = @socket.gets or raise Failed, "#{@label} worker exited"
189
- JSON.parse(line)
191
+ Message.read(@socket) or raise Failed, "#{@label} worker exited"
190
192
  end
191
193
 
192
194
  # Kills a worker that didn't +what+ within the timeout, and fails.
@@ -366,10 +368,17 @@ module Duel
366
368
  # be of its code. An installed gem's extension (in the directories
367
369
  # gems are installed in, by the worker that loaded it) is in no
368
370
  # version's tree, even in a bundle there (vendor/bundle, say, which
369
- # both versions may load from).
371
+ # both versions may load from). And fails if a version's workers
372
+ # loaded one twice (see Worker.twice): the candidate's, that something
373
+ # required before Duel loaded the version, and then its own over it.
370
374
  def check_builds
371
375
  workers = {"baseline" => @bases.first, "candidate" => @candidates.first}
372
376
  workers.each do |side, worker|
377
+ worker.twice.each do |feature, paths|
378
+ raise Failed, "the #{side} loaded #{feature} twice, from #{paths.join(" and ")}: something required it before " \
379
+ "Duel loaded the #{side}, and both stay loaded. Require what loads it in a block that runs once the version " \
380
+ "has loaded (c.in_worker, in .duel.rb), not as .duel.rb or a plugin loads"
381
+ end
373
382
  other = (side == "baseline") ? "candidate" : "baseline"
374
383
  theirs = workers.fetch(other).then { |w| w.extensions.select { |_, path| inside?(path, other, w) } }
375
384
  worker.extensions.each do |feature, path|
@@ -40,14 +40,18 @@ Duel.configure do |c|
40
40
 
41
41
  # Which runs a regression fails: :every_run, or :pull_requests, for a
42
42
  # project where people push to main directly (on GitHub Actions, another
43
- # run warns and passes, and a pushed commit gets the comment instead).
43
+ # run warns and passes, and a pushed commit gets the comment instead,
44
+ # from a workflow job written only where this is set: see docs/ci.md).
44
45
  # c.fail_on = :every_run
45
46
 
46
- # A label that accepts a regression in a pull request (nil for none).
47
- # c.accept_label = "duel: accept"
47
+ # A label that accepts a regression in a pull request, for a change
48
+ # that's slower on purpose (nil for none).
49
+ c.accept_label = "duel: accept"
48
50
 
49
51
  # What the workflow compares a push with: :before (the commit before it),
50
52
  # or :last_passed (the last commit Duel passed, which a branch,
51
53
  # duel/passed/main, marks), so small slowdowns add up until they fail.
54
+ # (A workflow job moves the branch, written only where this is set: see
55
+ # docs/ci.md.)
52
56
  # c.push_base = :before
53
57
  end
@@ -9,7 +9,8 @@ name: Perf comment<%= " (#{working_directory})" if working_directory %>
9
9
  # the pull request's code, and of what the Duel job kept (where that code
10
10
  # ran), it trusts only what that job could have said: the pull request's
11
11
  # number, if it names the pull request the run compared, and the comment
12
- # and the status, if they're ones Duel says. (The workflow must be on the
12
+ # and the status, if they're ones Duel says. A fork's report, which the
13
+ # fork's code wrote, is shown as text. (The workflow must be on the
13
14
  # default branch before it runs at all.)
14
15
  on:
15
16
  workflow_run:
@@ -47,13 +48,28 @@ jobs:
47
48
  COMMENT=$(said comment 'post|change')
48
49
  STATUS=$(said status 'passed|failed|neutral')
49
50
  [ -n "$PR" ] && [ -n "$COMMENT" ] && [ -n "$STATUS" ] || exit 0
50
- [ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq .head.sha)" = "$HEAD_SHA" ] || exit 0
51
+ # (The pull request's head commit, and the repository it's from.)
52
+ head=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '"\(.head.sha) \(.head.repo.full_name)"')
53
+ [ "${head%% *}" = "$HEAD_SHA" ] || exit 0
54
+ # The report, as Markdown for a branch of this repository (whose
55
+ # author can push here anyway), but for a fork's, as text: its run
56
+ # ran the fork's code, which could have written anything in it. In
57
+ # a code block, fenced with more backticks than any run of them in
58
+ # the report, it can't mention anyone, link anywhere or show an
59
+ # image. (And it's cut short where a comment couldn't hold it.)
60
+ report() {
61
+ if [ "${head#* }" = "$GITHUB_REPOSITORY" ]; then cat "$RUNNER_TEMP/report/duel.md" && return; fi
62
+ fence=$(grep -o '``*' "$RUNNER_TEMP/report/duel.md" | awk '{ if (length > n) n = length } END { n = (n < 3) ? 3 : n + 1; while (n-- > 0) printf "`" }')
63
+ printf 'From a fork, so shown as text:\n\n%s\n' "$fence"
64
+ head -c 60000 "$RUNNER_TEMP/report/duel.md"
65
+ printf '\n%s\n' "$fence"
66
+ }
51
67
  marker="<!-- <%= marker %>"
52
68
  # The first line of the last comment Duel posted here: its marker,
53
69
  # with its status (none in Duel 0.1.0's, which counts as another).
54
70
  last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \
55
71
  --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\") or . == \"$marker -->\")" | tail -n 1)
56
72
  if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
57
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
73
+ { echo "$marker status=$STATUS -->"; report; } > "$RUNNER_TEMP/comment.md"
58
74
  gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
59
75
  fi
@@ -173,6 +173,7 @@ jobs:
173
173
  path: ${{ runner.temp }}/duel/
174
174
  if-no-files-found: ignore # none when Duel didn't get to compare
175
175
  retention-days: 1
176
+ <% if comment_on_pushes? -%>
176
177
 
177
178
  # The report, as a new comment on a pushed commit, which notifies its
178
179
  # author, when Duel says to, as for a pull request (see the Perf comment
@@ -209,6 +210,8 @@ jobs:
209
210
  { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
210
211
  gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
211
212
  fi
213
+ <% end -%>
214
+ <% if mark_passed? -%>
212
215
 
213
216
  # With c.push_base = :last_passed in .duel.rb, a push is compared with
214
217
  # the last commit Duel passed, which a branch marks (each branch pushed
@@ -251,14 +254,19 @@ jobs:
251
254
  gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null || blocked
252
255
  exit 0
253
256
  fi
254
- compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status)
257
+ compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
258
+ # (GitHub can't compare commits with no history in common.)
259
+ *"No common ancestor"*) compared=unrelated ;;
260
+ esac
255
261
  case "$compared" in
256
262
  # (A newer push's run moved it, or this one's did, run again.)
257
263
  ahead | identical) ;;
258
- # (History rewritten, a force push's, say: moved anyway.)
259
- diverged | behind) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
264
+ # (History rewritten, a force push's, say, or replaced: moved anyway.)
265
+ diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
260
266
  *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
261
267
  esac
268
+ <% end -%>
269
+ <% if badge? -%>
262
270
 
263
271
  # Duel's badge, for the README: green (perf guarded) or red (perf
264
272
  # regressed), as the last push to the default branch that Duel compared
@@ -270,10 +278,18 @@ jobs:
270
278
  #
271
279
  # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/<%= badge_branch %>/badge.svg)](https://github.com/sferik/duel-ruby)
272
280
  #
273
- # It checks nothing out and runs none of the code.
281
+ # It checks nothing out, runs none of the code, and fetches nothing.
274
282
  badge:
275
283
  name: Update the badge
276
284
  needs: duel
285
+ # One at a time, so that one run's check of which push the badge
286
+ # shows can't interleave with another run's write (a push's run
287
+ # doesn't wait for the one before it). A badge job that's running is
288
+ # never cancelled, and one that's waiting gives way to a newer push's,
289
+ # whose result the badge should show anyway.
290
+ concurrency:
291
+ group: ${{ github.workflow }}-badge
292
+ cancel-in-progress: false
277
293
  if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && (needs.duel.result == 'failure' || needs.duel.outputs.status == 'passed' || needs.duel.outputs.status == 'failed') }}
278
294
  runs-on: ubuntu-latest
279
295
  permissions:
@@ -285,7 +301,6 @@ jobs:
285
301
  BRANCH: <%= badge_branch %>
286
302
  RESULT: ${{ needs.duel.result }}
287
303
  STATUS: ${{ needs.duel.outputs.status }}
288
- BADGES: https://raw.githubusercontent.com/sferik/duel-ruby/main/assets
289
304
  run: |
290
305
  # (Git can't make a branch where another's name is a folder of its
291
306
  # name: a branch named duel blocks duel/passed/main. If one does,
@@ -304,11 +319,59 @@ jobs:
304
319
  # Its commit names the push.)
305
320
  last=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BRANCH" --jq .commit.message 2>/dev/null | sed -n 's/^Duel.s badge, for \([0-9a-f]*\)$/\1/p') || true
306
321
  if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
307
- badge="badge.svg"
308
- if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then badge="badge-regressed.svg"; fi
309
- curl -fsSL "$BADGES/$badge" -o "$RUNNER_TEMP/badge.svg"
322
+ # (Its image, written here rather than fetched from anywhere: green,
323
+ # or red where the Duel job failed or found a regression.)
324
+ state="perf guarded" width=133 top="#34D058" bottom="#28A745" right="76.939C78.629 0 80 1.343 80"
325
+ if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then
326
+ state="perf regressed" width=142 top="#EA4A5A" bottom="#D73A49" right="85.939C87.629 0 89 1.343 89"
327
+ fi
328
+ cat > "$RUNNER_TEMP/badge.svg" <<SVG
329
+ <svg xmlns="http://www.w3.org/2000/svg" width="$width" height="20">
330
+ <title>Duel - $state</title>
331
+ <defs>
332
+ <linearGradient id="duel-fill" x1="50%" y1="0%" x2="50%" y2="100%">
333
+ <stop stop-color="#444D56" offset="0%"></stop>
334
+ <stop stop-color="#24292E" offset="100%"></stop>
335
+ </linearGradient>
336
+ <linearGradient id="state-fill" x1="50%" y1="0%" x2="50%" y2="100%">
337
+ <stop stop-color="$top" offset="0%"></stop>
338
+ <stop stop-color="$bottom" offset="100%"></stop>
339
+ </linearGradient>
340
+ </defs>
341
+ <g fill="none" fill-rule="evenodd">
342
+ <g font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
343
+ <path d="M0,3 C0,1.3431 1.3552,0 3.02702703,0 L53,0 L53,20 L3.02702703,20 C1.3552,20 0,18.6569 0,17 L0,3 Z" fill="url(#duel-fill)" fill-rule="nonzero"></path>
344
+ <text fill="#010101" fill-opacity=".3">
345
+ <tspan x="22.1981982" y="15" aria-hidden="true">Duel</tspan>
346
+ </text>
347
+ <text fill="#FFFFFF">
348
+ <tspan x="22.1981982" y="14">Duel</tspan>
349
+ </text>
350
+ </g>
351
+ <g transform="translate(53)" font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
352
+ <path d="M0 0h$right 3v14c0 1.657-1.37 3-3.061 3H0V0z" fill="url(#state-fill)" fill-rule="nonzero"></path>
353
+ <text fill="#010101" fill-opacity=".3" aria-hidden="true">
354
+ <tspan x="4" y="15">$state</tspan>
355
+ </text>
356
+ <text fill="#FFFFFF">
357
+ <tspan x="4" y="14">$state</tspan>
358
+ </text>
359
+ </g>
360
+ <g fill="#959DA5">
361
+ <path d="M4.6 3.6L6.9 4.5L14.3 11.9L12.9 13.3L5.5 5.9Z M17.4 3.6L15.1 4.5L7.7 11.9L9.1 13.3L16.5 5.9Z"></path>
362
+ <circle cx="16.6" cy="15.6" r="1.1"></circle>
363
+ <circle cx="5.4" cy="15.6" r="1.1"></circle>
364
+ </g>
365
+ <g stroke="#959DA5" stroke-linecap="round">
366
+ <path d="M12.1 14.5L15.5 11.1 M9.9 14.5L6.5 11.1" stroke-width="1.6"></path>
367
+ <path d="M13.8 12.8L16 15 M8.2 12.8L6 15" stroke-width="1.8"></path>
368
+ </g>
369
+ </g>
370
+ </svg>
371
+ SVG
310
372
  tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
311
373
  gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
312
374
  commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
313
375
  gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
314
376
  gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
377
+ <% end -%>
data/lib/duel/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Duel
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/duel/worker.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
3
  require "rbconfig"
5
4
  require "socket"
6
5
  require_relative "../duel"
7
6
  require_relative "context"
7
+ require_relative "message"
8
8
  require_relative "profile"
9
9
 
10
10
  module Duel
@@ -13,8 +13,8 @@ module Duel
13
13
  # WorkerProcess) with Worker.main(VERSION_ROOT, CANDIDATE_ROOT, CONTROL
14
14
  # [, CONFIG]), it gives its main object the DSL, loads the benchmarks,
15
15
  # connects to the runner's Unix socket at CONTROL, sends what it has
16
- # (see #hello), and answers one JSON request per line with one JSON
17
- # reply. (Not over stdin: a JRuby started by JRuby sees its stdin pipe
16
+ # (see #hello), and answers each request with a reply (see Message).
17
+ # (Not over stdin: a JRuby started by JRuby sees its stdin pipe
18
18
  # closed.)
19
19
  # @api private
20
20
  class Worker
@@ -25,6 +25,9 @@ module Duel
25
25
 
26
26
  def self.main(version_root, candidate_root, control, config_path = nil)
27
27
  config = config_path ? Duel.load_config(config_path) : (Duel.config = Config.new(candidate_root))
28
+ # (What was loaded before the version, which loading it may take off
29
+ # $LOADED_FEATURES, to load its own over: see Worker.twice.)
30
+ before = $LOADED_FEATURES.dup
28
31
  config.load_into(File.expand_path(version_root))
29
32
  TOPLEVEL_BINDING.receiver.extend(DSL)
30
33
  config.benchmark_files.each { |file| load file }
@@ -34,11 +37,13 @@ module Duel
34
37
  begin
35
38
  # The benchmarks are the only work in this process: it starts tidy.
36
39
  GC.start
37
- worker = new(config.metrics, config.measures, hooks: config.hooks)
40
+ worker = new(config.metrics, config.measures, hooks: config.hooks, before: before)
38
41
  begin
39
42
  socket = UNIXSocket.new(control)
40
- socket.puts JSON.generate(worker.hello)
41
- socket.each_line { |line| socket.puts JSON.generate(worker.handle(JSON.parse(line))) }
43
+ Message.write(socket, worker.hello)
44
+ while (request = Message.read(socket))
45
+ Message.write(socket, worker.handle(request))
46
+ end
42
47
  ensure
43
48
  worker.close
44
49
  end
@@ -49,8 +54,10 @@ module Duel
49
54
 
50
55
  # Its metric instances: the plugins' outermost, then the built-in
51
56
  # ones, CPU innermost (see Worker.made). +hooks+: the configuration's
52
- # (see Harness).
53
- def initialize(plugin_metrics = [], measures = {}, hooks: NO_HOOKS)
57
+ # (see Harness). +before+: the features loaded before the version was
58
+ # (see Worker.main).
59
+ def initialize(plugin_metrics = [], measures = {}, hooks: NO_HOOKS, before: [])
60
+ @before = before
54
61
  @metrics, @specs = Worker.made(plugin_metrics + BUILT_IN, measures)
55
62
  @benchmarks = Duel.benchmarks
56
63
  @hooks = hooks
@@ -88,7 +95,7 @@ module Duel
88
95
  def hello
89
96
  {"benchmarks" => @benchmarks.values.map(&:to_h), "metrics" => @specs, "ruby" => RUBY_DESCRIPTION,
90
97
  "memory" => !Native::Memory.used.nil?, "profiler" => Worker.profiler?, "yjit" => Worker.yjit?,
91
- "extensions" => Worker.extensions, "gem_dirs" => Worker.gem_dirs}
98
+ "extensions" => Worker.extensions, "twice" => Worker.twice(@before + $LOADED_FEATURES), "gem_dirs" => Worker.gem_dirs}
92
99
  end
93
100
 
94
101
  # The native extensions loaded (CRuby's and TruffleRuby's builds, and
@@ -96,13 +103,26 @@ module Duel
96
103
  # from the directory on the load path it was found in (the deepest,
97
104
  # of several), "my_gem/my_gem.bundle", say, or else its file's name.
98
105
  # (For the runner to check each version loaded its own builds: see
106
+ # Lane#check_builds.) Of a feature loaded from more than one file, the
107
+ # last.
108
+ def self.extensions(features = $LOADED_FEATURES, load_path = $LOAD_PATH) = loaded(features, load_path).transform_values(&:last)
109
+
110
+ # The native extensions of +features+ loaded from more than one file,
111
+ # as {feature => real paths}, in the order they loaded. In a baseline
112
+ # worker, one of the candidate's that something required before the
113
+ # worker loaded the version (Duel's own json, in json's repository,
114
+ # say), and then the version's own: as an extension can't be
115
+ # unloaded, both stay loaded. (For the runner to fail: see
99
116
  # Lane#check_builds.)
100
- def self.extensions(features = $LOADED_FEATURES, load_path = $LOAD_PATH)
117
+ def self.twice(features, load_path = $LOAD_PATH) = loaded(features, load_path).select { |_, paths| paths.size > 1 }
118
+
119
+ # The native extensions loaded, as {feature => real paths}.
120
+ def self.loaded(features, load_path)
101
121
  dirs = load_path.map { |dir| File.join(File.expand_path(dir), "") }
102
- features.select { |feature| File.absolute_path?(feature) && feature.end_with?(".#{RbConfig::CONFIG.fetch("DLEXT")}") }.to_h do |path|
122
+ features.select { |feature| File.absolute_path?(feature) && feature.end_with?(".#{RbConfig::CONFIG.fetch("DLEXT")}") }.group_by do |path|
103
123
  dir = dirs.select { |prefix| path.start_with?(prefix) }.max_by(&:size)
104
- [dir ? path.delete_prefix(dir) : File.basename(path), File.realpath(path)]
105
- end
124
+ dir ? path.delete_prefix(dir) : File.basename(path)
125
+ end.transform_values { |paths| paths.map { |path| File.realpath(path) }.uniq }
106
126
  end
107
127
 
108
128
  # The directories gems are installed in, Gem.path's that are there, as
data/sig/duel/init.rbs CHANGED
@@ -22,6 +22,8 @@ module Duel
22
22
  @force: bool?
23
23
  @dry_run: bool?
24
24
  @gitlab: bool?
25
+ @badge: bool?
26
+ @settings: Config?
25
27
  @stackprof: bool?
26
28
 
27
29
  def initialize: (Array[String] argv, ?out: _Output, ?err: _Output, ?root: String) -> void
@@ -67,6 +69,18 @@ module Duel
67
69
  # The project's configuration, as the conventions have it.
68
70
  def config: () -> Config
69
71
 
72
+ # The project's settings, which decide the workflow's jobs (nil where
73
+ # .duel.rb doesn't load).
74
+ def settings: () -> Config?
75
+
76
+ def load_settings: () -> Config?
77
+
78
+ # Whether the workflow comments on a pushed commit.
79
+ def comment_on_pushes?: () -> bool
80
+
81
+ # Whether the workflow marks a push passed.
82
+ def mark_passed?: () -> bool
83
+
70
84
  # The library the gemspec names, or nil.
71
85
  def library: () -> String?
72
86
 
@@ -104,6 +118,9 @@ module Duel
104
118
  # The branch that marks the last commit Duel passed in a push.
105
119
  def passed_branch: () -> String
106
120
 
121
+ # Whether the workflow keeps a badge for the README (--badge).
122
+ def badge?: () -> bool?
123
+
107
124
  # The branch that holds the workflow's badge.
108
125
  def badge_branch: () -> String
109
126
 
@@ -0,0 +1,11 @@
1
+ module Duel
2
+ # What the runner and a worker say to each other, marshaled.
3
+ # @api private
4
+ module Message
5
+ # Sends +message+ over +io+.
6
+ def self.write: (IO io, untyped message) -> Integer
7
+
8
+ # The next message over +io+, or nil once the other end has closed it.
9
+ def self.read: (IO io) -> untyped
10
+ end
11
+ end
data/sig/duel/native.rbs CHANGED
@@ -15,6 +15,9 @@ module Duel
15
15
 
16
16
  # A C function of the process's own libraries, by name, taking and
17
17
  # returning Fiddle types (:INT, :LONG, :VOIDP...).
18
+ # Loads Fiddle, as the first C function is made.
19
+ def self.require_fiddle: () -> bool
20
+
18
21
  def self.function: (String name, Array[Symbol] args, Symbol returns) -> _Function
19
22
 
20
23
  # Instructions retired in user space, from the CPU's counters.
@@ -89,12 +92,17 @@ module Duel
89
92
 
90
93
  # This process's resident memory in bytes, or nil where it can't tell.
91
94
  module Memory
92
- STATM: String
93
- MACOS: MacOS?
95
+ STATUS: String
96
+ self.@macos: MacOS?
97
+
98
+ # macOS's counter, opened as resident memory is first read, or nil.
99
+ def self.macos_counter: () -> MacOS?
94
100
 
95
- def self.resident: (?statm: String, ?macos: MacOS?) -> Integer?
101
+ def self.resident: (?status: String, ?macos: MacOS?) -> Integer?
96
102
 
97
103
  def self.used: () -> Integer?
104
+
105
+ def self.collect: () -> void
98
106
  end
99
107
  end
100
108
  end
@@ -4,6 +4,9 @@ module Duel
4
4
  # Duel::RakeTask.new(:perf)
5
5
  # Duel::RakeTask.new(:perf_since_release, "--base", "last-release")
6
6
  class RakeTask
7
+ # Loads Duel's command, as the task runs.
8
+ def self.load_duel: () -> bool
9
+
7
10
  def initialize: (?Symbol | String name, *String args) -> void
8
11
  end
9
12
  end
data/sig/duel/runner.rbs CHANGED
@@ -2,7 +2,7 @@ module Duel
2
2
  # Where reports go ($stdout, or a StringIO in its place).
3
3
  # @api private
4
4
  interface _Output
5
- def puts: (*_ToS objects) -> void
5
+ def puts: (*_ToS objects) -> nil
6
6
 
7
7
  def print: (*_ToS objects) -> void
8
8
  end
@@ -108,6 +108,7 @@ module Duel
108
108
  @runs: ProcessSample?
109
109
  @description: String
110
110
  @extensions: Hash[String, String]
111
+ @twice: Hash[String, Array[String]]
111
112
  @gem_dirs: Array[String]
112
113
  @pid: Integer
113
114
  @socket: UNIXSocket
@@ -135,6 +136,9 @@ module Duel
135
136
  # The native extensions it loaded, by feature.
136
137
  attr_reader extensions: Hash[String, String]
137
138
 
139
+ # Those it loaded from more than one file, by feature.
140
+ attr_reader twice: Hash[String, Array[String]]
141
+
138
142
  # The directories gems are installed in, as real paths ending in "/".
139
143
  attr_reader gem_dirs: Array[String]
140
144