allocation_stats 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ # Licensed under the Apache License, Version 2.0, found in the LICENSE file.
3
+
4
+ require_relative File.join("spec_helper")
5
+
6
+ describe AllocationStats do
7
+ it "should trace everything if TRACE_PROCESS_ALLOCATIONS" do
8
+ IO.popen({"TRACE_PROCESS_ALLOCATIONS" => "1"}, "ruby -r ./lib/allocation_stats -e 'puts 0'") do |io|
9
+ out = io.read
10
+ out.should match("Object Allocation Report")
11
+ end
12
+ end
13
+
14
+ it "should only track new objects" do
15
+ existing_array = [1,2,3,4,5]
16
+
17
+ stats = AllocationStats.trace do
18
+ new_array = [1,2,3,4,5]
19
+ end
20
+
21
+ stats.new_allocations.class.should be Array
22
+ stats.new_allocations.size.should == 1
23
+ end
24
+
25
+ it "should only track new objects, non-block mode" do
26
+ existing_array = [1,2,3,4,5]
27
+
28
+ stats = AllocationStats.trace
29
+ new_array = [1,2,3,4,5]
30
+ stats.stop
31
+
32
+ stats.new_allocations.class.should be Array
33
+ stats.new_allocations.size.should == 1
34
+ end
35
+
36
+ it "should only track new objects; String keys in Hashes count twice :(" do
37
+ existing_array = [1,2,3,4,5]
38
+
39
+ stats = AllocationStats.trace do
40
+ new_hash = {"foo" => "bar", "baz" => "quux"}
41
+ end
42
+
43
+ stats.new_allocations.size.should == 7
44
+ end
45
+
46
+ it "should only track new objects, using instance method" do
47
+ existing_array = [1,2,3,4,5]
48
+
49
+ stats = AllocationStats.new
50
+
51
+ stats.trace do
52
+ new_object = Object.new
53
+ new_array = [4]
54
+ new_string = "yarn"
55
+ end
56
+
57
+ stats.new_allocations.class.should be Array
58
+ stats.new_allocations.size.should == 3
59
+ end
60
+
61
+ it "should only track new objects" do
62
+ existing_array = [1,2,3,4,5]
63
+
64
+ my_instance = MyClass.new
65
+
66
+ stats = AllocationStats.new(burn: 3).trace do
67
+ # this method instantiates 2**(n-1) Strings on the n'th call
68
+ my_instance.memoizing_method
69
+ end
70
+
71
+ stats.new_allocations.class.should be Array
72
+ stats.new_allocations.size.should == 8
73
+ end
74
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ # Licensed under the Apache License, Version 2.0, found in the LICENSE file.
3
+
4
+ require_relative "../lib/allocation_stats"
5
+ require "yaml"
6
+ require "yajl"
7
+ require "pry"
8
+
9
+ if RbConfig::CONFIG["MAJOR"].to_i < 2 || RbConfig::CONFIG["MINOR"].to_i < 1
10
+ warn "Error: AllocationStats requires Ruby 2.1 or greater"
11
+ exit 1
12
+ end
13
+
14
+ def allocate_a_string_from_spec_helper
15
+ return "a string from spec_helper"
16
+ end
17
+
18
+ class MyClass
19
+ def my_method
20
+ @new_hash = {0 => "foo", 1 => "bar"}
21
+ end
22
+
23
+ MY_METHOD_BODY_LINE = __LINE__ - 3
24
+
25
+ # This method allocates a different number of objects each call:
26
+ # 1st call: 1x Array, 1x String
27
+ # 2nd call; 2x Strings
28
+ # 3rd call; 4x Strings
29
+ # 4th call; 8x Strings
30
+ def memoizing_method
31
+ @c ||= []
32
+
33
+ (@c.size + 1).times { @c << "string" }
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allocation_stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Rawlins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Tooling for tracing object allocations in Ruby 2.1
28
+ email:
29
+ - sam.rawlins@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .yardopts
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE
39
+ - README.markdown
40
+ - Rakefile
41
+ - TODO
42
+ - allocation_stats.gemspec
43
+ - examples/my_class.rb
44
+ - examples/trace_my_class_group_by.rb
45
+ - examples/trace_my_class_raw.rb
46
+ - examples/trace_object_allocations.rb
47
+ - examples/trace_psych_group_by.rb
48
+ - examples/trace_psych_keys.rb
49
+ - lib/active_support/core_ext/module/delegation.rb
50
+ - lib/allocation_stats.rb
51
+ - lib/allocation_stats/allocation.rb
52
+ - lib/allocation_stats/allocations_proxy.rb
53
+ - spec/allocation_stats/allocations_proxy_spec.rb
54
+ - spec/allocation_stats_spec.rb
55
+ - spec/spec_helper.rb
56
+ homepage:
57
+ licenses:
58
+ - Apache v2
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>'
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.99
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Tooling for tracing object allocations in Ruby 2.1
80
+ test_files: []