benchmark_g_c 1.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af54b96fde0fbc9a0e24fe66507ac9374bd5a96a
4
+ data.tar.gz: b3fc137a41a8e935a90cdca8f838870ce00cba8e
5
+ SHA512:
6
+ metadata.gz: 56ed8d4f500b4f15685789245cb680da694b1844c00c5b3dc5b656c3aa31cfe7f41d4dbc4fef1ce55449b576bcac0f8e0bcd415f5747788d44df8fc960c89ad0
7
+ data.tar.gz: 0b9ac1c1bb2156a085d2072ed0b18bdaf29831c7e3b9244408ee4589d29d7575a3073f0a46168618b435ca0ba2783190b9f6cef52a787fa63225950d9ccf6ffd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in benchmark_g_c.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nicholas Jacques
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # BenchmarkGC
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/benchmark_g_c`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'benchmark_g_c'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install benchmark_g_c
22
+
23
+ ## Usage
24
+
25
+ class Thing
26
+ include BenchmarkGC
27
+
28
+ def method_name
29
+ do_some_stuff
30
+ end
31
+ end
32
+
33
+ Thing.new.bcheck(logger: false) { method_name }
34
+
35
+ Parameter options (all are optional):
36
+ name: (string) Print the name of the benchmarked code
37
+ times: (integer) Number of times to run the benchmarked code in succession
38
+ logger: (boolean) ActiveRecord::Base.logger should log
39
+
40
+ bcheck returns the value of the benchmarked code, making this possible:
41
+ class Thing
42
+ ...
43
+ def method_name
44
+ bcheck { do_some_stuff }
45
+ end
46
+ end
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/benchmark_g_c.
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'benchmark_g_c/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "benchmark_g_c"
8
+ spec.version = BenchmarkGC::VERSION
9
+ spec.authors = ["Nicholas Jacques"]
10
+ spec.email = ["jnicholasjacques@gmail.com"]
11
+
12
+ spec.summary = %q{Benchmark Garbage Collect stats relevant to memory optimization.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "benchmark_g_c"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module BenchmarkGC
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,111 @@
1
+ require "benchmark_g_c/version"
2
+
3
+ module BenchmarkGC
4
+ @@total_objects_allocated = 0
5
+ @@total_bchecks = 0
6
+
7
+ def self.total_bchecks
8
+ @@total_bchecks
9
+ end
10
+
11
+ def self.increment_bchecks
12
+ @@total_bchecks += 1
13
+ end
14
+
15
+ def self.total_objects_allocated
16
+ @@total_objects_allocated
17
+ end
18
+
19
+ def self.add_objects(objects)
20
+ @@total_objects_allocated += objects
21
+ end
22
+
23
+ def bcheck(options = {})
24
+ default_options = { name: '', times: 1, logger: true }
25
+ options = default_options.merge(options)
26
+ name = options[:name]
27
+ times = options[:times]
28
+ logger = options[:logger]
29
+ # This should first check if gem is in Rails environment
30
+ ActiveRecord::Base.logger = nil unless logger
31
+ results = []
32
+ return_value = nil
33
+ times.times do
34
+ time_before = Time.now
35
+ stat_before = GC.stat
36
+ return_value = yield
37
+ time_after = Time.now
38
+ stat_after = GC.stat
39
+ results.push(
40
+ time_elapsed: time_after - time_before,
41
+ heap_free_slots_added: stat_after[:heap_free_slots] - stat_before[:heap_free_slots],
42
+ objects_allocated: stat_after[:total_allocated_objects] - stat_before[:total_allocated_objects],
43
+ objects_freed: stat_after[:total_freed_objects] - stat_before[:total_freed_objects]
44
+ )
45
+ print_benchmark(name, time_before, time_after, stat_before, stat_after)
46
+ end
47
+ averaged_results = average_results(results)
48
+ BenchmarkGC::increment_bchecks
49
+ BenchmarkGC::add_objects(averaged_results[:objects_allocated])
50
+ print_averaged_results(averaged_results, times)
51
+ return_value
52
+ end
53
+
54
+ def print_benchmark(name, time_before, time_after, stat_before, stat_after)
55
+ print "-------------------------------------------------------------------\n"
56
+ print "#{name}\n"
57
+ print "Time Elapsed: #{time_after - time_before}\n\n"
58
+ printf("%-25s %-14s %-14s %-14s\n", 'GC Stat', 'before', 'after', 'diff')
59
+ print "-------------------------------------------------------------------\n"
60
+ print_stats('heap_free_slots', stat_before[:heap_free_slots], stat_after[:heap_free_slots])
61
+ print_stats('total_allocated_objects', stat_before[:total_allocated_objects], stat_after[:total_allocated_objects])
62
+ print_stats('total_freed_objects', stat_before[:total_freed_objects], stat_after[:total_freed_objects])
63
+ print "-------------------------------------------------------------------\n"
64
+ end
65
+
66
+ def print_stats(name, before, after)
67
+ formatter = "%-25s %-14d %-14d %-14d\n"
68
+ printf(formatter, name, before, after, after - before)
69
+ end
70
+
71
+ def average_results(results)
72
+ summed_results = sum_results(results)
73
+ summed_results.each do |key, value|
74
+ summed_results[key] = value / results.length
75
+ end
76
+ end
77
+
78
+ def sum_results(results)
79
+ summed_results = results[0].clone
80
+ results[1..-1].each do |result|
81
+ summed_results.keys.each do |key|
82
+ summed_results[key] += result[key]
83
+ end
84
+ end
85
+ summed_results
86
+ end
87
+
88
+ def print_averaged_results(results, times_run)
89
+ print "-------------------------------------------------------------------\n"
90
+ print "Average Results after running #{times_run} times\n"
91
+ print "-------------------------------------------------------------------\n"
92
+ formatter = "%-25s %-14s\n"
93
+ results.each do |key, value|
94
+ printf(formatter, key, underscore_number(value))
95
+ end
96
+ print "Total bchecks run for server instance: #{underscore_number BenchmarkGC::total_bchecks}\n"
97
+ print "Total Objects Allocated for server instance: #{underscore_number BenchmarkGC::total_objects_allocated}\n"
98
+ print "-------------------------------------------------------------------\n"
99
+ end
100
+
101
+ def underscore_number(number)
102
+ insert_underscores(number.round.to_s.reverse).reverse
103
+ end
104
+
105
+ def insert_underscores(string)
106
+ (1..(string.length - 1) / 3).each do |n|
107
+ string.insert(n * 3 + n - 1, '_') unless string[n * 3 + n - 1] == '-'
108
+ end
109
+ string
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benchmark_g_c
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Jacques
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-06 00:00:00.000000000 Z
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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - jnicholasjacques@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - benchmark_g_c.gemspec
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/benchmark_g_c.rb
57
+ - lib/benchmark_g_c/version.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Benchmark Garbage Collect stats relevant to memory optimization.
82
+ test_files: []