koine-profiler 0.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6cf14a8e986ee18a7bf62d8789fb178c6c79b506
4
- data.tar.gz: 63326ff4d1822874a9c5ebd7053016fc5e3d6977
2
+ SHA256:
3
+ metadata.gz: 2603470fb2462b192b7290f6930c6c3aa4a71b94a198eb01ea34a4d0e1bcd90e
4
+ data.tar.gz: e9d010acbfd05cfeb38dc721c87daf27517e7d049813a7a50ca210145c43810b
5
5
  SHA512:
6
- metadata.gz: 922a971e1f4457345eefdec93df54a38a7ba1e1b5c1cbd6cf84a53f06db5da73254886f0499104f13846099d15f93ffcfa482ac9edab23e481dd147ae0a6074c
7
- data.tar.gz: 43842ed68421c9196aef2def2426f7ef77e3ddd2a1e66b19bb4e06074f674ced540564483230c0bbb2f04e7c2f5fe8d2773d8e109b20d24c8829756869f519a0
6
+ metadata.gz: 30d0fc8cbb86571c2e7142a4af368c64ac715998f8522db06624548630cddb67d9f96cb7b0482e6f3e36cd9208054c39e578554ee6f48d9a25e1236107a2c8a2
7
+ data.tar.gz: 8e41780379d1db9d9c44ce5edc46c032ee00a9cabe43a189bdd1e04adf1d7e949260bbab2c2fc6601bff927b26fb0d469b97481e30720ee0045d38b1a0ad45c7
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --format documentation
2
1
  --color
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 Marcelo Jacobus
3
+ Copyright (c) 2017-2020 Marcelo Jacobus
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -5,8 +5,6 @@ Simple profiler
5
5
  [![Build Status](https://travis-ci.org/mjacobus/koine-profiler.svg?branch=master)](https://travis-ci.org/mjacobus/koine-profiler)
6
6
  [![Code Coverage](https://scrutinizer-ci.com/g/mjacobus/koine-profiler/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mjacobus/koine-profiler/?branch=master)
7
7
  [![Code Climate](https://codeclimate.com/github/mjacobus/koine-profiler/badges/gpa.svg)](https://codeclimate.com/github/mjacobus/koine-profiler)
8
- [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mjacobus/koine-profiler/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mjacobus/koine-profiler/?branch=master)
9
- [![Dependency Status](https://gemnasium.com/mjacobus/koine-profiler.svg)](https://gemnasium.com/mjacobus/koine-profiler)
10
8
  [![Gem Version](https://badge.fury.io/rb/koine-profiler.svg)](https://badge.fury.io/rb/koine-profiler)
11
9
 
12
10
  ## Installation
@@ -28,6 +26,8 @@ Or install it yourself as:
28
26
  ## Usage
29
27
 
30
28
  ```ruby
29
+ profiler = Koine::Profiler.new
30
+
31
31
  profiler.profile('some potentially slow task') do
32
32
  potentially_slow()
33
33
  end
@@ -44,12 +44,16 @@ require 'koine/profiler/reporters/cli'
44
44
 
45
45
  reporter = Koine::Profiler::Reporters::Cli.new
46
46
 
47
- # all
48
- reporter.report(entries)
47
+ reporter.report(reporter.entries)
48
+ ```
49
+
50
+ Output example:
49
51
 
50
- # 5 slowest
51
- five_slowest = profile.entries.slowest.limit(5)
52
- reporter.report(five_slowest)
52
+ ```text
53
+ +----------------------------+-------------------+--------------+---------+
54
+ | Entry | Elapsed Time | Memory | # hits |
55
+ | some potentially slow task | 48.59363997100241 | 7.5625 | 1000000 |
56
+ +----------------------------+-------------------+--------------+---------+
53
57
  ```
54
58
 
55
59
  ## Development
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "koine/profiler"
3
+ require 'bundler/setup'
4
+ require 'koine/profiler'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "koine/profiler"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
@@ -0,0 +1,40 @@
1
+ require 'bundler/setup'
2
+ require 'koine/profiler'
3
+ require 'koine/profiler/reporters/cli'
4
+
5
+ MAX = 1_000_000
6
+
7
+ reporter = Koine::Profiler::Reporters::Cli.new
8
+ profiler = Koine::Profiler.instance
9
+
10
+ profiler.profile('arrays integers') do
11
+ data = []
12
+ 1.upto(MAX).each do |num|
13
+ profiler.profile('arrays integers item') do
14
+ data << num
15
+ end
16
+ end
17
+ data.length
18
+ end
19
+
20
+ profiler.profile('arrays hashes') do
21
+ data = []
22
+ 1.upto(MAX).each do |num|
23
+ profiler.profile('arrays hashes item') do
24
+ data << { num: num }
25
+ end
26
+ end
27
+ data.length
28
+ end
29
+
30
+ reporter.report(profiler.entries)
31
+
32
+ # Output example:
33
+ #
34
+ # +----------------------+-------------------+--------------+---------+
35
+ # | Entry | Elapsed Time | Memory | # hits |
36
+ # | arrays integers item | 48.59363997100241 | 7.5625 | 1000000 |
37
+ # | arrays integers | 50.281231385 | 8.14453125 | 1 |
38
+ # | arrays hashes item | 64.81660490299825 | 152.8828125 | 1000000 |
39
+ # | arrays hashes | 66.691896445 | 264.04296875 | 1 |
40
+ # +----------------------+-------------------+--------------+---------+
@@ -4,36 +4,38 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'koine/profiler/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "koine-profiler"
7
+ spec.name = 'koine-profiler'
8
8
  spec.version = Koine::Profiler::VERSION
9
- spec.authors = ["Marcelo Jacobus"]
10
- spec.email = ["marcelo.jacobus@gmail.com"]
9
+ spec.authors = ['Marcelo Jacobus']
10
+ spec.email = ['marcelo.jacobus@gmail.com']
11
11
 
12
- spec.summary = %q{A simple profiler}
13
- spec.description = %q{A simple profiler for profiling tests, for instance}
14
- spec.homepage = "https://github.com/mjacobus/koine-profiler"
15
- spec.license = "MIT"
12
+ spec.summary = 'A simple profiler'
13
+ spec.description = 'A simple profiler for profiling tests, for instance'
14
+ spec.homepage = 'https://github.com/mjacobus/koine-profiler'
15
+ spec.license = 'MIT'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
19
19
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against " \
23
- "public gem pushes."
22
+ fail 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
24
  end
25
25
 
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
27
  f.match(%r{^(test|spec|features)/})
28
28
  end
29
- spec.bindir = "exe"
29
+ spec.bindir = 'exe'
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
31
+ spec.require_paths = ['lib']
32
32
 
33
33
  spec.add_dependency 'terminal-table'
34
- spec.add_development_dependency "simplecov"
35
- spec.add_development_dependency "coveralls"
36
- spec.add_development_dependency "bundler", "~> 1.14"
37
- spec.add_development_dependency "rake", "~> 10.0"
38
- spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_dependency 'get_process_mem'
35
+ spec.add_development_dependency 'simplecov'
36
+ spec.add_development_dependency 'object_comparator'
37
+ spec.add_development_dependency 'coveralls'
38
+ spec.add_development_dependency 'bundler', '~> 2.1.4'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
41
  end
@@ -1,24 +1,41 @@
1
- require "koine/profiler/version"
2
- require "koine/profiler/entries"
3
- require "koine/profiler/entry_group"
4
- require "koine/profiler/entry"
1
+ require 'koine/profiler/version'
2
+ require 'koine/profiler/entry'
3
+ require 'get_process_mem'
5
4
 
6
5
  module Koine
7
6
  class Profiler
8
- attr_reader :entries
9
-
10
7
  def initialize
11
- @entries = Entries.new
8
+ @entries = {}
12
9
  end
13
10
 
14
11
  def profile(name)
15
12
  value = nil
16
13
  start_time = Time.now.utc
14
+ start_memory = GetProcessMem.new.mb
17
15
  value = yield if block_given?
16
+ finish_memory = GetProcessMem.new.mb
18
17
  finish_time = Time.now.utc
19
-
20
- entries.append(name, finish_time - start_time)
18
+ add_entry(
19
+ name,
20
+ time: finish_time - start_time,
21
+ memory: finish_memory - start_memory
22
+ )
21
23
  value
22
24
  end
25
+
26
+ def entries
27
+ @entries.values
28
+ end
29
+
30
+ def self.instance
31
+ @instance ||= new
32
+ end
33
+
34
+ private
35
+
36
+ def add_entry(name, time:, memory:)
37
+ entry = @entries[name] ||= Entry.new(name)
38
+ entry.increment(elapsed_time: time, memory_used: memory)
39
+ end
23
40
  end
24
41
  end
@@ -1,18 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Koine
2
4
  class Profiler
3
5
  class Entry
4
- attr_reader :name, :elapsed_time
5
-
6
- def initialize(name, elapsed_time)
7
- @name, @elapsed_time = name.to_s, elapsed_time
8
- end
6
+ attr_reader :name
7
+ attr_reader :elapsed_time
8
+ attr_reader :memory_used
9
+ attr_reader :hits
9
10
 
10
- def ==(other)
11
- other.name == name && other.elapsed_time == elapsed_time
11
+ def initialize(name)
12
+ @name = name
13
+ @elapsed_time = 0
14
+ @memory_used = 0
15
+ @hits = 0
12
16
  end
13
17
 
14
- def <=>(other)
15
- elapsed_time <=> other.elapsed_time
18
+ def increment(elapsed_time:, memory_used:)
19
+ @hits += 1
20
+ @elapsed_time += elapsed_time
21
+ @memory_used += memory_used
16
22
  end
17
23
  end
18
24
  end
@@ -10,10 +10,10 @@ module Koine
10
10
  end
11
11
 
12
12
  def report(entries)
13
- @table << ['Entry', 'Elapsed Time', '# of calls']
13
+ @table << ['Entry', 'Elapsed Time', 'Memory', '# hits']
14
14
 
15
15
  entries.each do |entry|
16
- @table << [entry.name, entry.elapsed_time, entry.entries.size]
16
+ @table << [entry.name, entry.elapsed_time, entry.memory_used, entry.hits]
17
17
  end
18
18
 
19
19
  @output << @table
@@ -1,5 +1,5 @@
1
1
  module Koine
2
2
  class Profiler
3
- VERSION = "0.1.0"
3
+ VERSION = '2.0.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koine-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-23 00:00:00.000000000 Z
11
+ date: 2020-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: terminal-table
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: get_process_mem
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: simplecov
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: object_comparator
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: coveralls
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +86,14 @@ dependencies:
58
86
  requirements:
59
87
  - - "~>"
60
88
  - !ruby/object:Gem::Version
61
- version: '1.14'
89
+ version: 2.1.4
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
- version: '1.14'
96
+ version: 2.1.4
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: rake
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -111,11 +139,10 @@ files:
111
139
  - Rakefile
112
140
  - bin/console
113
141
  - bin/setup
142
+ - examples/profiling_example.rb
114
143
  - koine-profiler.gemspec
115
144
  - lib/koine/profiler.rb
116
- - lib/koine/profiler/entries.rb
117
145
  - lib/koine/profiler/entry.rb
118
- - lib/koine/profiler/entry_group.rb
119
146
  - lib/koine/profiler/reporters/cli.rb
120
147
  - lib/koine/profiler/version.rb
121
148
  homepage: https://github.com/mjacobus/koine-profiler
@@ -138,8 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
165
  - !ruby/object:Gem::Version
139
166
  version: '0'
140
167
  requirements: []
141
- rubyforge_project:
142
- rubygems_version: 2.5.1
168
+ rubygems_version: 3.0.3
143
169
  signing_key:
144
170
  specification_version: 4
145
171
  summary: A simple profiler
@@ -1,79 +0,0 @@
1
- require 'forwardable'
2
-
3
- module Koine
4
- class Profiler
5
- class Entries
6
- extend Forwardable
7
-
8
- def_delegators :storage, :size
9
- def_delegators :to_a, :each
10
-
11
- def initialize(groups = [])
12
- @storage = Hash.new
13
- groups.each { |group| append_group(group) }
14
- end
15
-
16
- def append(name, time)
17
- group = find_by_name(name) || create_group(name)
18
- append_to_group(group, name, time)
19
- end
20
-
21
- def find_by_name(name)
22
- storage[name.to_s]
23
- end
24
-
25
- def elapsed_time
26
- storage.inject(0) do |total, (_key, group)|
27
- total + group.elapsed_time
28
- end
29
- end
30
-
31
- def sort_by(&_block)
32
- sorted = storage.sort_by { |item| yield(item[1]) }.map {|_key, group| group }
33
- create(sorted)
34
- end
35
-
36
- def reverse
37
- create(storage.to_a.reverse.map { |_key, group| group })
38
- end
39
-
40
- def slowest
41
- sort_by(&:elapsed_time).reverse
42
- end
43
-
44
- def to_a
45
- storage.values
46
- end
47
-
48
- def limit(number)
49
- create(to_a.slice(0, number.to_i))
50
- end
51
-
52
- def ==(other)
53
- storage.to_a == other.storage.to_a
54
- end
55
-
56
- private
57
-
58
- def create(groups = [])
59
- self.class.new(groups)
60
- end
61
-
62
- def create_group(name)
63
- append_group(EntryGroup.new(name))
64
- end
65
-
66
- def append_group(group)
67
- storage[group.name] = group
68
- end
69
-
70
- def append_to_group(group, name, time)
71
- group.append(Entry.new(name, time))
72
- end
73
-
74
- protected
75
-
76
- attr_reader :storage
77
- end
78
- end
79
- end
@@ -1,32 +0,0 @@
1
- module Koine
2
- class Profiler
3
- class EntryGroup
4
- InvalidEntryError = Class.new(StandardError)
5
-
6
- attr_reader :name, :entries
7
-
8
- def initialize(name)
9
- @name = name.to_s
10
- @entries = []
11
- end
12
-
13
- def append(entry)
14
- raise InvalidEntryError.new(entry.name) unless entry.name == name
15
-
16
- entries << entry
17
- end
18
-
19
- def ==(other)
20
- entries == other.entries
21
- end
22
-
23
- def <=>(other)
24
- elapsed_time <=> other.elapsed_time
25
- end
26
-
27
- def elapsed_time
28
- entries.inject(0) { |total, entry| entry.elapsed_time + total }
29
- end
30
- end
31
- end
32
- end