require-profiler 0.2.1 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e11b353825774069b1dea2916f61688970a61ea00e59c54ccd68d39e311f452
4
- data.tar.gz: 1580e8719935ec0d70d98f48fdad60207e777a475a86640e9d1cdac390993329
3
+ metadata.gz: d9031019da202ea4bf4ba530de358c6759bb8417de3f96047a138a9d262c0ff0
4
+ data.tar.gz: e51c7eb92cc7419bcdd396103a0565db26e9cf2fd97013577ffcc8b5b127a337
5
5
  SHA512:
6
- metadata.gz: ce85f247ab89347bbd51a609e10254bd1aaf485ae892476a3e67fb722f5037d68b76a153560e8172ea2c9b605aafc005f7accb35a11e77913eba9b7d80616974
7
- data.tar.gz: c0897c79cb3cf3bca1c612d9794ebd00f8ff3d295d98b2faf664190f4bc6d0ba235e01b11a06e3edb7f79197db848d2923b15ae34850cdb9b2febf0349219877
6
+ metadata.gz: cc3d71a84a0faac6f801a9836dc0e0b3c3c6c3d694275f709f8a399229c8a1ee5cb8fe3b9ea4f1c311e04f93e0ffb3c28f3462ce6c582888e155da0a64b469d6
7
+ data.tar.gz: d16347ab1249f15e34f325f11f0505f17985c877bf2808b3e1727bff31c4369d95791032546f5f987034f1061ec95a774f1788646afc5f8c8b010374b26da1cc
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.2.2 (2026-06-05)
6
+
7
+ - Fixed YAML tracking when Pathname is used. ([@palkan][])
8
+
5
9
  ## 0.2.1 (2026-05-26)
6
10
 
7
11
  - Add YAML tracking. ([@palkan][])
