memory_profiler 1.0.1 → 1.0.2

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: a33279b5aa11573ac4eb1c99c14d8ec0ac944827da175b6eef03d77bc57c652f
4
+ data.tar.gz: dc21db4963eb3840031d72de3b095a1a3414265dac0cd7df06de0c83ef0b95ad
5
5
  SHA512:
6
- metadata.gz: 11d3bf57b920c915a0a98f6cea8798349983b0c64047f481ab1227301ff100a8de16f858ee7d21028d32e3dd04015446f77555bfd050755637714da5d1cc08b3
7
- data.tar.gz: 46d2df335163a6ddc35ea3a0eff97573dc7b8a50d671e9aa8940a5acb300ea342d7eb38e10f03f39f3705c4e9ed318a336c68f72ddf39a5cdf4f6bb81b9cc7e5
6
+ metadata.gz: 421e253a198c2bc3881f57ac9f016bf69a5644f96eef211c1073442ce45b6a79021c3af2cdd77a8f916607080127d96a991c7c4fcd7b650ef107dd93161233ee
7
+ data.tar.gz: 8582b18f48088f242734d353ddd86a09ee9e2a1b2bb1ccdd1e066dd45ed29da2ef3417f2b1a82667c3863445358464f73fdc89e1e715c621d627469fc7b39be4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.2 - 17-06-2024
4
+
5
+ - Add ability to profile commands via CLI @fatkodima
6
+
3
7
  ## 1.0.1 - 23-10-2022
4
8
 
5
9
  - Adapts tests to Ruby 3.0 / 3.1
data/README.md CHANGED
@@ -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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "memory_profiler"
4
+ require "base64"
5
+
6
+ def deserialize_hash(data)
7
+ Marshal.load(Base64.urlsafe_decode64(data)) if data
8
+ end
9
+
10
+ options = deserialize_hash(ENV["MEMORY_PROFILER_OPTIONS"]) || {}
11
+
12
+ at_exit do
13
+ report = MemoryProfiler.stop
14
+ report.pretty_print(**options)
15
+ end
16
+
17
+ MemoryProfiler.start(options)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "optparse"
4
+ require "base64"
4
5
 
5
6
  module MemoryProfiler
6
7
  class CLI
@@ -14,20 +15,6 @@ module MemoryProfiler
14
15
  ignore_files: "memory_profiler/lib"
15
16
  }.freeze
16
17
 
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
18
  def run(argv)
32
19
  options = {}
33
20
  parser = option_parser(options)
@@ -43,17 +30,24 @@ module MemoryProfiler
43
30
  return STATUS_ERROR
44
31
  end
45
32
 
46
- MemoryProfiler.start(reporter_options(options))
47
- load script
48
-
49
- STATUS_SUCCESS
33
+ if script == "run"
34
+ # We are profiling a command.
35
+ profile_command(options, argv)
36
+ else
37
+ # We are profiling a ruby file.
38
+ begin
39
+ MemoryProfiler.start(options)
40
+ load(script)
41
+ ensure
42
+ report = MemoryProfiler.stop
43
+ report.pretty_print(**options)
44
+ end
45
+ STATUS_SUCCESS
46
+ end
50
47
  rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e
51
48
  puts parser
52
49
  puts e.message
53
50
  STATUS_ERROR
54
- ensure
55
- report = MemoryProfiler.stop
56
- report&.pretty_print(**results_options(options))
57
51
  end
58
52
 
59
53
  private
@@ -66,7 +60,7 @@ module MemoryProfiler
66
60
  A Memory Profiler for Ruby
67
61
 
68
62
  Usage:
69
- #{BIN_NAME} [options] <script.rb> [--] [script-options]
63
+ #{BIN_NAME} [options] run [--] command [command-options]
70
64
  BANNER
71
65
 
72
66
  opts.separator ""
@@ -138,12 +132,16 @@ module MemoryProfiler
138
132
  end
139
133
  end
140
134
 
141
- def reporter_options(options)
142
- options.select { |k, _v| REPORTER_KEYS.include?(k) }
135
+ def profile_command(options, argv)
136
+ env = {}
137
+ env["MEMORY_PROFILER_OPTIONS"] = serialize_hash(options) if options.any?
138
+ gem_path = File.expand_path('../', __dir__)
139
+ env["RUBYOPT"] = "-I #{gem_path} -r memory_profiler/autorun #{ENV['RUBYOPT']}"
140
+ exec(env, *argv)
143
141
  end
144
142
 
145
- def results_options(options)
146
- options.select { |k, _v| RESULTS_KEYS.include?(k) }
143
+ def serialize_hash(hash)
144
+ Base64.urlsafe_encode64(Marshal.dump(hash))
147
145
  end
148
146
  end
149
147
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MemoryProfiler
4
- VERSION = "1.0.1"
4
+ VERSION = "1.0.2"
5
5
  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: 1.0.1
4
+ version: 1.0.2
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-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Memory profiling routines for Ruby 2.5+
14
14
  email:
@@ -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
@@ -52,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
53
  - !ruby/object:Gem::Version
53
54
  version: '0'
54
55
  requirements: []
55
- rubygems_version: 3.3.20
56
+ rubygems_version: 3.5.11
56
57
  signing_key:
57
58
  specification_version: 4
58
59
  summary: Memory profiling routines for Ruby 2.5+