memory_profiler 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87e903eb12fc8a885066fe621d8254a4297555fa
4
- data.tar.gz: 179a6ccb31c2b71ae9d1ef620be16ce1fa9b4d70
3
+ metadata.gz: d7bad0d25de1214584652c298193da15c2306470
4
+ data.tar.gz: 58b4c27c8e49262703918d27f08e9a2d25b7ba90
5
5
  SHA512:
6
- metadata.gz: ac3255e9b9623c580c3c7cdba776a530907936f8d4ff1c78b46ce42b3d7f97b446082d190c74d7a7f050ec7036d2c662f6736ce1db104ccd0cf17cc466f0ad67
7
- data.tar.gz: c490d5aa5e024119839aaf8948c27d992c6b40325f8ded2447c228d726145c5668d9ee29d3ac13f8f7d0a8d9c0bc064130fcf67209103e493ad8e4ad5acb73ad
6
+ metadata.gz: 724ecac4311bd0080037b9694d0fcf01d5ccce32886257d5fcdaf7a18fd5a1391d2296878b791b2ab30e7fc6086afc8381878ffa097e041b1eab1f983ce8164a
7
+ data.tar.gz: 9ddb1527672a92743baa9c29f0cb3ec2db1fda0336b3502197a614decd8d547a3bf2a8c1946b7809b666a98f27da7f344e2b6634d13e9fe08b396d0a8eec329a
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.9
4
+ - Add options for pretty printer to customize report
5
+
3
6
  ## 0.9.8
4
7
  - Add optional start/stop sematics to memory profiler api @nicklamuro @dgynn
5
8
 
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
+ [![Build Status](https://travis-ci.org/SamSaffron/memory_profiler.svg?branch=master)](https://travis-ci.org/SamSaffron/memory_profiler)
2
+ [![Gem Version](https://badge.fury.io/rb/memory_profiler.svg)](https://rubygems.org/gems/memory_profiler)
3
+
1
4
  # MemoryProfiler
2
5
 
3
6
  A memory profiler for Ruby
4
7
 
5
- [![Build Status](https://travis-ci.org/SamSaffron/memory_profiler.png?branch=master)](https://travis-ci.org/SamSaffron/memory_profiler)
6
-
7
8
  ## Requirements
8
9
 
9
10
  Ruby(MRI) Version 2.1.0 and above.
@@ -5,6 +5,7 @@ module MemoryProfiler
5
5
  @gem_guess_cache = Hash.new
6
6
  @location_cache = Hash.new { |h,k| h[k] = Hash.new.compare_by_identity }
7
7
  @class_name_cache = Hash.new.compare_by_identity
8
+ @string_cache = Hash.new
8
9
  end
9
10
 
10
11
  def guess_gem(path)
@@ -28,5 +29,8 @@ module MemoryProfiler
28
29
  @class_name_cache[klass] ||= ((klass.is_a?(Class) && klass.name) || '<<Unknown>>').to_s
29
30
  end
30
31
 
32
+ def lookup_string(obj)
33
+ @string_cache[obj] ||= String.new << obj
34
+ end
31
35
  end
32
36
  end
@@ -64,8 +64,15 @@ module MemoryProfiler
64
64
  # Collects object allocation and memory of ruby code inside of passed block.
65
65
  def run(&block)
66
66
  start
67
- block.call
68
- stop
67
+ begin
68
+ block.call
69
+ rescue Exception
70
+ ObjectSpace.trace_object_allocations_stop
71
+ GC.enable
72
+ raise
73
+ else
74
+ stop
75
+ end
69
76
  end
70
77
 
71
78
  private
@@ -100,7 +107,7 @@ module MemoryProfiler
100
107
  class_name = helper.lookup_class_name(klass)
101
108
  gem = helper.guess_gem(file)
102
109
 
103
- string = '' << obj if klass == String
110
+ string = klass == String ? helper.lookup_string(obj) : nil
104
111
 
105
112
  memsize = ObjectSpace.memsize_of(obj) + rvalue_size_adjustment
106
113
  # compensate for API bug
@@ -63,6 +63,9 @@ module MemoryProfiler
63
63
  # @param [Hash] options the options for output
64
64
  # @option opts [String] :to_file a path to your log file
65
65
  # @option opts [Boolean] :color_output a flag for whether to colorize output
66
+ # @option opts [Integer] :retained_strings how many retained strings to print
67
+ # @option opts [Integer] :allocated_strings how many allocated strings to print
68
+ # @option opts [Boolean] :detailed_report should report include detailed information
66
69
  def pretty_print(io = $stdout, **options)
67
70
  # Handle the special case that Ruby PrettyPrint expects `pretty_print`
68
71
  # to be a customized pretty printing function for a class
@@ -76,26 +79,34 @@ module MemoryProfiler
76
79
  io.puts "Total allocated: #{total_allocated_memsize} bytes (#{total_allocated} objects)"
77
80
  io.puts "Total retained: #{total_retained_memsize} bytes (#{total_retained} objects)"
78
81
 
79
- io.puts
80
- ["allocated", "retained"]
81
- .product(["memory", "objects"])
82
- .product(["gem", "file", "location", "class"])
83
- .each do |(type, metric), name|
84
- dump "#{type} #{metric} by #{name}", self.send("#{type}_#{metric}_by_#{name}"), io
85
- end
82
+ if options[:detailed_report] != false
83
+ io.puts
84
+ ["allocated", "retained"]
85
+ .product(["memory", "objects"])
86
+ .product(["gem", "file", "location", "class"])
87
+ .each do |(type, metric), name|
88
+ dump "#{type} #{metric} by #{name}", self.send("#{type}_#{metric}_by_#{name}"), io
89
+ end
90
+ end
86
91
 
87
92
  io.puts
88
- dump_strings(io, "Allocated", strings_allocated)
93
+ dump_strings(io, "Allocated", strings_allocated, limit: options[:allocated_strings])
89
94
  io.puts
90
- dump_strings(io, "Retained", strings_retained)
95
+ dump_strings(io, "Retained", strings_retained, limit: options[:retained_strings])
91
96
 
92
97
  io.close if io.is_a? File
93
98
  end
94
99
 
95
100
  private
96
101
 
97
- def dump_strings(io, title, strings)
102
+ def dump_strings(io, title, strings, limit: nil)
98
103
  return unless strings
104
+
105
+ if limit
106
+ return if limit == 0
107
+ strings = strings[0...limit]
108
+ end
109
+
99
110
  io.puts "#{title} String Report"
100
111
  io.puts @colorize.line("-----------------------------------")
101
112
  strings.each do |string, stats|
@@ -1,3 +1,3 @@
1
1
  module MemoryProfiler
2
- VERSION = "0.9.8"
2
+ VERSION = "0.9.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memory_profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-27 00:00:00.000000000 Z
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Memory profiling routines for Ruby 2.1+
14
14
  email:
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubyforge_project:
53
- rubygems_version: 2.5.2
53
+ rubygems_version: 2.6.13
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Memory profiling routines for Ruby 2.1+