@@ -0,0 +1,191 @@
1
+ ---
2
+ name: rails-boot-profiling
3
+ description: Profile Rails application boot time to find slow requires. Use when the user asks why the app boots slowly, wants to profile boot time, or needs to optimize require/load performance.
4
+ gem: require-profiler
5
+ versions: ">= 0.2"
6
+ ---
7
+
8
+ # Rails Boot Time Profiling
9
+
10
+ Profile Rails application boot time using the require-profiler gem. This skill helps identify which `require`, `load`, YAML, and HTTP calls dominate startup time, and provides tools to drill deeper into slow files.
11
+
12
+ ## Prerequisites
13
+
14
+ Before profiling, ensure these conditions are met:
15
+
16
+ 1. The `require-profiler` gem is in the Gemfile (at minimum in the development group).
17
+
18
+ 2. Eager loading must be enabled for the environment you are profiling. Check the environment config:
19
+
20
+ ```ruby
21
+ # config/environments/development.rb (or the target environment)
22
+ config.eager_load = true
23
+ ```
24
+
25
+ If `eager_load` is `false`, the profile will miss most application code — only files loaded during boot are captured, and lazy-loaded files will not appear.
26
+
27
+ 3. Add `-W0` to the Ruby command to suppress warnings and keep output clean.
28
+
29
+ ## Step 1: Run a Full Boot Profile
30
+
31
+ Run the base profiling command:
32
+
33
+ ```sh
34
+ bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
35
+ ```
36
+
37
+ This loads `config/boot.rb` first (to set up Bundler and Bootsnap) and then profiles everything loaded by `config/environment.rb`.
38
+
39
+ The output is an indented tree showing each required file with its load time (self + children) in milliseconds:
40
+
41
+ ```
42
+ config/environment.rb — 4312.071ms
43
+ config/application.rb — 3672.445ms
44
+ railties (>= 0) — 1023.112ms
45
+ actionpack (>= 0) — 412.331ms
46
+ actionview (>= 0) — 198.442ms
47
+ app/models/user.rb — 87.203ms
48
+ app/models/order.rb — 142.891ms
49
+ app/models/concerns/auditable.rb — 12.004ms
50
+ config/initializers/stripe.rb — 523.117ms
51
+ stripe (>= 0) — 498.201ms
52
+ ```
53
+
54
+ To get a quick count of how many files were loaded:
55
+
56
+ ```sh
57
+ bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb | wc -l
58
+ ```
59
+
60
+ ## Step 2: Narrow the Scope
61
+
62
+ ### Filter by Threshold
63
+
64
+ Exclude files that loaded faster than a given number of milliseconds (supports floats):
65
+
66
+ ```sh
67
+ REQUIRE_PROFILE_THRESHOLD=100 bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
68
+ ```
69
+
70
+ This shows only files that took 100ms or more to load — useful for quickly spotting the biggest offenders.
71
+
72
+ ### Filter by Focus Pattern
73
+
74
+ Show only files matching a pattern (uses `Regexp.new(...)` under the hood):
75
+
76
+ ```sh
77
+ REQUIRE_PROFILE_FOCUS="stripe" bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
78
+ ```
79
+
80
+ The focus filter also keeps ancestor nodes in the tree, so you can see the full require chain leading to the matched files.
81
+
82
+ Combine both for a precise view:
83
+
84
+ ```sh
85
+ REQUIRE_PROFILE_THRESHOLD=50 REQUIRE_PROFILE_FOCUS="initializers" bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
86
+ ```
87
+
88
+ ## Step 3: Check YAML and HTTP Activity During Boot
89
+
90
+ By default, require-profiler tracks YAML file loads (`YAML.load_file`, etc.) and adds them to the profile tree. This helps find initializers or gems that parse large YAML configs at boot time.
91
+
92
+ HTTP request tracking is also available but requires the [sniffer](https://github.com/aderyabin/sniffer) gem to be in the Gemfile. This surfaces any HTTP calls made during boot (e.g., config fetches from remote services, gem activation pings).
93
+
94
+ To disable either:
95
+
96
+ ```sh
97
+ # Disable YAML tracking
98
+ REQUIRE_PROFILER_YAML=false bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
99
+
100
+ # Disable HTTP tracking
101
+ REQUIRE_PROFILER_HTTP=false bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
102
+
103
+ # Disable all plugins (YAML + HTTP)
104
+ REQUIRE_PROFILER_PLUGINS=false bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
105
+ ```
106
+
107
+ ## Step 4: Deep-Dive with Stackprof
108
+
109
+ When you identify a file that is unexpectedly slow to load, use Stackprof to profile what happens inside that file during `require`:
110
+
111
+ ```sh
112
+ REQUIRE_PROFILE_STACKPROF=config/initializers/stripe.rb bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
113
+ ```
114
+
115
+ The `stackprof` gem must be in the Gemfile. This generates two files:
116
+
117
+ - `config-initializers-stripe-stackprof.json` — JSON format, viewable in [Speedscope](https://www.speedscope.app/)
118
+ - `config-initializers-stripe-stackprof.dump` — raw Stackprof data, analyzable with the `stackprof` CLI
119
+
120
+ To analyze with the stackprof CLI:
121
+
122
+ ```sh
123
+ bundle exec stackprof config-initializers-stripe-stackprof.dump
124
+ bundle exec stackprof config-initializers-stripe-stackprof.dump --method 'ClassName#method_name'
125
+ ```
126
+
127
+ To view in Speedscope, open https://www.speedscope.app/ and drag the `.json` file onto the page (nothing is uploaded — parsing is local).
128
+
129
+ ## Step 5: Export as JSON for Speedscope
130
+
131
+ Generate a Speedscope-compatible JSON profile of the entire boot:
132
+
133
+ ```sh
134
+ REQUIRE_PROFILE_PATH=tmp/require-profile.json bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
135
+ ```
136
+
137
+ This writes a JSON file conforming to the [Speedscope file format schema](https://www.speedscope.app/file-format-schema.json). Open it in Speedscope and use the **Left Heavy** view to find the most expensive require chains, and the **Sandwich** view to find files that appear repeatedly across different chains.
138
+
139
+ If the Speedscope CLI is installed (`npm install -g speedscope`), open it directly:
140
+
141
+ ```sh
142
+ npx speedscope tmp/require-profile.json
143
+ ```
144
+
145
+ You can also use the `REQUIRE_PROFILE_FORMAT` env var to select the output format explicitly (`text`, `json`, or `call_stack`). When the output path ends in `.json`, the JSON format is selected automatically.
146
+
147
+ ## Collapsed Call Stack Format
148
+
149
+ For flame graph generation with external tools (e.g., `flamegraph.pl`, `inferno`), use the collapsed call stack format:
150
+
151
+ ```sh
152
+ REQUIRE_PROFILE_FORMAT=call_stack REQUIRE_PROFILE_PATH=tmp/require-profile.txt bundle exec ruby -W0 -r./config/boot -require-prof config/environment.rb
153
+ ```
154
+
155
+ This emits one line per stack in Brendan Gregg's collapsed format with per-frame self time in milliseconds.
156
+
157
+ ## Environment Variable Reference
158
+
159
+ | Variable | Purpose | Example |
160
+ |---|---|---|
161
+ | `REQUIRE_PROFILE_THRESHOLD` | Minimum load time in ms to include (float) | `100`, `50.5` |
162
+ | `REQUIRE_PROFILE_FOCUS` | Regexp pattern to filter files | `"stripe"`, `"initializers"` |
163
+ | `REQUIRE_PROFILE_PATH` | Output file path (enables file output) | `tmp/require-profile.json` |
164
+ | `REQUIRE_PROFILE_FORMAT` | Output format: `text`, `json`, `call_stack` | `json` |
165
+ | `REQUIRE_PROFILE_STACKPROF` | File path to deep-profile with Stackprof | `config/initializers/stripe.rb` |
166
+ | `REQUIRE_PROFILER_YAML` | Disable YAML tracking when set to `false` | `false` |
167
+ | `REQUIRE_PROFILER_HTTP` | Disable HTTP tracking when set to `false` | `false` |
168
+ | `REQUIRE_PROFILER_PLUGINS` | Disable all plugins when set to `false` | `false` |
169
+
170
+ ## Recommended Agent Workflow
171
+
172
+ Follow this sequence when a user asks about slow boot time:
173
+
174
+ 1. **Get a baseline.** Run the full profile command and note the total boot time (the top-level entry's duration) and total file count (`| wc -l`).
175
+
176
+ 2. **Find the top offenders.** Re-run with `REQUIRE_PROFILE_THRESHOLD=100` (or adjust based on total time) to surface only slow files. Report the top 5-10 slowest entries to the user.
177
+
178
+ 3. **Investigate specific areas.** If the user suspects a particular gem or area, use `REQUIRE_PROFILE_FOCUS` to zoom in. Otherwise, focus on the slowest entries from step 2.
179
+
180
+ 4. **Check for boot-time side effects.** Look at YAML and HTTP entries in the profile. HTTP calls during boot are almost always worth investigating — they add latency and can fail. YAML loads of large files can also be significant.
181
+
182
+ 5. **Deep-dive when needed.** For files that are unexpectedly slow (the load time seems too high for what the file does), use `REQUIRE_PROFILE_STACKPROF` to generate a Stackprof profile and identify what's happening inside that file.
183
+
184
+ 6. **Generate a JSON profile for handoff.** If the user wants to explore the data themselves, or if the profile is too large to analyze in text, export to JSON with `REQUIRE_PROFILE_PATH=tmp/require-profile.json` and point them to Speedscope.
185
+
186
+ 7. **Suggest actionable improvements** based on findings:
187
+ - Move heavy gem requires behind lazy loading or autoload
188
+ - Defer initializer work to `after_initialize` or `to_prepare` callbacks
189
+ - Replace boot-time HTTP calls with cached configs or async fetches
190
+ - Split large YAML files or cache parsed results
191
+ - Consider using `bootsnap` if not already present
@@ -6,15 +6,15 @@ module RequireProfiler
6
6
  class YAMLPlugin < Base
7
7
  module Patch
8
8
  def load_file(path, ...)
9
- YAMLPlugin.track(path) { super }
9
+ YAMLPlugin.track(path.to_s) { super }
10
10
  end
11
11
 
12
12
  def unsafe_load_file(path, ...)
13
- YAMLPlugin.track(path) { super }
13
+ YAMLPlugin.track(path.to_s) { super }
14
14
  end
15
15
 
16
16
  def safe_load_file(path, ...)
17
- YAMLPlugin.track(path) { super }
17
+ YAMLPlugin.track(path.to_s) { super }
18
18
  end
19
19
  end
20
20
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequireProfiler # :nodoc:
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
@@ -134,6 +134,7 @@ files:
134
134
  - lib/equire-prof.rb
135
135
  - lib/require-profiler.rb
136
136
  - lib/require_profiler.rb
137
+ - lib/require_profiler/hyperdrive/skills/rails-boot-profiling/SKILL.md
137
138
  - lib/require_profiler/plugins.rb
138
139
  - lib/require_profiler/plugins/http_plugin.rb
139
140
  - lib/require_profiler/plugins/yaml_plugin.rb
@@ -153,6 +154,8 @@ metadata:
153
154
  documentation_uri: https://github.com/palkan/require-profiler
154
155
  homepage_uri: https://github.com/palkan/require-profiler
155
156
  source_code_uri: https://github.com/palkan/require-profiler
157
+ hyperdrive_targets: railties
158
+ hyperdrive_artifacts: skill
156
159
  rdoc_options: []
157
160
  require_paths:
158
161
  - lib