profmem 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/lib/profmem.rb +88 -0
- data/lib/profmem/version.rb +3 -0
- data/profmem.gemspec +23 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Phil Hofmann <phil@branch14.org>
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
__
|
2
|
+
_ __ _ __ ___ / _|_ __ ___ ___ _ __ ___
|
3
|
+
| '_ \| '__/ _ \| |_| '_ ` _ \ / _ \ '_ ` _ \
|
4
|
+
| |_) | | | (_) | _| | | | | | __/ | | | | |
|
5
|
+
| .__/|_| \___/|_| |_| |_| |_|\___|_| |_| |_|
|
6
|
+
|_|
|
7
|
+
|
8
|
+
# Welcome to profmem
|
9
|
+
|
10
|
+
A simple tool to profile memory, built to identify memory leaks.
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Let's say you want to profile the memory consumption of your Rspec
|
15
|
+
suite...
|
16
|
+
|
17
|
+
First in your Gemfile's test group add profmem
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
gem 'profmem', require: false
|
21
|
+
...
|
22
|
+
|
23
|
+
Secondly in your `spec_helper` file require profmem and start it right
|
24
|
+
at the top. (It is safe to do this unconditionally as it will only
|
25
|
+
trigger if the env var PROFMEM is set.)
|
26
|
+
|
27
|
+
require 'profmem'
|
28
|
+
Profmem.start
|
29
|
+
|
30
|
+
In an after-suite-callback of Rspec tell memprof to summarize it's
|
31
|
+
findings
|
32
|
+
|
33
|
+
config.after(:suite) do
|
34
|
+
Profmem.summarize
|
35
|
+
end
|
36
|
+
|
37
|
+
Run you Rspec suite with env var PROFMEM set to the destination of the
|
38
|
+
profiling data
|
39
|
+
|
40
|
+
$ PROFMEM=pm.dump rspec
|
41
|
+
|
42
|
+
Finally run profmem to do the number crunching (this might take a
|
43
|
+
while) and study the results
|
44
|
+
|
45
|
+
$ profmem pm.dump pm.txt
|
46
|
+
$ less pm.txt
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/profmem.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Profmem
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def setup
|
10
|
+
if ENV['PROFMEM']
|
11
|
+
require 'objspace'
|
12
|
+
ObjectSpace.trace_object_allocations_start
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def finalize
|
17
|
+
if path = ENV['PROFMEM']
|
18
|
+
io = File.open(path, "w")
|
19
|
+
ObjectSpace.dump_all(output: io)
|
20
|
+
io.close
|
21
|
+
puts "est. total memory consumed: #{ObjectSpace.memsize_of_all}"
|
22
|
+
puts "memory profiling data written to #{path} (#{File.size(path)})"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def run(args)
|
27
|
+
|
28
|
+
puts
|
29
|
+
puts 'Reading input file...'
|
30
|
+
total = `wc -l "#{args[0]}"`.strip.split(' ')[0].to_i
|
31
|
+
puts "Data points:\t#{total}"
|
32
|
+
width = total.to_s.length
|
33
|
+
|
34
|
+
data = []
|
35
|
+
counter = 0
|
36
|
+
File.open(args[0]) do |f|
|
37
|
+
f.each_line do |line|
|
38
|
+
data << (parsed=JSON.parse(line))
|
39
|
+
counter += 1
|
40
|
+
print "Progress:\t% #{width}s\r" % counter
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
puts
|
45
|
+
puts
|
46
|
+
puts 'Grouping by generation...'
|
47
|
+
|
48
|
+
generations = data.group_by { |row| row["generation"] }
|
49
|
+
|
50
|
+
puts 'Ranking...'
|
51
|
+
ranked = {}
|
52
|
+
generations.each do |gen, allocs|
|
53
|
+
gen ||= 1 # sometimes gen is nil
|
54
|
+
ranked[gen] = {
|
55
|
+
rank: gen * allocs.size,
|
56
|
+
allocs: allocs
|
57
|
+
}
|
58
|
+
end
|
59
|
+
generations = nil # free some memory
|
60
|
+
|
61
|
+
puts 'Sorting...'
|
62
|
+
ranked = ranked.sort_by { |k, v| -v[:rank] }
|
63
|
+
|
64
|
+
puts 'Writing output file...'
|
65
|
+
puts "Generations:\t#{ranked.size}"
|
66
|
+
width = ranked.size.to_s.length
|
67
|
+
|
68
|
+
File.open(args[1], 'w') do |f|
|
69
|
+
counter = 0
|
70
|
+
ranked.each do |k,v|
|
71
|
+
counter += 1
|
72
|
+
print "Progress:\t% #{width}s\r" % counter
|
73
|
+
f.puts "generation #{k} objects #{v[:allocs].count} rank #{v[:rank]}"
|
74
|
+
locs = v[:allocs].group_by { |a| [a['file'], a['line']] * ':' }
|
75
|
+
locs = locs.sort_by { |loc, allocs| -allocs.size }
|
76
|
+
locs.each do |loc, allocs|
|
77
|
+
f.puts " #{allocs.size} #{loc}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
puts
|
83
|
+
puts
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Profmem.run(ARGV) if $0 == __FILE__
|
data/profmem.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'profmem/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "profmem"
|
8
|
+
spec.version = Profmem::VERSION
|
9
|
+
spec.authors = ["phil"]
|
10
|
+
spec.email = ["phil@branch14.org"]
|
11
|
+
spec.description = %q{A simple tool to profile memory, built to identify memory leaks.}
|
12
|
+
spec.summary = %q{A simple tool to profile memory, built to identify memory leaks.}
|
13
|
+
spec.homepage = "http://github.com/branch14/profmem"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: profmem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- phil
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A simple tool to profile memory, built to identify memory leaks.
|
47
|
+
email:
|
48
|
+
- phil@branch14.org
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/profmem.rb
|
59
|
+
- lib/profmem/version.rb
|
60
|
+
- profmem.gemspec
|
61
|
+
homepage: http://github.com/branch14/profmem
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: 1021228832940894519
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 1021228832940894519
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: A simple tool to profile memory, built to identify memory leaks.
|
92
|
+
test_files: []
|