memory_profiler 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4c8c5a12cd11ab3f9c5ab852e6bca0cb442ccc25f54997f49243a19aa6cef9b
4
- data.tar.gz: 5c0ca217ca3cbc0edca725071512688443ac24a5d3e9f28980d93d3dd30a323f
3
+ metadata.gz: d5a66fca1aa2343e5380049e2079a9949bf315ab053db4106ab591ec8f1a3b87
4
+ data.tar.gz: cf5d847031900969429d4aa295afe385375bf18582444dcc559e4171561c23b7
5
5
  SHA512:
6
- metadata.gz: 11d3bf57b920c915a0a98f6cea8798349983b0c64047f481ab1227301ff100a8de16f858ee7d21028d32e3dd04015446f77555bfd050755637714da5d1cc08b3
7
- data.tar.gz: 46d2df335163a6ddc35ea3a0eff97573dc7b8a50d671e9aa8940a5acb300ea342d7eb38e10f03f39f3705c4e9ed318a336c68f72ddf39a5cdf4f6bb81b9cc7e5
6
+ metadata.gz: 27898d1f93136732b68dfc3c624b20865c9a5772f017204afefe23e61295e5d00775c6b3ba73e4747d6cc5df862cc251ba6046313d54e6893187aab075442eb6
7
+ data.tar.gz: 4c2f5c257ed8a9be18b06bbe3931527e1da026ad431e00e05c10e4a3938eaa4329cc65e7fd79c89257d2994dcacb5d9e3c5d363f10993625ff362e778750a82b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0 - 17-09-2024
4
+
5
+ - Remove EOL Rubies: < 3.1 are no longer supported (use an earlier version of the gem if needed)
6
+
7
+ ## 1.0.2 - 17-06-2024
8
+
9
+ - Add ability to profile commands via CLI @fatkodima
10
+
3
11
  ## 1.0.1 - 23-10-2022
4
12
 
