From 249e2bbf1b6777ea46513a26e11bee639cb6d505 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 14 Sep 2026 21:25:25 +1200 Subject: [PATCH 1/4] Fix process metrics memory total --- bake/process/metrics.rb | 12 ++-- test/process/metrics/bake.rb | 105 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 test/process/metrics/bake.rb diff --git a/bake/process/metrics.rb b/bake/process/metrics.rb index ce0d8a9..48c6ab6 100644 --- a/bake/process/metrics.rb +++ b/bake/process/metrics.rb @@ -38,7 +38,7 @@ def metrics(pid: nil, ppid: nil) summary = Process::Metrics::General.capture(pid: pid, ppid: ppid) - shared_memory = 0 + process_memory = 0 private_memory = 0 total_memory = host.total_size @@ -52,7 +52,7 @@ def metrics(pid: nil, ppid: nil) terminal.print_line if memory = general.memory - shared_memory += memory.proportional_size + process_memory += memory.proportional_size private_memory += memory.unique_size terminal.print_line( @@ -65,7 +65,7 @@ def metrics(pid: nil, ppid: nil) format_memory[memory.unique_size, total_memory] ) else - shared_memory += general.resident_size + process_memory += general.resident_size proportional = false terminal.print_line( @@ -80,7 +80,7 @@ def metrics(pid: nil, ppid: nil) if proportional terminal.print_line( :key, "Memory: ".rjust(20), :reset, - format_memory[shared_memory, total_memory] + format_memory[process_memory, total_memory] ) terminal.print_line( @@ -90,13 +90,13 @@ def metrics(pid: nil, ppid: nil) else terminal.print_line( :key, "Memory: ".rjust(20), :reset, - format_memory[shared_memory, total_memory] + format_memory[process_memory, total_memory] ) end terminal.print_line( :key, "Memory (Total): ".rjust(20), :reset, - format_memory[shared_memory + private_memory, total_memory] + format_memory[process_memory, total_memory] ) end diff --git a/test/process/metrics/bake.rb b/test/process/metrics/bake.rb new file mode 100644 index 0000000..7c2fb6d --- /dev/null +++ b/test/process/metrics/bake.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2026, by Samuel Williams. + +require "process/metrics/general" +require "process/metrics/host" + +describe "process:metrics bake task" do + class FakeTerminal + attr_reader :lines + + def initialize + @lines = [] + @current = +"" + end + + def print(*arguments) + @current << arguments.reject{|argument| argument.is_a?(Symbol)}.join + end + + def print_line(*arguments) + print(*arguments) + @lines << @current + @current = +"" + end + + def width + 80 + end + end + + let(:task) do + mod = Module.new + path = File.expand_path("../../../bake/process/metrics.rb", __dir__) + mod.module_eval(File.read(path), path) + + terminal = self.terminal + Class.new do + include mod + + define_method(:terminal) {terminal} + define_method(:format_memory) do |value, total| + super(value, total, terminal) + end + end.new + end + + let(:terminal) {FakeTerminal.new} + + it "does not add private memory to proportional memory in the summary total" do + general_capture = Process::Metrics::General.method(:capture) + host_memory_capture = Process::Metrics::Host::Memory.method(:capture) + + begin + Process::Metrics::General.define_singleton_method(:capture) do |pid: nil, ppid: nil| + memory = Process::Metrics::Memory.new( + 1, + 110 * 1024 * 1024, + 100 * 1024 * 1024, + 20 * 1024 * 1024, + 0, + 10 * 1024 * 1024, + 80 * 1024 * 1024, + 0, + 0, + 0, + 0, + 0, + 0 + ) + + process = Process::Metrics::General.new( + pid, + nil, + nil, + 0.0, + 0, + 110 * 1024 * 1024, + 0.0, + 0.0, + 0.0, + "test process", + memory + ) + + {pid => process} + end + + Process::Metrics::Host::Memory.define_singleton_method(:capture) do + Process::Metrics::Host::Memory.new(1024 * 1024 * 1024, 512 * 1024 * 1024, nil, nil, nil) + end + + task.metrics(pid: 1234) + ensure + Process::Metrics::General.define_singleton_method(:capture, general_capture) + Process::Metrics::Host::Memory.define_singleton_method(:capture, host_memory_capture) + end + + line = terminal.lines.find{|line| line.include?("Memory (Total):")} + + expect(line).to be(:include?, "100.0MiB") + expect(line).not.to be(:include?, "190.0MiB") + end +end From 23341076b0f139fe514e9a5b6bf22ec50321c947 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 14 Sep 2026 22:01:45 +1200 Subject: [PATCH 2/4] Use Sus mocks in bake metrics test --- test/process/metrics/bake.rb | 79 +++++++++++++++--------------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/test/process/metrics/bake.rb b/test/process/metrics/bake.rb index 7c2fb6d..7c4655c 100644 --- a/test/process/metrics/bake.rb +++ b/test/process/metrics/bake.rb @@ -49,53 +49,40 @@ def width let(:terminal) {FakeTerminal.new} it "does not add private memory to proportional memory in the summary total" do - general_capture = Process::Metrics::General.method(:capture) - host_memory_capture = Process::Metrics::Host::Memory.method(:capture) + memory = Process::Metrics::Memory.new( + 1, + 110 * 1024 * 1024, + 100 * 1024 * 1024, + 20 * 1024 * 1024, + 0, + 10 * 1024 * 1024, + 80 * 1024 * 1024, + 0, + 0, + 0, + 0, + 0, + 0 + ) - begin - Process::Metrics::General.define_singleton_method(:capture) do |pid: nil, ppid: nil| - memory = Process::Metrics::Memory.new( - 1, - 110 * 1024 * 1024, - 100 * 1024 * 1024, - 20 * 1024 * 1024, - 0, - 10 * 1024 * 1024, - 80 * 1024 * 1024, - 0, - 0, - 0, - 0, - 0, - 0 - ) - - process = Process::Metrics::General.new( - pid, - nil, - nil, - 0.0, - 0, - 110 * 1024 * 1024, - 0.0, - 0.0, - 0.0, - "test process", - memory - ) - - {pid => process} - end - - Process::Metrics::Host::Memory.define_singleton_method(:capture) do - Process::Metrics::Host::Memory.new(1024 * 1024 * 1024, 512 * 1024 * 1024, nil, nil, nil) - end - - task.metrics(pid: 1234) - ensure - Process::Metrics::General.define_singleton_method(:capture, general_capture) - Process::Metrics::Host::Memory.define_singleton_method(:capture, host_memory_capture) - end + process = Process::Metrics::General.new( + 1234, + nil, + nil, + 0.0, + 0, + 110 * 1024 * 1024, + 0.0, + 0.0, + 0.0, + "test process", + memory + ) + + expect(Process::Metrics::General).to receive(:capture).with_options(be == {pid: 1234, ppid: nil}).and_return(1234 => process) + expect(Process::Metrics::Host::Memory).to receive(:capture).and_return(Process::Metrics::Host::Memory.new(1024 * 1024 * 1024, 512 * 1024 * 1024, nil, nil, nil)) + + task.metrics(pid: 1234) line = terminal.lines.find{|line| line.include?("Memory (Total):")} From bb64208171bdd1c728ae9161351b4dc41eb40d98 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 14 Sep 2026 22:19:28 +1200 Subject: [PATCH 3/4] Cover metrics summary fallback --- test/process/metrics/bake.rb | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/process/metrics/bake.rb b/test/process/metrics/bake.rb index 7c4655c..cb0bd17 100644 --- a/test/process/metrics/bake.rb +++ b/test/process/metrics/bake.rb @@ -48,6 +48,10 @@ def width let(:terminal) {FakeTerminal.new} + def host_memory + Process::Metrics::Host::Memory.new(1024 * 1024 * 1024, 512 * 1024 * 1024, nil, nil, nil) + end + it "does not add private memory to proportional memory in the summary total" do memory = Process::Metrics::Memory.new( 1, @@ -78,10 +82,10 @@ def width "test process", memory ) - + expect(Process::Metrics::General).to receive(:capture).with_options(be == {pid: 1234, ppid: nil}).and_return(1234 => process) - expect(Process::Metrics::Host::Memory).to receive(:capture).and_return(Process::Metrics::Host::Memory.new(1024 * 1024 * 1024, 512 * 1024 * 1024, nil, nil, nil)) - + expect(Process::Metrics::Host::Memory).to receive(:capture).and_return(host_memory) + task.metrics(pid: 1234) line = terminal.lines.find{|line| line.include?("Memory (Total):")} @@ -89,4 +93,29 @@ def width expect(line).to be(:include?, "100.0MiB") expect(line).not.to be(:include?, "190.0MiB") end + + it "uses resident memory in the summary total when detailed memory is unavailable" do + process = Process::Metrics::General.new( + 1234, + nil, + nil, + 0.0, + 0, + 110 * 1024 * 1024, + 0.0, + 0.0, + 0.0, + "test process", + nil + ) + + expect(Process::Metrics::General).to receive(:capture).with_options(be == {pid: 1234, ppid: nil}).and_return(1234 => process) + expect(Process::Metrics::Host::Memory).to receive(:capture).and_return(host_memory) + + task.metrics(pid: 1234) + + line = terminal.lines.find{|line| line.include?("Memory (Total):")} + + expect(line).to be(:include?, "110.0MiB") + end end From 5f6dbd1a89952b66e0137d65ca533f13f57a126e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 14 Sep 2026 22:24:22 +1200 Subject: [PATCH 4/4] Avoid covering evaluated bake task --- test/process/metrics/bake.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/process/metrics/bake.rb b/test/process/metrics/bake.rb index cb0bd17..62b409e 100644 --- a/test/process/metrics/bake.rb +++ b/test/process/metrics/bake.rb @@ -10,9 +10,10 @@ class FakeTerminal attr_reader :lines - def initialize + def initialize(width: 80) @lines = [] @current = +"" + @width = width end def print(*arguments) @@ -26,14 +27,19 @@ def print_line(*arguments) end def width - 80 + @width end end - let(:task) do + let(:metrics_module) do mod = Module.new path = File.expand_path("../../../bake/process/metrics.rb", __dir__) - mod.module_eval(File.read(path), path) + mod.module_eval(File.read(path)) + mod + end + + let(:task) do + mod = metrics_module terminal = self.terminal Class.new do