leaks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mario Visic
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # Leaks
2
+
3
+ Leaks loops your rpsec tests and monitors process RAM usage to detect memory leaks. Processes with continiously growing usage could protentially have a leak in them.
4
+ I was inspired to create leaks after reading [a blog post by Timothy Elliott](http://holymonkey.com/how-to-find-and-fix-a-memory-leak-in-a-ruby-c-extension.html) about a technique for hunting down memory leaks in rails apps.
5
+
6
+ ## Compatability
7
+
8
+ - Works with RSpec (patches for minitest and test unit are welcome)
9
+ - Works on OSX and Linux (Patches for Windows welcome)
10
+ - Tested on Ruby MRI 1.9.3 (patches for other ruby interpreters welcome)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'leaks'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ ### Rails
25
+
26
+ After adding leaks to your gemfile, fire up the rails console in a test environment:
27
+
28
+ ```
29
+ rails c test
30
+ ```
31
+
32
+ Now simply point leaks to a given spec directory (leave blank to use your entire test suite):
33
+
34
+ ```
35
+ Leaks::Runner.run
36
+ # or
37
+ Leaks::Runner.run('/path/to/spec')
38
+ ```
39
+
40
+ You can pass a single folder, or even an array of folders, for example:
41
+
42
+ ```
43
+ Leaks::Runner.run(['spec/models', 'spec/controllers'])
44
+ ```
45
+
46
+ You can run leaks in verbose mode to see the output of your tests (careful it can get noisy):
47
+
48
+ ```
49
+ Leaks::Runner.run('spec', true)
50
+ ```
51
+
52
+ Now watch the output, the RAM usage will most likely grow for a while. If after a few minutes it's still growing it could be an indication of a problem. Please take note of your test suite size, if your test suite takes 10 minutes to run then be sure to let it run through once or twice before verifying the output.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'leaks/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "leaks"
8
+ gem.version = Leaks::VERSION
9
+ gem.authors = ["Mario Visic"]
10
+ gem.email = ["mario@mariovisic.com"]
11
+ gem.description = %q{Leaks loops your rpsec tests and monitors process RAM usage to detect any memory leaks. Processes with continiously growing usage could protentially have a leak in them.}
12
+ gem.summary = %q{Find memory leaks using your rspec tests}
13
+ gem.homepage = "https://github.com/mariovisic/leaks"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ gem.add_runtime_dependency 'rspec-core', '~> 2.0'
22
+ end
@@ -0,0 +1,6 @@
1
+ require 'leaks/printer'
2
+ require 'leaks/ram_usage'
3
+ require 'leaks/rspec_runner'
4
+ require 'leaks/runner'
5
+ require 'leaks/spinner'
6
+ require 'leaks/version'
@@ -0,0 +1,19 @@
1
+ module Leaks
2
+ module Printer
3
+ def self.run
4
+ loop { print_status; sleep 0.1 }
5
+ end
6
+
7
+ def self.print_status
8
+ print "#{13.chr}#{Spinner} "
9
+
10
+ status = "Runs: #{Runner.runs} #{Runner.ram_usage} Stable: #{Runner.ram_usage.stable_for} sec "
11
+
12
+ if Runner.ram_usage.increased?
13
+ print "\033[31m#{status}\033[0m"
14
+ else
15
+ print "\033[32m#{status}\033[0m"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ module Leaks
2
+ class RAMUsage
3
+ def initialize
4
+ @previous = kilobytes_used
5
+ @current = @previous
6
+ @stable = Time.now
7
+ end
8
+
9
+ def increased?
10
+ if current > @previous
11
+ @stable = Time.now
12
+ true
13
+ else
14
+ false
15
+ end
16
+ end
17
+
18
+ def current
19
+ @previous = @current
20
+ @current = kilobytes_used
21
+ end
22
+
23
+ def to_s
24
+ "Usage: #{current}KB"
25
+ end
26
+
27
+ def stable_for
28
+ (Time.now - @stable).to_i
29
+ end
30
+
31
+ private
32
+
33
+ def kilobytes_used
34
+ `ps -o vsz= -p #{$$}`.to_i
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ require 'rspec'
2
+
3
+ module Leaks
4
+ class RSpecRunner < RSpec::Core::Runner
5
+ def self.run(args, err, out)
6
+ trap_interrupt
7
+ options = RSpec::Core::ConfigurationOptions.new(args)
8
+ options.parse_options
9
+
10
+ unless RSpec::Core::CommandLine.new(options).run(err, out) == 0
11
+ raise RSpecError.new("A test has failed, quitting now. Run again in verbose mode to diagnose the problem")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module Leaks
2
+ class RSpecError < StandardError; end
3
+
4
+ module Runner
5
+ class << self
6
+ attr_reader :ram_usage
7
+ attr_reader :runs
8
+ end
9
+
10
+ def self.run(path = 'spec', verbose=false)
11
+ @ram_usage = RAMUsage.new
12
+ @paths = Array(path)
13
+ @out = verbose ? $stdout : nil
14
+ @err = $stderr
15
+ @runs = 0
16
+ @printer = Thread.new { Printer.run }
17
+
18
+ if defined?(ActiveRecord::Base)
19
+ ActiveRecord::Base.logger = nil
20
+ end
21
+
22
+ RSpecRunner.run(@paths, @err, @out)
23
+
24
+ loop do
25
+ RSpecRunner.run([], @err, @out)
26
+ @runs +=1
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ module Leaks
2
+ module Spinner
3
+ @chars = [ '|', '/', '-', '\\' ]
4
+
5
+ def self.to_s
6
+ @chars.push @chars.shift
7
+ @chars[0]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Leaks
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leaks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mario Visic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ description: Leaks loops your rpsec tests and monitors process RAM usage to detect
31
+ any memory leaks. Processes with continiously growing usage could protentially have
32
+ a leak in them.
33
+ email:
34
+ - mario@mariovisic.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - leaks.gemspec
44
+ - lib/leaks.rb
45
+ - lib/leaks/printer.rb
46
+ - lib/leaks/ram_usage.rb
47
+ - lib/leaks/rspec_runner.rb
48
+ - lib/leaks/runner.rb
49
+ - lib/leaks/spinner.rb
50
+ - lib/leaks/version.rb
51
+ homepage: https://github.com/mariovisic/leaks
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Find memory leaks using your rspec tests
75
+ test_files: []