benchmark-malloc 0.1.0 → 0.2.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: 95f4aeccd4fe1f05278d14d87023639aad34522818a4189d7b72479351ce2565
4
- data.tar.gz: faf9e95804733e6de3a77e32c7f6965e27358bc157810857de5da55c074fb7df
3
+ metadata.gz: 48576a6003a6fd1a9a0f38673fe26a07d1c08a273a30b35a70129eb0cca17295
4
+ data.tar.gz: 7260d8c45bab763a0fa86e3a0f83e433bd636e4281c7efd58e528225d08a4948
5
5
  SHA512:
6
- metadata.gz: 1ada0b8d9a8b86ee935ec5ca3acb871fff6b8a110b154fdde435f793271e42a95907383303d8151856ce252c91b1ce605f14cf7c3802d0351967bfc935e6812f
7
- data.tar.gz: 4b35be840c0e8a63730a5627e36efb53e19ba4fdae5d106544d37a8bf325bf6f6c35a90d3aafedf2ce0f84fb117e45a18b24ddfd0d1cd0842dc93a35fec81a54
6
+ metadata.gz: c9729e0e361bcc64c7b0c9563d0c10ad351e2275d2de3ee49358ae9f99f893079d1304af5208f587aab31ab54935317c73c1ce164ee5ae2924943c5fe9a5e0c5
7
+ data.tar.gz: 12d4b77967a79b3a8c87c8e5198f87f5c104dc62b5308e3fe1201158817e000c3bc75bb6853c8fde33c9f7f665c48b57ad7383daf301f0ae0b2ed0b120cf85a4
@@ -1,7 +1,14 @@
1
1
  # Change log
2
2
 
3
- ## [v0.1.0] - 2016-04-06
3
+ ## [v0.2.0] - 2020-03-07
4
+
5
+ ### Changed
6
+ * Change class level #run call to #trace
7
+ * Change gemspec to only load source files
8
+
9
+ ## [v0.1.0] - 2019-04-06
4
10
 
5
11
  Initial release
6
12
 
13
+ [v0.2.0]: https://github.com/piotrmurach/benchmark-malloc/compare/v0.1.0...v0.2.0
7
14
  [v0.1.0]: https://github.com/piotrmurach/benchmark-malloc/compare/v0.1.0
data/README.md CHANGED
@@ -16,6 +16,8 @@
16
16
 
17
17
  > Trace memory allocations and collect stats.
18
18
 
