execution_time 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: 442112d9c9feb0bf2e01861f7ab6d3c0bd812133cf9523005fcd792c261558cc
4
+ data.tar.gz: c177f92ed4ca9683c6db8ece732622252d483f7162d9eb61f222cb436b353c68
5
+ SHA512:
6
+ metadata.gz: a4eddc87a2234c1943a29d5b2e5a1b9db919e36ee8a076953627f2cd9aaa3b85b1412489b58be074f73d62c58112377d11241c1a6c99d1650dc56f12d911e6f3
7
+ data.tar.gz: 7283093d13704a632d5f8c68f1aa83696cd118a2d8f1e737a2d4212a5b92c3d24b8540ce94e0a4e9cfefbd00803bebb249c1e2f98a9a51dccf08a383233107ff
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Igor Kasyanchuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # ExecutionTime
2
+
3
+ This gem can get you an execution time info similar to processed requests directly in rails console.
4
+
5
+ `[METRICS] Completed in 908.3ms | Allocations: 2894 | ActiveRecord: 0.9ms (queries: 13)`
6
+
7
+ ## Usage
8
+
9
+ Just add this gem to the Gemfile and start `rails c`. After this try to do something like `User.first`.
10
+
11
+ If you want to measure few lines of code just wrap it with `begin/end`.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'execution_time'
19
+ ```
20
+
21
+ ## First run
22
+
23
+ Sometime you can see that there are more SQL queries or allocated objects because Ruby just loading objects in memory or verifying connection to the DB.
24
+
25
+ ## Contributing
26
+
27
+ You are welcome to contribute.
28
+
29
+ ## License
30
+ 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,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ExecutionTime'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,14 @@
1
+ module ExecutionTime
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ console do
5
+ if const_defined?("Pry")
6
+ Pry.send :prepend, ExecutionTime::PryExt
7
+ end
8
+ if const_defined?("IRB::Context")
9
+ IRB::Context.send :prepend, ExecutionTime::IrbContextExt
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ExecutionTime
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,85 @@
1
+ require "execution_time/railtie"
2
+
3
+ module ExecutionTime
4
+ class AppMetrics
5
+ @@counter = 0
6
+
7
+ cattr_reader :counter
8
+
9
+ def self.reset
10
+ @@counter = 0
11
+ end
12
+
13
+ def call(event_name, started, finished, event_id, payload)
14
+ @@counter += 1
15
+ end
16
+ # def red; colorize(self, "\e[1m\e[31m"); end
17
+ # def green; colorize(self, "\e[1m\e[32m"); end
18
+ # def dark_green; colorize(self, "\e[32m"); end
19
+ # def yellow; colorize(self, "\e[1m\e[33m"); end
20
+ # def blue; colorize(self, "\e[1m\e[34m"); end
21
+ # def dark_blue; colorize(self, "\e[34m"); end
22
+ # def colorize(text, color_code) "#{color_code}#{text}\e[0m" end
23
+ def AppMetrics.with_color(text)
24
+ "\e[1m\e[32m[METRICS]\e[0m \e[32m#{text}\e[0m"
25
+ end
26
+ end
27
+
28
+ ActiveSupport::Notifications.subscribe(
29
+ "sql.active_record",
30
+ ExecutionTime::AppMetrics.new
31
+ )
32
+
33
+ class Measurer
34
+ def Measurer.watch
35
+ AppMetrics.reset
36
+
37
+ start = Time.now
38
+ before = GC.stat(:total_allocated_objects)
39
+ ActiveRecord::LogSubscriber.reset_runtime
40
+
41
+ result = yield
42
+
43
+ after = GC.stat(:total_allocated_objects)
44
+ duration = (Time.now - start)
45
+ db_after = ActiveRecord::LogSubscriber.reset_runtime
46
+
47
+ info = "Completed in #{(duration * 1000).round(1)}ms | Allocations: #{after - before}"
48
+
49
+ if AppMetrics.counter > 0
50
+ info << " | ActiveRecord: #{db_after.round(1)}ms"
51
+ info << " (queries: #{AppMetrics.counter})"
52
+ end
53
+
54
+ puts AppMetrics.with_color(info)
55
+ result
56
+ end
57
+ end
58
+
59
+ module IrbContextExt
60
+ def evaluate(*args)
61
+ Measurer.watch do
62
+ super(*args)
63
+ end
64
+ end
65
+ end
66
+
67
+ module BindingExt
68
+ def eval(*args)
69
+ if args == ['self'] || args == ['']
70
+ super(*args)
71
+ else
72
+ Measurer.watch do
73
+ super(*args)
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ module PryExt
80
+ def evaluate_ruby(code)
81
+ current_binding.class.send :prepend, BindingExt
82
+ super(code)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :execution_time do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: execution_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Measure execution time in Rails console
42
+ email:
43
+ - igorkasyanchuk@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/execution_time.rb
52
+ - lib/execution_time/railtie.rb
53
+ - lib/execution_time/version.rb
54
+ - lib/tasks/execution_time_tasks.rake
55
+ homepage: https://github.com/igorkasyanchuk
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.0.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Measure execution time in Rails console
78
+ test_files: []