profilizer 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
+ SHA256:
3
+ metadata.gz: bd6e4cb9fd428cdeb8fd286926b4755b2fe92963551f067dbeb39c4ea6039f34
4
+ data.tar.gz: 6cec08e96bc08e68ac506a8273abeb6a54dba4ebdbc13eb719b2cc992fa083ef
5
+ SHA512:
6
+ metadata.gz: e6d8f8283746df3b427d5c8dc8ecb54fa53ae3f341a4f1ee61b3a47379d0bfec1e685e15384e1dd9554933167a7852f47cefed873536c3bd9e3c11eae4e9b5e1
7
+ data.tar.gz: 24ea81e36e5d27ed89efc988e7f8dff35b2cd1342d83c60ea4750338c620665454902018bb71cba452710c79ed027fb2f5192907def70ea510db1ddbd0c5d258
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ require: rubocop-rspec
2
+
3
+ Layout/LineLength:
4
+ Max: 120
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Naming/MethodParameterName:
10
+ MinNameLength: 2
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - 'spec/**/*.rb'
15
+ - '*.gemspec'
16
+
17
+ Lint/RaiseException:
18
+ Enabled: true
19
+
20
+ Lint/StructNewOverride:
21
+ Enabled: true
22
+
23
+ Style/HashEachMethods:
24
+ Enabled: true
25
+
26
+ Style/HashTransformKeys:
27
+ Enabled: true
28
+
29
+ Style/HashTransformValues:
30
+ Enabled: true
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in profilizer.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+
10
+ group :development, :test do
11
+ gem 'pry', '~> 0.12.2', require: false
12
+ gem 'rubocop', require: false
13
+ gem 'rubocop-rspec', require: false
14
+ end
15
+
16
+ group :test do
17
+ gem 'rspec', '~> 3.0'
18
+ gem 'simplecov', require: false
19
+ gem 'simplecov-console', require: false
20
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ profilizer (0.1.0)
5
+ ruby2_keywords (~> 0.0.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ ast (2.4.0)
12
+ coderay (1.1.2)
13
+ diff-lcs (1.3)
14
+ docile (1.3.2)
15
+ jaro_winkler (1.5.4)
16
+ method_source (0.9.2)
17
+ parallel (1.19.1)
18
+ parser (2.7.0.5)
19
+ ast (~> 2.4.0)
20
+ pry (0.12.2)
21
+ coderay (~> 1.1.0)
22
+ method_source (~> 0.9.0)
23
+ rainbow (3.0.0)
24
+ rake (12.3.3)
25
+ rexml (3.2.4)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.1)
31
+ rspec-support (~> 3.9.1)
32
+ rspec-expectations (3.9.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.2)
39
+ rubocop (0.81.0)
40
+ jaro_winkler (~> 1.5.1)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.0.1)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ rexml
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-rspec (1.38.1)
48
+ rubocop (>= 0.68.1)
49
+ ruby-progressbar (1.10.1)
50
+ ruby2_keywords (0.0.2)
51
+ simplecov (0.18.5)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov-console (0.7.2)
55
+ ansi
56
+ simplecov
57
+ terminal-table
58
+ simplecov-html (0.12.2)
59
+ terminal-table (1.8.0)
60
+ unicode-display_width (~> 1.1, >= 1.1.1)
61
+ unicode-display_width (1.7.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ profilizer!
68
+ pry (~> 0.12.2)
69
+ rake (~> 12.0)
70
+ rspec (~> 3.0)
71
+ rubocop
72
+ rubocop-rspec
73
+ simplecov
74
+ simplecov-console
75
+
76
+ BUNDLED WITH
77
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dzmitry Kremez
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,125 @@
1
+ # Profilizer
2
+
3
+ Profilizer is a Ruby gem for easy profile of methods. It helps you to track how fast you methods are, how much memory the consume and how much objects were allocated.
4
+
5
+ The normal hand on profiling in Ruby doesn't require any gems and looks like this:
6
+
7
+ ```ruby
8
+ def profile
9
+ start = Time.now
10
+ yield if block_given?
11
+ end = Time.now
12
+ puts "Duration: #{end - start}"
13
+ end
14
+ ```
15
+
16
+ However, it is not handy in case you want more options of you just need to repeat this code many time in different application.
17
+
18
+ Here is what is gem propose instead:
19
+
20
+ ```ruby
21
+ class A
22
+ include Profilizer
23
+
24
+ profilize def foo
25
+ a = 1
26
+ b = 2
27
+ c = a + b
28
+ end
29
+ end
30
+
31
+ A.new.foo
32
+ ```
33
+
34
+ It will output in STDOUT something like:
35
+
36
+ ```
37
+ Objects Freed: 39
38
+ Time: 0.02 seconds
39
+ Memory usage: 2.05 MB
40
+ ```
41
+
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'profilizer'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ $ bundle install
54
+
55
+ Or install it yourself as:
56
+
57
+ $ gem install profilizer
58
+
59
+ ## Usage
60
+
61
+ ### Basic usage
62
+
63
+ ```ruby
64
+ class A
65
+ include Profilizer
66
+
67
+ profilize def foo
68
+ a = 1
69
+ b = 2
70
+ c = a + b
71
+ end
72
+ end
73
+
74
+ A.new.foo
75
+ ```
76
+
77
+ It prints in STDOUT something like:
78
+
79
+ ```
80
+ Objects Freed: 39
81
+ Time: 0.02 seconds
82
+ Memory usage: 2.05 MB
83
+ ```
84
+
85
+ ### Configuration
86
+
87
+ What if you only want to profile time or just used memory or objects allocations only.
88
+ Here is how you could config
89
+
90
+ ```ruby
91
+ class A
92
+ include Profilizer
93
+
94
+ def foo
95
+ a = 1
96
+ b = 2
97
+ c = a + b
98
+ end
99
+
100
+ profilize :foo, time: true, memory: false, gc: false
101
+ end
102
+
103
+ A.new.foo
104
+ ```
105
+
106
+ It prints:
107
+
108
+ ```
109
+ Time: 0.02 seconds
110
+ ```
111
+
112
+ ## Development
113
+
114
+ 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.
115
+
116
+ 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).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dkremez/profilizer.
121
+
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'profilizer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'benchmark'
4
+
5
+ module Profilizer
6
+ class Profiler
7
+ def profile_method(memory: true, time: true, gc: true)
8
+ profile_memory(!memory) do
9
+ profile_time(!time) do
10
+ profile_gc(!gc) do
11
+ yield
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def profile_memory(skip, &block)
18
+ return block.call if skip
19
+
20
+ memory_usage_before = `ps -o rss= -p #{Process.pid}`.to_i
21
+ result = block.call
22
+ memory_usage_after = `ps -o rss= -p #{Process.pid}`.to_i
23
+
24
+ used_memory = ((memory_usage_after - memory_usage_before) / 1024.0).round(2)
25
+ puts "Memory usage: #{used_memory} MB"
26
+ result
27
+ end
28
+
29
+ def profile_time(skip, &block)
30
+ return block.call if skip
31
+
32
+ result = nil
33
+
34
+ time_elapsed = Benchmark.realtime do
35
+ result = block.call
36
+ end
37
+
38
+ puts "Time: #{time_elapsed.round(2)} seconds"
39
+ result
40
+ end
41
+
42
+ def profile_gc(skip, &block)
43
+ return block.call if skip
44
+
45
+ GC.start
46
+ before = GC.stat(:total_freed_objects)
47
+ result = block.call
48
+ GC.start
49
+ after = GC.stat(:total_freed_objects)
50
+
51
+ puts "Objects Freed: #{after - before}"
52
+ result
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Profilizer
4
+ VERSION = '0.1.0'
5
+ end
data/lib/profilizer.rb ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require 'ruby2_keywords'
5
+
6
+ require 'profilizer/profiler'
7
+ require 'profilizer/version'
8
+
9
+ module Profilizer
10
+ class ArgumentError < StandardError; end
11
+
12
+ OUR_BLOCK = lambda do
13
+ extend(ClassMethods)
14
+ extend ModuleMethods if instance_of?(Module)
15
+ end
16
+
17
+ private_constant :OUR_BLOCK
18
+
19
+ module ModuleMethods
20
+ def included(base = nil, &block)
21
+ if base.nil? && block
22
+ super do
23
+ instance_exec(&block)
24
+ instance_exec(&OUR_BLOCK)
25
+ end
26
+ else
27
+ base.instance_exec(&OUR_BLOCK)
28
+ end
29
+ end
30
+ end
31
+
32
+ extend ModuleMethods
33
+
34
+ module ClassMethods
35
+ def profilize(method_name, time: true, gc: true, memory: true)
36
+ prepend_profilizer_module!
37
+ define_profilized_method!(method_name, time: time, gc: gc, memory: memory)
38
+ method_name
39
+ end
40
+
41
+ private
42
+
43
+ def prepend_profilizer_module!
44
+ return if defined?(@_prifilizer_module)
45
+
46
+ @_prifilizer_module = Module.new { extend ProfilizerModule }
47
+ prepend @_prifilizer_module
48
+ end
49
+
50
+ def define_profilized_method!(*args, **kwargs)
51
+ @_prifilizer_module.public_send __method__, self, *args, **kwargs
52
+ end
53
+
54
+ module ProfilizerModule
55
+ def define_profilized_method!(klass, method_name, time: nil, gc: nil, memory: nil)
56
+ original_visibility = method_visibility(klass, method_name)
57
+
58
+ define_method method_name do |*args, &block|
59
+ result = nil
60
+ Profiler.new.profile_method(time: time, gc: gc, memory: memory) do
61
+ result = super(*args, &block)
62
+ end
63
+ result
64
+ end
65
+
66
+ ruby2_keywords method_name
67
+
68
+ send original_visibility, method_name
69
+ end
70
+
71
+ private
72
+
73
+ def method_visibility(klass, method_name)
74
+ if klass.private_method_defined?(method_name)
75
+ :private
76
+ elsif klass.protected_method_defined?(method_name)
77
+ :protected
78
+ elsif klass.public_method_defined?(method_name)
79
+ :public
80
+ else
81
+ raise ArgumentError, "Method #{method_name} is not defined on #{klass}"
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ # private_constant ProfilizerModule
88
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/profilizer/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'profilizer'
7
+ spec.version = Profilizer::VERSION
8
+ spec.authors = ['Dzmitry Kremez']
9
+ spec.email = ['mr.dkremez@gmail.com']
10
+
11
+ spec.summary = 'Profile you methods easier'
12
+ spec.description = <<~DEC
13
+ Some times we need to test in development how long our methods execute,
14
+ how much memery code consumes, how many objects it allocates.
15
+ What if we don't want to touch the code a lot and just like kind of a decorator
16
+ that will log profile info into the console.
17
+ This gem helps to do it in one line of code.
18
+ DEC
19
+ spec.homepage = 'https://github.com/dkremez/profilizer'
20
+ spec.license = 'MIT'
21
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
22
+
23
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
24
+
25
+ spec.metadata['homepage_uri'] = spec.homepage
26
+ spec.metadata['source_code_uri'] = spec.homepage
27
+ spec.metadata['changelog_uri'] = spec.homepage
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = 'exe'
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ['lib']
37
+
38
+ spec.add_runtime_dependency 'ruby2_keywords', '~> 0.0.2'
39
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: profilizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dzmitry Kremez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby2_keywords
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
27
+ description: |
28
+ Some times we need to test in development how long our methods execute,
29
+ how much memery code consumes, how many objects it allocates.
30
+ What if we don't want to touch the code a lot and just like kind of a decorator
31
+ that will log profile info into the console.
32
+ This gem helps to do it in one line of code.
33
+ email:
34
+ - mr.dkremez@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - ".gitignore"
40
+ - ".rspec"
41
+ - ".rubocop.yml"
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - bin/console
48
+ - bin/setup
49
+ - lib/profilizer.rb
50
+ - lib/profilizer/profiler.rb
51
+ - lib/profilizer/version.rb
52
+ - profilizer.gemspec
53
+ homepage: https://github.com/dkremez/profilizer
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ allowed_push_host: https://rubygems.org
58
+ homepage_uri: https://github.com/dkremez/profilizer
59
+ source_code_uri: https://github.com/dkremez/profilizer
60
+ changelog_uri: https://github.com/dkremez/profilizer
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.3.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Profile you methods easier
80
+ test_files: []