1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
# MemoryProf
MemoryProf tracks memory usage during your test suite run, and can help to detect test examples and groups that cause memory spikes. Memory profiling supports the following metrics: RSS, allocations and GC time.
Example output:
```sh
[TEST PROF INFO] MemoryProf results
Final RSS: 673KB
Top 5 groups (by RSS):
AnswersController (./spec/controllers/answers_controller_spec.rb:3) – +80KB (13.50%)
QuestionsController (./spec/controllers/questions_controller_spec.rb:3) – +32KB (9.08%)
CommentsController (./spec/controllers/comments_controller_spec.rb:3) – +16KB (3.27%)
Top 5 examples (by RSS):
destroys question (./spec/controllers/questions_controller_spec.rb:38) – +144KB (24.38%)
change comments count (./spec/controllers/comments_controller_spec.rb:7) – +120KB (20.00%)
change Votes count (./spec/shared_examples/controllers/voted_examples.rb:23) – +90KB (16.36%)
change Votes count (./spec/shared_examples/controllers/voted_examples.rb:23) – +64KB (12.86%)
fails (./spec/shared_examples/controllers/invalid_examples.rb:3) – +32KB (5.00%)
```
The output shows the amount of memory used by each example and top-level example group (typically, a test file).
## Instructions
To activate MemoryProf with:
### RSpec
Use `TEST_MEM_PROF` environment variable to set which metric to use:
```sh
TEST_MEM_PROF='rss' rspec ...
TEST_MEM_PROF='alloc' rake rspec ...
TEST_MEM_PROF='gc' rake rspec ...
```
### Minitest
In Minitest 6+, you must first activate TestProf plugin by adding `Minitest.load :test_prof` in your test helper.
Use `TEST_MEM_PROF` environment variable to set which metric to use:
```sh
TEST_MEM_PROF='rss' rake test
TEST_MEM_PROF='alloc' rake test
TEST_MEM_PROF='gc' rake test
```
or use CLI options as well:
```sh
# Run a specific file using CLI option
ruby test/my_super_test.rb --mem-prof=rss
# Show the list of possible options:
ruby test/my_super_test.rb --help
```
## Configuration
By default, MemoryProf tracks the top 5 examples and groups that use the largest amount of memory.
You can set how many examples/groups to display with the option:
```sh
TEST_MEM_PROF='rss' TEST_MEM_PROF_COUNT=10 rspec ...
```
or with CLI options for Minitest:
```sh
# Run a specific file using CLI option
ruby test/my_super_test.rb --mem-prof=rs --mem-prof-top-count=10
```
## Supported Ruby Engines & OS
Currently the allocation mode is not supported for JRuby.
Since RSS depends on the OS, MemoryProf uses different tools to retrieve it:
* Linux – `/proc/$pid/statm` file,
* macOS, Solaris, BSD – `ps`,
* Windows – `Get-Process`, requires PowerShell to be installed.
|