5
13
  - Adapts tests to Ruby 3.0 / 3.1
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![CI](https://github.com/SamSaffron/memory_profiler/workflows/CI/badge.svg)](https://github.com/SamSaffron/memory_profiler/actions?query=workflow%3ACI)
1
+ [![CI](https://github.com/SamSaffron/memory_profiler/actions/workflows/ci.yml/badge.svg)](https://github.com/SamSaffron/memory_profiler/actions/workflows/ci.yml)
2
2
  [![Gem Version](https://badge.fury.io/rb/memory_profiler.svg)](https://rubygems.org/gems/memory_profiler)
3
3
 
4
4
  # MemoryProfiler
@@ -7,7 +7,7 @@ A memory profiler for Ruby
7
7
 
8
8
  ## Requirements
9
9
 
10
- Ruby(MRI) Version 2.5.0 and above.
10
+ Ruby(MRI) Version 3.1.0 and above.
11
11
 
12
12
  ## Installation
13
13
 
@@ -32,14 +32,23 @@ There are two ways to use `memory_profiler`:
32
32
  ### Command Line
33
33
 
34
34
  The easiest way to use memory_profiler is via the command line, which requires no modifications to your program. The basic usage is:
35
+
36
+ ```
37
+ $ ruby-memory-profiler [options] run [--] command [command-options]
35
38
  ```
36
- $ ruby-memory-profiler [options] <script.rb> [--] [script-options]
39
+
40
+ Example:
41
+
42
+ ```
43
+ $ ruby-memory-profiler --pretty run -- rubocop --cache false
44
+
45
+ $ ruby-memory-profiler --max=10 --pretty run -- ruby notify_users.rb 1 2 3 --quiet
37
46
  ```
38
- Where `script.rb` is the program you want to profile.
39
47
 
40
48
  For a full list of options, execute the following command:
49
+
41
50
  ```
42
- ruby-memory-profiler -h
51
+ $ ruby-memory-profiler -h
43
52
  ```
44
53
 
45
54
  ### Convenience API
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "memory_profiler"
4
+
5
+ def deserialize_hash(data)
6
+ Marshal.load(data.unpack1("m0")) if data
7
+ end
8
+
9
+ options = deserialize_hash(ENV["MEMORY_PROFILER_OPTIONS"]) || {}
10
+
11
+ at_exit do
12
+ report = MemoryProfiler.stop
13
+ report.pretty_print(**options)
14
+ end
15
+
16
+ MemoryProfiler.start(options)
@@ -14,20 +14,6 @@ module MemoryProfiler
14
14
  ignore_files: "memory_profiler/lib"
15
15
  }.freeze
16
16
 
17
- REPORTER_KEYS = [
18
- :top, :trace, :ignore_files, :allow_files
19
- ].freeze
20
-
21
- RESULTS_KEYS = [
22
- :to_file, :color_output, :retained_strings, :allocated_strings,
23
- :detailed_report, :scale_bytes, :normalize_paths
24
- ].freeze
25
-
26
- private_constant :BIN_NAME, :VERSION_INFO,:STATUS_SUCCESS, :STATUS_ERROR,
27
- :DEFAULTS, :REPORTER_KEYS, :RESULTS_KEYS
28
-
29
- #
30
-
31
17
  def run(argv)
32
18
  options = {}
33
19
  parser = option_parser(options)
@@ -43,17 +29,24 @@ module MemoryProfiler
43
29
  return STATUS_ERROR
44
30
  end
45
31
 
46
- MemoryProfiler.start(reporter_options(options))
47
- load script
48
-
49
- STATUS_SUCCESS
32
+ if script == "run"
33
+ # We are profiling a command.
34
+ profile_command(options, argv)
35
+ else
36
+ # We are profiling a ruby file.
37
+ begin
38
+ MemoryProfiler.start(options)
39
+ load(script)
40
+ ensure
41
+ report = MemoryProfiler.stop
42
+ report.pretty_print(**options)
43
+ end
44
+ STATUS_SUCCESS
45
+ end
50
46
  rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e
51
47
  puts parser
52
48
  puts e.message
53
49
  STATUS_ERROR
54
- ensure
55
- report = MemoryProfiler.stop
56
- report&.pretty_print(**results_options(options))
57
50
  end
58
51
 
59
52
  private
@@ -66,7 +59,7 @@ module MemoryProfiler
66
59
  A Memory Profiler for Ruby
67
60
 
68
61
  Usage:
69
- #{BIN_NAME} [options] <script.rb> [--] [script-options]
62
+ #{BIN_NAME} [options] run [--] command [command-options]
70
63
  BANNER
71
64
 
72
65
  opts.separator ""
@@ -138,12 +131,16 @@ module MemoryProfiler
138
131
  end
139
132
  end
140
133
 
141
- def reporter_options(options)
142
- options.select { |k, _v| REPORTER_KEYS.include?(k) }
134
+ def profile_command(options, argv)
135
+ env = {}
136
+ env["MEMORY_PROFILER_OPTIONS"] = serialize_hash(options) if options.any?
137
+ gem_path = File.expand_path('../', __dir__)
138
+ env["RUBYOPT"] = "-I #{gem_path} -r memory_profiler/autorun #{ENV['RUBYOPT']}"
139
+ exec(env, *argv)
143
140
  end
144
141
 
145
- def results_options(options)
146
- options.select { |k, _v| RESULTS_KEYS.include?(k) }
142
+ def serialize_hash(hash)
143
+ [Marshal.dump(hash)].pack("m0")
147
144
  end
148
145
  end
149
146
  end
@@ -6,7 +6,6 @@ module MemoryProfiler
6
6
  def initialize
7
7
  @gem_guess_cache = Hash.new
8
8
  @location_cache = Hash.new { |h, k| h[k] = Hash.new.compare_by_identity }
9
- @class_name_cache = Hash.new.compare_by_identity
10
9
  @string_cache = Hash.new
11
10
  end
12
11
 
@@ -30,34 +29,17 @@ module MemoryProfiler
30
29
  end
31
30
 
32
31
  KERNEL_CLASS_METHOD = Kernel.instance_method(:class)
33
- if UnboundMethod.method_defined?(:bind_call)
34
- def object_class(obj)
35
- klass = obj.class rescue nil
36
- unless Class === klass
37
- # attempt to determine the true Class when .class returns something other than a Class
38
- klass = KERNEL_CLASS_METHOD.bind_call(obj)
39
- end
40
- klass
41
- end
42
- else
43
- def object_class(obj)
44
- klass = obj.class rescue nil
45
- unless Class === klass
46
- # attempt to determine the true Class when .class returns something other than a Class
47
- klass = KERNEL_CLASS_METHOD.bind(obj).call
48
- end
49
- klass
32
+ def object_class(obj)
33
+ klass = obj.class rescue nil
34
+ unless Class === klass
35
+ # attempt to determine the true Class when .class returns something other than a Class
36
+ klass = KERNEL_CLASS_METHOD.bind_call(obj)
50
37
  end
38
+ klass
51
39
  end
52
40
 
53
- if Object.name.frozen? # Since Ruby 2.7 Module#name no longer allocate a new string
54
- def lookup_class_name(klass)
55
- ((klass.is_a?(Class) && klass.name) || '<<Unknown>>').to_s
56
- end
57
- else
58
- def lookup_class_name(klass)
59
- @class_name_cache[klass] ||= ((klass.is_a?(Class) && klass.name) || '<<Unknown>>').to_s
60
- end
41
+ def lookup_class_name(klass)
42
+ ((klass.is_a?(Class) && klass.name) || '<<Unknown>>').to_s
61
43
  end
62
44
 
63
45
  def lookup_string(obj)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MemoryProfiler
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memory_profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2024-09-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Memory profiling routines for Ruby 2.5+
13
+ description: Memory profiling routines for Ruby 3.1+
14
14
  email:
15
15
  - sam.saffron@gmail.com
16
16
  executables:
@@ -23,6 +23,7 @@ files:
23
23
  - README.md
24
24
  - bin/ruby-memory-profiler
25
25
  - lib/memory_profiler.rb
26
+ - lib/memory_profiler/autorun.rb
26
27
  - lib/memory_profiler/cli.rb
27
28
  - lib/memory_profiler/helpers.rb
28
29
  - lib/memory_profiler/monochrome.rb
@@ -36,7 +37,8 @@ files:
36
37
  homepage: https://github.com/SamSaffron/memory_profiler
37
38
  licenses:
38
39
  - MIT
39
- metadata: {}
40
+ metadata:
41
+ changelog_uri: https://github.com/SamSaffron/memory_profiler/blob/master/CHANGELOG.md
40
42
  post_install_message:
41
43
  rdoc_options: []
42
44
  require_paths:
@@ -45,15 +47,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
47
  requirements:
46
48
  - - ">="
47
49
  - !ruby/object:Gem::Version
48
- version: 2.5.0
50
+ version: 3.1.0
49
51
  required_rubygems_version: !ruby/object:Gem::Requirement
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
53
55
  version: '0'
54
56
  requirements: []
55
- rubygems_version: 3.3.20
57
+ rubygems_version: 3.5.11
56
58
  signing_key:
57
59
  specification_version: 4
58
- summary: Memory profiling routines for Ruby 2.5+
60
+ summary: Memory profiling routines for Ruby 3.1+
59
61
  test_files: []