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..62b409e --- /dev/null +++ b/test/process/metrics/bake.rb @@ -0,0 +1,127 @@ +# 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(width: 80) + @lines = [] + @current = +"" + @width = width + 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 + @width + end + end + + let(:metrics_module) do + mod = Module.new + path = File.expand_path("../../../bake/process/metrics.rb", __dir__) + mod.module_eval(File.read(path)) + mod + end + + let(:task) do + mod = metrics_module + + 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} + + 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, + 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( + 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(host_memory) + + task.metrics(pid: 1234) + + 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 + + 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