19
+ The **Benchmark::Malloc** is used by [rspec-benchmark](https://github.com/piotrmurach/rspec-benchmark)
20
+
19
21
  ## Installation
20
22
 
21
23
  Add this line to your application's Gemfile:
@@ -35,7 +37,7 @@ Or install it yourself as:
35
37
  ## Usage
36
38
 
37
39
  ```ruby
38
- benc_malloc = Benchmark::Malloc.new
40
+ bench_malloc = Benchmark::Malloc.new
39
41
 
40
42
  stats = bench_malloc.run { %w[foo bar baz].sort[1] }
41
43
 
@@ -44,6 +46,29 @@ stats.allocated.total_objects # => 3
44
46
  stats.allocated.total_memory # => 120
45
47
  ```
46
48
 
49
+ ## API
50
+
51
+ ### start & stop
52
+
53
+ You can manually begin tracing memory allocations with the `start` method:
54
+
55
+ ```ruby
56
+ malloc = Benchmark::Malloc.new
57
+ malloc.start
58
+ ```
59
+
60
+ Any Ruby code after the `start` invocation will count towards the stats:
61
+
62
+ ```ruby
63
+ %w[foo bar baz].sort[1]
64
+ ```
65
+
66
+ Finally, to finish tracing call the `stop` method:
67
+
68
+ ```ruby
69
+ malloc.stop
70
+ ```
71
+
47
72
  ## Development
48
73
 
49
74
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -52,7 +77,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
52
77
 
53
78
  ## Contributing
54
79
 
55
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/benchmark-malloc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/benchmark-malloc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
81
 
57
82
  ## License
58
83
 
@@ -1 +1 @@
1
- require_relative 'benchmark/malloc'
1
+ require_relative "benchmark/malloc"
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'objspace'
3
+ require "objspace"
4
4
 
5
- require_relative 'malloc/allocation'
6
- require_relative 'malloc/allocation_set'
7
- require_relative 'malloc/allocation_result'
8
- require_relative 'malloc/version'
5
+ require_relative "malloc/allocation"
6
+ require_relative "malloc/allocation_set"
7
+ require_relative "malloc/allocation_result"
8
+ require_relative "malloc/version"
9
9
 
10
10
  module Benchmark
11
11
  class Malloc
@@ -18,14 +18,20 @@ module Benchmark
18
18
  # @api public
19
19
  attr_reader :warmup
20
20
 
21
- def self.run(&work)
21
+ # Trace memory allocations
22
+ #
23
+ # @api public
24
+ def self.trace(&work)
22
25
  Malloc.new.run(&work)
23
26
  end
24
27
 
28
+ # Create a memory allocation tracer
29
+ #
30
+ # @api public
25
31
  def initialize(warmup: 0)
26
32
  @warmup = warmup
27
33
  @running = false
28
- @alloc_path = ::File.join(__FILE__[0...-3], 'allocation.rb')
34
+ @alloc_path = ::File.join(__FILE__[0...-3], "allocation.rb")
29
35
  end
30
36
 
31
37
  # @api private
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'objspace'
3
+ require "objspace"
4
4
 
5
5
  module Benchmark
6
6
  class Malloc
@@ -1,7 +1,7 @@
1
- # frozen_stirng_literal: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Benchmark
4
4
  class Malloc
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end # Malloc
7
7
  end # Benchmark
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchmark-malloc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-06 00:00:00.000000000 Z
11
+ date: 2020-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '1.17'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '1.17'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -42,48 +28,45 @@ dependencies:
42
28
  name: rspec
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - ">="
31
+ - - "~>"
46
32
  - !ruby/object:Gem::Version
47
33
  version: '3.0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - ">="
38
+ - - "~>"
53
39
  - !ruby/object:Gem::Version
54
40
  version: '3.0'
55
41
  description: Trace memory allocations and collect stats.
56
42
  email:
57
- - me@piotrmurach.com
43
+ - piotr@piotrmurach.com
58
44
  executables: []
59
45
  extensions: []
60
- extra_rdoc_files: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ - CHANGELOG.md
49
+ - LICENSE.txt
61
50
  files:
62
51
  - CHANGELOG.md
63
52
  - LICENSE.txt
64
53
  - README.md
65
- - Rakefile
66
- - benchmark-malloc.gemspec
67
54
  - lib/benchmark-malloc.rb
68
55
  - lib/benchmark/malloc.rb
69
56
  - lib/benchmark/malloc/allocation.rb
70
57
  - lib/benchmark/malloc/allocation_result.rb
71
58
  - lib/benchmark/malloc/allocation_set.rb
72
59
  - lib/benchmark/malloc/version.rb
73
- - spec/spec_helper.rb
74
- - spec/unit/allocation/new_spec.rb
75
- - spec/unit/allocation_set/new_spec.rb
76
- - spec/unit/run_spec.rb
77
- - tasks/console.rake
78
- - tasks/coverage.rake
79
- - tasks/spec.rake
80
60
  homepage: https://github.com/piotrmurach/benchmark-malloc
81
61
  licenses:
82
62
  - MIT
83
63
  metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ bug_tracker_uri: https://github.com/piotrmurach/benchmark-malloc/issues
66
+ changelog_uri: https://github.com/piotrmurach/benchmark-malloc/CHANGELOG.md
67
+ documentation_uri: https://www.rubydoc.info/gems/benchmark-malloc
84
68
  homepage_uri: https://github.com/piotrmurach/benchmark-malloc
85
69
  source_code_uri: https://github.com/piotrmurach/benchmark-malloc
86
- changelog_uri: https://github.com/piotrmurach/benchmark-malloc/CHANGELOG.md
87
70
  post_install_message:
88
71
  rdoc_options: []
89
72
  require_paths:
@@ -99,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
82
  - !ruby/object:Gem::Version
100
83
  version: '0'
101
84
  requirements: []
102
- rubygems_version: 3.0.3
85
+ rubygems_version: 3.1.2
103
86
  signing_key:
104
87
  specification_version: 4
105
88
  summary: Trace memory allocations and collect stats.
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
3
- FileList['tasks/**/*.rake'].each(&method(:import))
4
-
5
- desc 'Run all specs'
6
- task ci: %w[ spec ]
7
-
8
- task default: :spec
@@ -1,34 +0,0 @@
1
- lib = File.expand_path("../lib", __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "benchmark/malloc/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "benchmark-malloc"
7
- spec.version = Benchmark::Malloc::VERSION
8
- spec.authors = ["Piotr Murach"]
9
- spec.email = ["me@piotrmurach.com"]
10
-
11
- spec.summary = %q{Trace memory allocations and collect stats.}
12
- spec.description = %q{Trace memory allocations and collect stats.}
13
- spec.homepage = "https://github.com/piotrmurach/benchmark-malloc"
14
- spec.license = "MIT"
15
-
16
- if spec.respond_to?(:metadata)
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/piotrmurach/benchmark-malloc"
19
- spec.metadata["changelog_uri"] = "https://github.com/piotrmurach/benchmark-malloc/CHANGELOG.md"
20
- end
21
-
22
- spec.files = Dir['{lib,spec}/**/*.rb']
23
- spec.files += Dir['tasks/*', 'benchmark-malloc.gemspec']
24
- spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
- spec.require_paths = ["lib"]
28
-
29
- spec.required_ruby_version = '>= 2.1.0'
30
-
31
- spec.add_development_dependency "bundler", ">= 1.17"
32
- spec.add_development_dependency "rake"
33
- spec.add_development_dependency "rspec", ">= 3.0"
34
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV['COVERAGE'] || ENV['TRAVIS']
4
- require 'simplecov'
5
- require 'coveralls'
6
-
7
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
- SimpleCov::Formatter::HTMLFormatter,
9
- Coveralls::SimpleCov::Formatter
10
- ]
11
-
12
- SimpleCov.start do
13
- command_name 'spec'
14
- add_filter 'spec'
15
- end
16
- end
17
-
18
- require "bundler/setup"
19
- require "benchmark-malloc"
20
-
21
- RSpec.configure do |config|
22
- # Enable flags like --only-failures and --next-failure
23
- config.example_status_persistence_file_path = ".rspec_status"
24
-
25
- # Disable RSpec exposing methods globally on `Module` and `main`
26
- config.disable_monkey_patching!
27
-
28
- config.expect_with :rspec do |c|
29
- c.syntax = :expect
30
- end
31
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Benchmark::Malloc::Allocation do
4
- it "gathers info about allocated object" do
5
- object = Object.new
6
- alloc = described_class.new(object)
7
-
8
- expect(alloc.memsize).to be <= 40
9
- expect(alloc.source_line).to eq(nil)
10
- expect(alloc.method_id).to eq(nil)
11
- expect(alloc.class_path).to eq(nil)
12
- expect(alloc.source_file).to eq(nil)
13
- end
14
-
15
- it "extracts allocation info" do
16
- object = Object.new
17
- alloc = described_class.new(object)
18
-
19
- extracted = alloc.extract(:class, :memsize)
20
-
21
- expect(extracted[0]).to eq(Object)
22
- expect(extracted[1]).to be <= 40
23
- end
24
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Benchmark::Malloc::AllocationSet do
4
-
5
- def make_allocation(object)
6
- Benchmark::Malloc::Allocation.new(object)
7
- end
8
-
9
- it "counts allocated objects" do
10
- object_alloc = make_allocation(Object.new)
11
- hash_alloc = make_allocation({Object.new => :foo})
12
- string_alloc = make_allocation(:bar)
13
-
14
- allocations = [object_alloc, hash_alloc, string_alloc]
15
- alloc_set = described_class.new(allocations)
16
-
17
- expect(alloc_set.count_objects).to eq({Hash => 1, Object => 1, Symbol => 1})
18
- expect(alloc_set.total_objects).to eq(3)
19
- end
20
-
21
- it "counts allocated memory" do
22
- object_alloc = make_allocation(Object.new)
23
- hash_alloc = make_allocation({Object.new => :foo})
24
- string_alloc = make_allocation(:bar)
25
-
26
- allocations = [object_alloc, hash_alloc, string_alloc]
27
- alloc_set = described_class.new(allocations)
28
-
29
- expect(alloc_set.count_memory[Hash]).to be <= 240
30
- expect(alloc_set.count_memory[Object]).to be <= 40
31
- expect(alloc_set.count_memory[Symbol]).to eq 0
32
-
33
- expect(alloc_set.total_memory).to be < 300
34
- end
35
-
36
- it "filters allocated objects" do
37
- object_alloc = make_allocation(Object.new)
38
- hash_alloc = make_allocation({Object.new => :foo})
39
- string_alloc = make_allocation(:bar)
40
-
41
- allocations = [object_alloc, hash_alloc, string_alloc]
42
- alloc_set = described_class.new(allocations)
43
-
44
- expect(alloc_set.filter(Object)).to eq([object_alloc])
45
- end
46
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Benchmark::Malloc do
4
- it "traces only new object allocations" do
5
- %i[foo bar baz].freeze
6
-
7
- sample = described_class.run do
8
- %i[foo bar baz].freeze
9
- end
10
-
11
- expect(sample.allocated.total_objects).to eq(1)
12
- expect(sample.allocated.total_memory).to be <= 40
13
- end
14
-
15
- it "traces block assigned instances" do
16
- memory = described_class.new
17
-
18
- sample = memory.run do
19
- _new_object = Object.new
20
- _new_array = [:baz]
21
- _new_string = 'foo' + 'baz'
22
- end
23
-
24
- # allocated
25
- expect(sample.allocated.total_objects).to be <= 5
26
- expect(sample.allocated.total_memory).to be <= 200
27
-
28
- expect(sample.allocated.count_objects[Object]).to eq(1)
29
- expect(sample.allocated.count_objects[Array]).to eq(1)
30
- expect(sample.allocated.count_objects[String]).to be <= 3
31
-
32
- expect(sample.allocated.count_memory[Object]).to be <= 40
33
- expect(sample.allocated.count_memory[String]).to be <= 120
34
- expect(sample.allocated.count_memory[Array]).to be <= 40
35
-
36
- # retained
37
- expect(sample.retained.total_objects).to be <= 5
38
- expect(sample.retained.total_memory).to be <= 200
39
-
40
- expect(sample.retained.count_objects[Object]).to eq(1)
41
- expect(sample.retained.count_objects[Array]).to eq(1)
42
- expect(sample.retained.count_objects[String]).to be <= 3
43
-
44
- expect(sample.retained.count_memory[Object]).to be <= 40
45
- expect(sample.retained.count_memory[String]).to be <= 120
46
- expect(sample.retained.count_memory[Array]).to be <= 40
47
- end
48
-
49
- it "traces large number of objects" do
50
- result = described_class.run do
51
- 10.times { |i| [i.to_s, {}] }
52
- end
53
-
54
- expect(result.allocated.total_objects).to eq(10 * 3)
55
- expect(result.allocated.total_memory).to be <= (3120)
56
- expect(result.allocated.count_objects).to eq({
57
- Array => 10, String => 10, Hash => 10})
58
-
59
- # memory
60
- expect(result.allocated.count_memory[Array]).to be <= 400
61
- expect(result.allocated.count_memory[String]).to be <= 400
62
- expect(result.allocated.count_memory[Hash]).to be <= 2400
63
- end
64
-
65
- it "raises when stopped without starting" do
66
- expect {
67
- described_class.new.stop
68
- }.to raise_error(Benchmark::Malloc::Error, "not started yet")
69
- end
70
-
71
- it "raises when started again" do
72
- expect {
73
- malloc = described_class.new
74
- malloc.start
75
- malloc.start
76
- }.to raise_error(Benchmark::Malloc::Error, "already running")
77
- end
78
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- desc 'Load gem inside irb console'
4
- task :console do
5
- require 'irb'
6
- require 'irb/completion'
7
- require_relative '../lib/benchmark-malloc'
8
- ARGV.clear
9
- IRB.start
10
- end
11
- task c: %w[ console ]
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- desc 'Measure code coverage'
4
- task :coverage do
5
- begin
6
- original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
- Rake::Task['spec'].invoke
8
- ensure
9
- ENV['COVERAGE'] = original
10
- end
11
- end
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
-
3
- begin
4
- require 'rspec/core/rake_task'
5
-
6
- desc 'Run all specs'
7
- RSpec::Core::RakeTask.new(:spec) do |task|
8
- task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
- end
10
-
11
- namespace :spec do
12
- desc 'Run unit specs'
13
- RSpec::Core::RakeTask.new(:unit) do |task|
14
- task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
- end
16
-
17
- desc 'Run integration specs'
18
- RSpec::Core::RakeTask.new(:integration) do |task|
19
- task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
- end
21
-
22
- desc 'Run performance specs'
23
- RSpec::Core::RakeTask.new(:perf) do |task|
24
- task.pattern = 'spec/performance{,/*/**}/*_spec.rb'
25
- end
26
- end
27
-
28
- rescue LoadError
29
- %w[spec spec:unit spec:integration spec:perf].each do |name|
30
- task name do
31
- $stderr.puts "In order to run #{name}, do `gem install rspec`"
32
- end
33
- end
34
- end