From 417e01a9cedd35db738a7a8bb3e78744ffb11cb5 Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <jen-shin@gitlab.com>
Date: Thu, 14 Dec 2023 22:40:42 +0800
Subject: [PATCH] Log RSS when we're printing RSpec elapsed time

---
 Gemfile                        |  1 +
 Gemfile.lock                   |  1 +
 spec/support/rspec_run_time.rb | 11 ++++++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/Gemfile b/Gemfile
index 9333b7a8519f1..60e6f73dc7411 100644
--- a/Gemfile
+++ b/Gemfile
@@ -492,6 +492,7 @@ group :test do
   gem 'rspec_profiling', '~> 0.0.6', feature_category: :tooling
   gem 'rspec-benchmark', '~> 0.6.0', feature_category: :tooling
   gem 'rspec-parameterized', '~> 1.0', require: false, feature_category: :tooling
+  gem 'os', '~> 1.1', feature_category: :tooling
 
   gem 'capybara', '~> 3.39', '>= 3.39.2' # rubocop:todo Gemfile/MissingFeatureCategory
   gem 'capybara-screenshot', '~> 1.0.26' # rubocop:todo Gemfile/MissingFeatureCategory
diff --git a/Gemfile.lock b/Gemfile.lock
index 86f524f7b6575..19fb625670412 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1997,6 +1997,7 @@ DEPENDENCIES
   openid_connect (= 1.3.0)
   openssl (~> 3.0)
   org-ruby (~> 0.9.12)
+  os (~> 1.1)
   pact (~> 1.63)
   parallel (~> 1.19)
   parser (~> 3.2, >= 3.2.2.4)
diff --git a/spec/support/rspec_run_time.rb b/spec/support/rspec_run_time.rb
index 976e902258dcc..977d488562437 100644
--- a/spec/support/rspec_run_time.rb
+++ b/spec/support/rspec_run_time.rb
@@ -1,5 +1,6 @@
 # frozen_string_literal: true
 
+require 'os'
 require 'yaml'
 require 'rspec/core/formatters/base_formatter'
 require_relative '../../tooling/lib/tooling/helpers/duration_formatter'
@@ -84,11 +85,19 @@ def output_elapsed_time
 
         # skip the output unless the duration increased by at least 1 second
         unless @last_elapsed_seconds.nil? || elapsed_seconds - @last_elapsed_seconds < 1
-          output.puts "# [RSpecRunTime] RSpec elapsed time: #{readable_duration(elapsed_seconds)}.\n\n"
+          output.puts \
+            "# [RSpecRunTime] RSpec elapsed time: #{readable_duration(elapsed_seconds)}. " \
+            "#{current_rss_in_megabytes}\n\n"
         end
 
         @last_elapsed_seconds = elapsed_seconds
       end
+
+      def current_rss_in_megabytes
+        rss_in_megabytes = OS.rss_bytes / 1024 / 1024
+
+        "Current RSS: ~#{rss_in_megabytes.round}M"
+      end
     end
   end
 end
-- 
GitLab