minitest-hog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d20e61db9373314953d5013c42f0230b11e0ec83
4
+ data.tar.gz: 18c01c3b533c98a3e918700c477916c10770b8df
5
+ SHA512:
6
+ metadata.gz: e21444cdaf7a7d9fee9d0550a30e0448860445aad9a7b51034c9856a723164312157c69fc9746cf61d9b71959ceba8c6e87d80c063bffffc0981773530e57535
7
+ data.tar.gz: 760443b668344d778cf964cf559154f0498e3646b664254b736b46d9df7e3b3590432791421ec4e53df67c239792b169801b4d3ce15f6923e1aeb537dcfe7358
@@ -0,0 +1,47 @@
1
+ Minitest Hog
2
+ ======================
3
+
4
+ Prints a list of tests that take too long.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem install minitest-hog
10
+
11
+ Usage
12
+ -----
13
+
14
+ Minitest Hog can be enabled from the command line using the `max-memory` parameter:
15
+
16
+ ruby test/example_test.rb --max-memory 16
17
+
18
+ This would print out a list of any tests that use more than 16k of memory:
19
+
20
+ # Running:
21
+
22
+ ....
23
+
24
+ Finished in 1.001143s
25
+
26
+ 4 runs, 7 assertions, 0 failures, 0 errors, 0 skips
27
+
28
+ 2 slow tests.
29
+ 0) ExampleTest#test_alpha: 31 kb
30
+ 1) ExampleTest#test_beta: 18 kb
31
+
32
+ Usage with Rake
33
+ ---------------
34
+
35
+ If you run your tests with Rake, set the TESTOPTS environment variable:
36
+
37
+ rake TESTOPTS="--max-memory 16"
38
+
39
+ Warning!
40
+ --------
41
+
42
+ There are many ways to measure memory usage in Ruby. The results from Minitest Hog may not be exact, but should accurately reflect proportional memory usage.
43
+
44
+ Authors
45
+ -------
46
+
47
+ [Adam Sanderson](netghost@gmail.com) (http://monkeyandcrow.com)
@@ -0,0 +1,89 @@
1
+ module Minitest
2
+ module Hog
3
+
4
+ attr_accessor :memory_used
5
+
6
+ def self.sample
7
+ sampler.call
8
+ end
9
+
10
+ def self.sampler= sampler
11
+ @memory_sampler = sampler
12
+ end
13
+
14
+ def self.sampler
15
+ @memory_sampler ||= Minitest::ShellPs.new
16
+ end
17
+
18
+ def before_setup
19
+ super
20
+ GC.start
21
+ GC.disable
22
+
23
+ self.memory_used = Minitest::Hog.sample
24
+ end
25
+
26
+ def after_teardown
27
+ super
28
+ if memory_used
29
+ self.memory_used = Minitest::Hog.sample - memory_used
30
+ else
31
+ self.memory_used
32
+ end
33
+
34
+ GC.enable
35
+ end
36
+
37
+ end
38
+
39
+ class HogReporter < Reporter
40
+ Record = Struct.new :location, :memory_used
41
+ attr_reader :piggy_tests, :max_memory
42
+
43
+ def initialize(io = STDOUT, options = {})
44
+ super
45
+
46
+ @max_memory = options.fetch(:max_memory, 64)
47
+ @piggy_tests = []
48
+ end
49
+
50
+ def record result
51
+ if result.memory_used > max_memory
52
+ piggy_tests << Record.new(result.location, result.memory_used)
53
+ end
54
+ end
55
+
56
+ def report
57
+ return if piggy_tests.empty?
58
+
59
+ piggy_tests.sort_by!{|r| -r.memory_used}
60
+
61
+ io.puts
62
+ io.puts "#{piggy_tests.length} hogs."
63
+ piggy_tests.each_with_index do |record, i|
64
+ io.puts "%3d) %s: %i kb" % [i+1, record.location, record.memory_used]
65
+ end
66
+ end
67
+ end
68
+
69
+ # Return the resident set size (RSS) in kB.
70
+ #
71
+ # This is fairly foolproof, but probably not the most accurate measure possible
72
+ # since shelling out most likely affects memory usage and isn't terribly efficient.
73
+ #
74
+ # Other strategies could be implemented in the future, for instance see NewRelic's samplers:
75
+ # https://github.com/newrelic/rpm/blob/release/lib/new_relic/agent/samplers/memory_sampler.rb
76
+ #
77
+ class ShellPs
78
+ def initialize(pid = Process::pid)
79
+ @pid = pid
80
+ end
81
+
82
+ # Based off of: http://zogovic.com/post/59091704817/memory-usage-monitor-in-ruby
83
+ # Example ps output: " RSS\n223124\n"
84
+ def call
85
+ `ps -o rss -p #{@pid}`.split("\n").last.to_i
86
+ end
87
+ end
88
+
89
+ end
@@ -0,0 +1,18 @@
1
+ module Minitest
2
+ @minitest_hog = false
3
+
4
+ def self.plugin_hog_options(opts, options)
5
+ opts.on "--max-memory MEM", Integer, "Report tests that use more than MEM kb." do |max_memory|
6
+ options[:max_memory] = max_memory.to_i
7
+ end
8
+ end
9
+
10
+ def self.plugin_hog_init(options)
11
+ if options[:max_memory]
12
+ require "minitest/hog"
13
+
14
+ self.reporter << Minitest::HogReporter.new(options[:io], options)
15
+ Minitest::Test.send :include, Minitest::Hog
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-hog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Sanderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.5'
27
+ description: Summarizes minitest tests, printing out tests that take too much memory.
28
+ email:
29
+ - netghost@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.markdown
35
+ - lib/minitest/hog.rb
36
+ - lib/minitest/hog_plugin.rb
37
+ homepage: https://github.com/adamsanderson/minitest-hog
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Identify memory hogs.
61
+ test_files: []