class_profiler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79286d695e8c01ec9433d4908005f7d6f232acd9
4
+ data.tar.gz: e3a38bcbf80398cabfd79ccde4b96ef7cdde7413
5
+ SHA512:
6
+ metadata.gz: bde33ef191d9ee417feff45649ddf64daba6c5c61769b71b0bf0dea9bc551de74c14827fb6af00c326142cf981281cad7c7878bc3ee8d0753c76fc6dd85e97e8
7
+ data.tar.gz: 1373a8272789a554b327f67bf08ad735ca22f5ebda05b23f16a3c681f76aec1001165ce03f5cacb9f50952dee068ddffc8e6f1526ec3384c3c0ba96c314b2306
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/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vasilakisfil@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in class_profiler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Filippos Vasilakis
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,80 @@
1
+ # ClassProfiler
2
+ Simple performance analyzer for Ruby classes. Just include it in the bottom of
3
+ your class and let it analyze your Ruby class performance.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'class_profiler'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install class_profiler
20
+
21
+ ## Usage
22
+ It can be used only for classes. You need to include it in the **bottom** of your class:
23
+
24
+ ```ruby
25
+ class Foobar
26
+ #includes/extend definitions
27
+
28
+ #methods
29
+ def hard_working_method
30
+ end
31
+ #more methods
32
+
33
+ include ClassProfiler #include it just before closing the class
34
+ end
35
+ ```
36
+
37
+ Or if you want something more configurable, to measure only specific methods and/or
38
+ modules you can use this little API:
39
+ ```ruby
40
+ include ClassProfiler.for(instance_methods: [
41
+ :on_correct_scale, :on_correct_currency, :as_financial_value
42
+ ], modules: [Financial, Company])
43
+ ```
44
+
45
+ It comes with a built-in rack middleware to report you the whole request-response cycle
46
+ (but you still have to `include ClassProfiler` in the classes of your interest)
47
+
48
+ ```ruby
49
+ config.middleware.use ClassProfiler::Rack
50
+ ```
51
+
52
+ If you want something more specific, then you only need to wrap the code of your
53
+ interest under `start` block:
54
+
55
+ ```ruby
56
+ ClassProfiler::Benchmark.instance.start 'MY LABEL' do
57
+ #code I want to benchmark
58
+ end
59
+ ```
60
+
61
+ Note that `ClassProfiler::Benchmark` class is a singleton.
62
+
63
+ ## How does it work?
64
+ You can find details on the motivation and implementation in [this blog post](https://blog.kollegorna.se/ruby-performance-profiling-an-unorthodox-approach-69c549e3293b).
65
+
66
+ ## Development
67
+
68
+ 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.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vasilakisfil/class_profiler. 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.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "class_profiler"
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
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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'class_profiler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "class_profiler"
8
+ spec.version = ClassProfiler::VERSION
9
+ spec.authors = ["Filippos Vasilakis"]
10
+ spec.email = ["vasilakisfil@gmail.com"]
11
+
12
+ spec.summary = %q{Simple class performance profiler with some metaprogramming alcohol}
13
+ spec.description = %q{Simple class performance profiler with some metaprogramming alcohol}
14
+ spec.homepage = "https://github.com/vasilakisfil/class_profiler"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,87 @@
1
+ require "class_profiler/version"
2
+ require "class_profiler/benchmark"
3
+ require "class_profiler/object_label"
4
+ require "class_profiler/rack"
5
+
6
+ #tons of metaprogramming and hacks, don't ask..
7
+ module ClassProfiler
8
+ CONSTANTIZE = ->(string) {
9
+ string.split(':').select{|i|
10
+ !i.blank? && i.downcase != '<class' && !i.start_with?('0x')
11
+ }.map{|i|
12
+ i.gsub("#","").gsub("<","").gsub(">","")
13
+ }.join("::").constantize #use const_get
14
+ }
15
+
16
+ #add some metaprogramming here
17
+ IGNORED_METHODS = [Object, BasicObject, Class, Class.new, Module].map{|o|
18
+ array = []
19
+ [:private_instance_methods, :protected_instance_methods, :instance_methods].each do |m|
20
+ array.concat(o.send(m, true))
21
+ end
22
+
23
+ [:private_methods, :protected_methods, :methods].each do |m|
24
+ array.concat(o.send(m, true))
25
+ end
26
+ array
27
+ }.flatten.uniq - [:initialize]
28
+
29
+ def self.for(options = {})
30
+ methods = options[:instance_methods] || []
31
+ _caller = CONSTANTIZE.call(caller_locations(1,1)[0].label)
32
+
33
+ if options[:modules]
34
+ methods.concat(
35
+ options[:modules].map{|m|
36
+ _get_methods_for(m)
37
+ }
38
+ )
39
+ end
40
+
41
+ @__cp_instance_methods = methods.flatten.uniq
42
+
43
+ return self
44
+ end
45
+
46
+ def self.included(base)
47
+ @__cp_instance_methods = _get_methods_for(base, true) if @__cp_instance_methods.nil?
48
+
49
+ default_protected_methods = self.protected_instance_methods
50
+ default_private_methods = self.private_instance_methods
51
+
52
+ (@__cp_instance_methods).each do |method_name|
53
+ base.send(:alias_method, "__#{method_name}", method_name)
54
+
55
+ base.send(:define_method, method_name) do |*args, &block|
56
+ Benchmark.instance.start(label(__method__)){
57
+ if block
58
+ self.send("__#{method_name}", *args, &block)
59
+ else
60
+ self.send("__#{method_name}", *args)
61
+ end
62
+ }
63
+ end
64
+
65
+ protected(method_name) if default_protected_methods.include?(method_name)
66
+ private(method_name) if default_private_methods.include?(method_name)
67
+ end
68
+
69
+ @__cp_instance_methods = nil
70
+ end
71
+
72
+ def self._get_methods_for(_caller, inherited = false)
73
+ _caller.constantize if _caller.is_a? String #use const_get
74
+
75
+ array = []
76
+ array.concat(
77
+ _caller.instance_methods(inherited)
78
+ ).concat(
79
+ _caller.protected_instance_methods(inherited)
80
+ ).concat(
81
+ _caller.private_instance_methods(inherited)
82
+ )
83
+
84
+ return array unless inherited
85
+ return (array - IGNORED_METHODS).flatten.uniq
86
+ end
87
+ end
@@ -0,0 +1,67 @@
1
+ require "class_profiler"
2
+
3
+ class ClassProfiler::Benchmark
4
+ include Singleton
5
+
6
+ def initialize(options = {})
7
+ @options = options
8
+ @sum_hash = {}
9
+ @active_labels = []
10
+ end
11
+
12
+ def start(label, &block)
13
+ append_active_label(label)
14
+
15
+ value = nil
16
+ time = ::Benchmark.measure {
17
+ value = block.call
18
+ }.real
19
+
20
+ sum_hash[label] = {num: 0, sum: 0} if sum_hash[label].nil?
21
+ sum_hash[label][:num] += 1
22
+ sum_hash[label][:sum] += time.round(5)
23
+
24
+ remove_active_label(label)
25
+ return value
26
+ end
27
+
28
+ def start_and_report(label = 'Total Time', &block)
29
+ bench!(label, &block)
30
+
31
+ report!(label)
32
+ end
33
+
34
+ def report(total_label = nil)
35
+ printf "######### Performance Report #########\n"
36
+ if sum_hash[total_label]
37
+ total_time = sum_hash[total_label][:sum].round(5)
38
+ puts total_time
39
+ sum_hash.sort_by{|label, values| values[:sum]}.to_h.each{|label, values|
40
+ printf "%-150s %s (%s)\n", "#{label} (total time):", values[:sum].round(5), "#{((values[:sum]/ total_time) * 100).round(1)}%"
41
+ printf "%-150s %s\n", "#{label} (number of calls):", values[:num]
42
+ printf "%-150s %s\n\n", "#{label} (average time):", (values[:sum]/values[:num]).round(5)
43
+ }
44
+ end
45
+ printf "\n######### (most time consuming method is at the bottom) #########"
46
+
47
+ reset!
48
+ end
49
+
50
+ def reset!
51
+ self.sum_hash = {}
52
+ end
53
+
54
+ def active?
55
+ active_labels.any?
56
+ end
57
+
58
+ private
59
+ attr_accessor :sum_hash, :options, :active_labels
60
+ def append_active_label(label)
61
+ active_labels << label
62
+ end
63
+
64
+ def remove_active_label(label)
65
+ self.active_labels = active_labels.select{|i| i != label}
66
+ end
67
+ end
@@ -0,0 +1,13 @@
1
+ module ObjectLabel
2
+ def label(method_name, notes = nil)
3
+ if notes
4
+ "#{self.class.name}##{method_name} (#{notes})"
5
+ else
6
+ "#{self.class.name}##{method_name}"
7
+ end
8
+ end
9
+ end
10
+
11
+ class Object
12
+ include ObjectLabel
13
+ end
@@ -0,0 +1,16 @@
1
+ module ClassProfiler
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ response = ::ClassProfiler::Benchmark.instance.start 'rack time' do
9
+ @status, @headers, @response = @app.call(env)
10
+ end
11
+
12
+ ::ClassProfiler::Benchmark.instance.report('rack time')
13
+ return response
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ClassProfiler
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_profiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Filippos Vasilakis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-05 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: Simple class performance profiler with some metaprogramming alcohol
42
+ email:
43
+ - vasilakisfil@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - class_profiler.gemspec
57
+ - lib/class_profiler.rb
58
+ - lib/class_profiler/benchmark.rb
59
+ - lib/class_profiler/object_label.rb
60
+ - lib/class_profiler/rack.rb
61
+ - lib/class_profiler/version.rb
62
+ homepage: https://github.com/vasilakisfil/class_profiler
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.6.12
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Simple class performance profiler with some metaprogramming alcohol
86
+ test_files: []