rails_console_benchmark 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eff8a0cf6f62a6eaf060b7cca6386779d5c2aedc2aa967c1744d554ae124a8a0
4
+ data.tar.gz: 142bbb22aed8be2063ec44aceaf00a6a9c98feb33a08ff4f3210d761525899a6
5
+ SHA512:
6
+ metadata.gz: 6d788649e3172dda31d50ef231bf65fddc081ac5c79e3d4b25dc32ab512d0b0053df189b50bf82f258e10e79ddef3c17dc48b931d2c5f3d37f2b99fe1b6098c1
7
+ data.tar.gz: 0d933f576411d45282a1e4660920915086d2edf6316d956b8e04a507dfd903d67ade2d45009c2b1ee03dcbac17428524996f5e8be061aa2d8a3e61326911676c
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2026-03-26
4
+
5
+ ### Added
6
+
7
+ - `Tracker.measure` for profiling a block of Ruby/Rails code.
8
+ - Wall time measurement via `Process.clock_gettime(Process::CLOCK_MONOTONIC)`, reported in milliseconds (3 decimal places).
9
+ - SQL query count tracking via `ActiveSupport::Notifications` (Rails-optional; skips `SCHEMA` and `CACHE` queries).
10
+ - Memory allocation and retention profiling via `memory_profiler` (first iteration only).
11
+ - Results displayed as a formatted terminal table via `terminal-table`.
12
+ - Multi-iteration support: pass an integer to `Tracker.measure(n)` to run the block N times and display min/max/mean/total statistics.
13
+ - Full RSpec test suite covering `Tracker` and `Formatter`, including Rails-optional and non-Rails code paths.
14
+ - SimpleCov test coverage reporting.
15
+ - RuboCop integration (with `rubocop-rake` and `rubocop-rspec`) for code style and quality enforcement.
16
+ - Full RBS type signatures for `Tracker` and `Formatter` in `sig/rails_console_benchmark.rbs`.
17
+ - `Formatter` displays `N/A` for SQL Queries when `ActiveSupport::Notifications` is not available.
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "rails_console_benchmark" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["matty.jacques@proton.me"](mailto:"matty.jacques@proton.me").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Matthew Jacques
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # RailsConsoleBenchmark
2
+
3
+ A Ruby gem for profiling a block of code from the Rails (or plain Ruby) console. It measures wall time, SQL query count, and memory allocation, then displays the results in a formatted terminal table.
4
+
5
+ ## Features
6
+
7
+ - **Wall time** — measured via `Process.clock_gettime(Process::CLOCK_MONOTONIC)`, reported in milliseconds (3 decimal places).
8
+ - **SQL query count** — tracked via `ActiveSupport::Notifications` when ActiveRecord is present. `SCHEMA` and `CACHE` queries are excluded.
9
+ - **Memory allocation** — total allocated and retained memory reported via `memory_profiler`. Memory profiling runs on the first iteration only.
10
+ - **Multi-iteration support** — run the block N times and get min/max/mean/total statistics.
11
+ - **Rails-optional** — works in plain Ruby projects with no ActiveRecord dependency.
12
+
13
+ ## Installation
14
+
15
+ Add to your application's Gemfile:
16
+
17
+ ```bash
18
+ bundle add rails_console_benchmark
19
+ ```
20
+
21
+ Or install directly:
22
+
23
+ ```bash
24
+ gem install rails_console_benchmark
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Single run
30
+
31
+ ```ruby
32
+ RailsConsoleBenchmark::Tracker.measure do
33
+ MyModel.where(active: true).to_a
34
+ end
35
+ ```
36
+
37
+ Example output:
38
+
39
+ ```
40
+ +------------------+------------+
41
+ | Metric | Value |
42
+ +------------------+------------+
43
+ | Wall Time (ms) | 42.371 |
44
+ | SQL Queries | 1 |
45
+ | Allocated Memory | 512.00 KB |
46
+ | Retained Memory | 4.00 KB |
47
+ +------------------+------------+
48
+ ```
49
+
50
+ ### Multiple iterations
51
+
52
+ ```ruby
53
+ RailsConsoleBenchmark::Tracker.measure(10) do
54
+ MyModel.where(active: true).to_a
55
+ end
56
+ ```
57
+
58
+ Example output:
59
+
60
+ ```
61
+ +------------------+--------+--------+---------+---------+
62
+ | Metric | Min | Max | Mean | Total |
63
+ +------------------+--------+--------+---------+---------+
64
+ | Wall Time (ms) | 38.112 | 56.903 | 44.210 | 442.100 |
65
+ | SQL Queries | 1 | 1 | 1.0 | 10 |
66
+ | Allocated Memory | 512.00 KB | 512.00 KB | 512.00 KB | 512.00 KB |
67
+ | Retained Memory | 4.00 KB | 4.00 KB | 4.00 KB | 4.00 KB |
68
+ +------------------+--------+--------+---------+---------+
69
+ ```
70
+
71
+ > Memory figures are from the first iteration only, so min/max/mean/total all show the same value.
72
+
73
+ ### Without Rails / ActiveRecord
74
+
75
+ When `ActiveSupport::Notifications` is not defined, the SQL Queries row displays `N/A`.
76
+
77
+ ## Requirements
78
+
79
+ - Ruby >= 3.2.0
80
+ - [`memory_profiler`](https://github.com/SamSaffron/memory_profiler) `~> 1.0`
81
+ - [`terminal-table`](https://github.com/tj/terminal-table) `~> 3.0`
82
+ - Rails / ActiveRecord is **optional**
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then run `rake spec` to run the test suite. You can also run `bin/console` for an interactive prompt.
87
+
88
+ To install the gem locally:
89
+
90
+ ```bash
91
+ bundle exec rake install
92
+ ```
93
+
94
+ To release a new version, update `VERSION` in `version.rb` and run:
95
+
96
+ ```bash
97
+ bundle exec rake release
98
+ ```
99
+
100
+ This creates a git tag, pushes commits and the tag, and publishes the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MattyJacques/rails_console_benchmark. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/MattyJacques/rails_console_benchmark/blob/main/CODE_OF_CONDUCT.md).
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the RailsConsoleBenchmark project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/MattyJacques/rails_console_benchmark/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'terminal-table'
4
+
5
+ module RailsConsoleBenchmark
6
+ # Formats and displays benchmark results as a table in the console.
7
+ module Formatter
8
+ class << self
9
+ def display(result)
10
+ wall_times = result[:wall_times_ms]
11
+ headings, rows = wall_times.length == 1 ? single_result(result) : aggregate_result(result)
12
+ puts Terminal::Table.new(headings: headings, rows: rows)
13
+ end
14
+
15
+ private
16
+
17
+ def single_result(result)
18
+ rows = [
19
+ ['Wall Time (ms)', result[:wall_times_ms].first],
20
+ sql_row(result[:sql_queries], single: true),
21
+ ['Allocated Memory', format_bytes(result[:allocated_memory])],
22
+ ['Retained Memory', format_bytes(result[:retained_memory])]
23
+ ]
24
+ [%w[Metric Value], rows]
25
+ end
26
+
27
+ def aggregate_result(result)
28
+ rows = [
29
+ aggregate_row('Wall Time (ms)', result[:wall_times_ms]),
30
+ sql_row(result[:sql_queries]),
31
+ memory_row('Allocated Memory', result[:allocated_memory]),
32
+ memory_row('Retained Memory', result[:retained_memory])
33
+ ]
34
+ [%w[Metric Min Max Mean Total], rows]
35
+ end
36
+
37
+ def sql_row(sql_queries, single: false)
38
+ if sql_queries.nil?
39
+ single ? ['SQL Queries', 'N/A'] : ['SQL Queries', *(['N/A'] * 4)]
40
+ elsif single
41
+ ['SQL Queries', sql_queries.first]
42
+ else
43
+ aggregate_row('SQL Queries', sql_queries)
44
+ end
45
+ end
46
+
47
+ def aggregate_row(label, values)
48
+ total = values.sum
49
+ [
50
+ label,
51
+ values.min,
52
+ values.max,
53
+ (total.to_f / values.length).round(3),
54
+ total.round(3)
55
+ ]
56
+ end
57
+
58
+ def memory_row(label, bytes)
59
+ formatted = format_bytes(bytes)
60
+ [label, *([formatted] * 4)]
61
+ end
62
+
63
+ def format_bytes(bytes)
64
+ return '0 B' if bytes.zero?
65
+
66
+ units = %w[B KB MB GB]
67
+ exp = (Math.log(bytes) / Math.log(1024)).floor
68
+ exp = [exp, units.length - 1].min
69
+ format('%<value>.2f %<unit>s', value: bytes.to_f / (1024**exp), unit: units[exp])
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'memory_profiler'
4
+
5
+ module RailsConsoleBenchmark
6
+ # Measures wall time, SQL query counts, and memory usage for a block of code.
7
+ class Tracker
8
+ def self.measure(iterations = 1, &)
9
+ new(iterations).measure(&)
10
+ end
11
+
12
+ def initialize(iterations = 1)
13
+ raise ArgumentError, 'iterations must be >= 1' if iterations < 1
14
+
15
+ @iterations = iterations
16
+ @track_sql = defined?(ActiveSupport::Notifications)
17
+ end
18
+
19
+ def measure(&)
20
+ wall_times, sql_counts, memory_report = collect_measurements(&)
21
+ RailsConsoleBenchmark::Formatter.display(
22
+ wall_times_ms: wall_times,
23
+ sql_queries: sql_counts,
24
+ allocated_memory: memory_report.total_allocated_memsize,
25
+ retained_memory: memory_report.total_retained_memsize
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ def collect_measurements(&block)
32
+ results = (0...@iterations).map { |i| run_iteration(i, &block) }
33
+ [wall_times(results), sql_counts(results), memory_report(results)]
34
+ end
35
+
36
+ def wall_times(results)
37
+ results.map { |wt, *| (wt * 1000).round(3) }
38
+ end
39
+
40
+ def sql_counts(results)
41
+ @track_sql ? results.map { |_wt, sql, *| sql } : nil
42
+ end
43
+
44
+ def memory_report(results)
45
+ results.first.last
46
+ end
47
+
48
+ def run_iteration(index, &block)
49
+ result, sql_count = with_sql_tracking do
50
+ if index.zero?
51
+ timed_with_memory_profile { start_time_and_call(&block) }
52
+ else
53
+ [start_time_and_call(&block), nil]
54
+ end
55
+ end
56
+ wall_time, report = result
57
+ [wall_time, sql_count, report]
58
+ end
59
+
60
+ def with_sql_tracking(&block)
61
+ return [block.call, nil] unless @track_sql
62
+
63
+ sql_count = 0
64
+ subscriber = lambda { |*, payload|
65
+ next if payload[:name]&.start_with?('SCHEMA', 'CACHE')
66
+
67
+ sql_count += 1
68
+ }
69
+ result = ActiveSupport::Notifications.subscribed(subscriber, 'sql.active_record', &block)
70
+ [result, sql_count]
71
+ end
72
+
73
+ def timed_with_memory_profile(&block)
74
+ wall_time = nil
75
+ report = MemoryProfiler.report { wall_time = block.call }
76
+ [wall_time, report]
77
+ end
78
+
79
+ def start_time_and_call(&block)
80
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
81
+ block.call
82
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsConsoleBenchmark
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rails_console_benchmark/version'
4
+ require_relative 'rails_console_benchmark/formatter'
5
+ require_relative 'rails_console_benchmark/tracker'
6
+
7
+ # Top-level namespace for the RailsConsoleBenchmark gem.
8
+ module RailsConsoleBenchmark
9
+ end
@@ -0,0 +1,33 @@
1
+ module RailsConsoleBenchmark
2
+ VERSION: String
3
+
4
+ class Tracker
5
+ def self.measure: (?::Integer iterations) { () -> untyped } -> void
6
+
7
+ def initialize: (?::Integer iterations) -> void
8
+
9
+ def measure: () { () -> untyped } -> void
10
+
11
+ private
12
+
13
+ def collect_measurements: () { () -> untyped } -> [Array[Float], Array[Integer]?, untyped]
14
+
15
+ def wall_times: (Array[[Float, untyped, untyped]]) -> Array[Float]
16
+
17
+ def sql_counts: (Array[[Float, Integer?, untyped]]) -> Array[Integer]?
18
+
19
+ def memory_report: (Array[[Float, untyped, untyped]]) -> untyped
20
+
21
+ def run_iteration: (::Integer index) { () -> untyped } -> [Float, Integer?, untyped]
22
+
23
+ def with_sql_tracking: () { () -> untyped } -> [untyped, Integer?]
24
+
25
+ def timed_with_memory_profile: () { () -> untyped } -> [Float, untyped]
26
+
27
+ def start_time_and_call: () { () -> untyped } -> Float
28
+ end
29
+
30
+ module Formatter
31
+ def self.display: ({ wall_times_ms: Array[Float], sql_queries: Array[Integer]?, allocated_memory: Integer, retained_memory: Integer }) -> void
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_console_benchmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Jacques
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: memory_profiler
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: terminal-table
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ description: A gem that measures block execution time, database query counts (when
41
+ ActiveRecord is present), and memory allocation, displaying results in a formatted
42
+ terminal table. Works in plain Ruby and Rails projects alike.
43
+ email:
44
+ - matty.jacques@proton.me
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/rails_console_benchmark.rb
55
+ - lib/rails_console_benchmark/formatter.rb
56
+ - lib/rails_console_benchmark/tracker.rb
57
+ - lib/rails_console_benchmark/version.rb
58
+ - sig/rails_console_benchmark.rbs
59
+ homepage: https://github.com/MattyJacques/rails_console_benchmark
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/MattyJacques/rails_console_benchmark
64
+ source_code_uri: https://github.com/MattyJacques/rails_console_benchmark
65
+ changelog_uri: https://github.com/MattyJacques/rails_console_benchmark/blob/main/CHANGELOG.md
66
+ rubygems_mfa_required: 'true'
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.2.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 4.0.8
82
+ specification_version: 4
83
+ summary: 'Profile Ruby/Rails console block execution: wall time, SQL query count,
84
+ and memory usage.'
85
+ test_files: []