bench_bloc 0.1.13 → 0.1.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d212bad6fb362933adb5a571872b9ff24dad79d6cd67492ad7cb839977d27a29
4
- data.tar.gz: 5a49a1b404cc05d5cdd9c23bae77434fffeda8c8032bcf9366e0f8a81aeca7a8
3
+ metadata.gz: 2970be47e52ee530bbb07311f9803822b131b622387a92ed7466c9aec3b0ba36
4
+ data.tar.gz: 9f08eee57d653e12b434cb4cd5e1898a46d528ac297ec030ddcc469e889b3d56
5
5
  SHA512:
6
- metadata.gz: 625f8f5856b8a8c0b122a8b54ef29bc0a512a62eae992722577df644b19ad94f74c8b39f21541d2ec300d8cc75c53d78b60374b74b54f8e74123c3a3006126e2
7
- data.tar.gz: 1c43e81eafae7e2ba50b462d023dc5c461a3f6cf892aaf106aa49f834a6c923d1b12b65aaad452dbd8c130ac767db843457bec964e67e723898a66c7a462ef58
6
+ metadata.gz: a89f3492876b64adca86fbb6e668a047f5e39d2441965edf5f6213061fe99608264aa750c775626bb94cea94d13dc4874216d6ceb89876da057fdc08127a7086
7
+ data.tar.gz: 3387d9b7090682d90dedf1607ac3320e3f1d46287a12acda57ee884cc15b1723897831968d6a84f27b59571755a5c0c6f6b1581b244cbedc4371ac61a61b4f3d
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bench_bloc (0.1.13)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.1)
21
+ rspec-support (~> 3.9.1)
22
+ rspec-expectations (3.9.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.2)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bench_bloc!
35
+ bundler (~> 2.0.2)
36
+ pry
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 2.0.2
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # BenchBloc
2
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/bench_bloc`. 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
3
+ BenchBloc is a benchmarking tool for Ruby that allows you to benchmark your code by using a simple hash syntax, which will generate rake tasks, which when run will format and log to a bench_bloc.log file. It currently allows you to bench via Ruby's built-in Benchmark API or through the [ruby-prof gem](https://github.com/ruby-prof/ruby-prof).
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,33 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ In a '/bench_bloc' folder at the root of your app, create any number of files that end in '\*.bloc.rb'.
24
+ In these files use the Ruby hash syntax to namespace how you want the rake tasks to be generated. Task namespaces can be nested.
25
+
26
+ To generate a task, a hash must contain at least the `prof` property, among others. The `prof` property must be a lamda, the block of which will be the code that you will benchmark.
27
+
28
+ ```
29
+ {
30
+ posts: {
31
+ save_a_post: {
32
+ prof: (obj) -> { sleep 3 }
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ Will generate the `rake bench_bloc:posts:save_a_post` rake task.
39
+ When this task is run the results will be formatted and saved to /log/bench_bloc.log
40
+
41
+ ```
42
+
43
+ ---
44
+ Test Description
45
+ Total Time: 3.0 seconds
46
+
47
+ Test Sleep 3 Seconds
48
+ 3.0 seconds
49
+ ```
26
50
 
27
51
  ## Development
28
52
 
@@ -32,7 +56,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
56
 
33
57
  ## Contributing
34
58
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bench_bloc. 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.
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jdpaterson/bench_bloc. 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.
36
60
 
37
61
  ## License
38
62
 
@@ -40,4 +64,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
64
 
41
65
  ## Code of Conduct
42
66
 
43
- Everyone interacting in the BenchBloc project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bench_bloc/blob/master/CODE_OF_CONDUCT.md).
67
+ Everyone interacting in the BenchBloc project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jdpaterson/bench_bloc/blob/master/CODE_OF_CONDUCT.md).
data/bench_bloc.gemspec CHANGED
@@ -35,7 +35,8 @@ Gem::Specification.new do |spec|
35
35
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
36
  spec.require_paths = ["lib"]
37
37
 
38
- spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "bundler", "~> 2.0.2"
39
39
  spec.add_development_dependency "rake", "~> 10.0"
40
40
  spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "pry"
41
42
  end
data/lib/bench_bloc.rb CHANGED
@@ -1,12 +1,13 @@
1
- require 'bench_bloc/version'
2
- require 'bench_bloc/helpers/benchmark_helpers'
3
- require 'bench_bloc/helpers/rake_helpers'
4
- require 'bench_bloc/helpers/ruby_prof_helpers'
1
+ require 'bench_bloc/bloc/bloc'
2
+ require 'bench_bloc/bloc/bloc_namespace'
3
+ require 'bench_bloc/bloc/bloc_task'
4
+ require 'bench_bloc/formatter/formatter'
5
+ require 'bench_bloc/formatter/benchmark'
6
+ require 'bench_bloc/logger/logger'
5
7
  require 'bench_bloc/railtie' if defined?(Rails)
8
+ require 'bench_bloc/version'
9
+
6
10
 
7
11
  module BenchBloc
8
12
  class Error < StandardError; end
9
- include BenchBloc::BenchmarkHelpers
10
- include BenchBloc::RakeHelpers
11
- include BenchBloc::RubyProfHelpers
12
13
  end
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ module BenchBloc
3
+ # Responsible for generating rake tasks from a hash and/or creating child Blocs
4
+ class Bloc
5
+ include Rake::DSL
6
+ attr_accessor :bloc_hash, :bloc_namespaces
7
+ def initialize bloc_hash
8
+ @bloc_hash, @bloc_namespaces = bloc_hash, []
9
+ end
10
+
11
+ def generate_bloc
12
+ bloc_namespaces.push(
13
+ Bloc::Namespace.new(
14
+ :bench_bloc,
15
+ bloc_hash
16
+ )
17
+ )
18
+ end
19
+
20
+ def rake_bloc
21
+ bloc_namespaces.each do |bn|
22
+ bn.rake_namespace
23
+ end
24
+ end
25
+
26
+ def [](namespace_key)
27
+ bench_bloc_namespace
28
+ .bloc_namespaces
29
+ .find { |bn| namespace_key == bn.namespace_key }
30
+ end
31
+
32
+ private
33
+ def is_task? obj
34
+ obj.keys.any?(:to_profile)
35
+ end
36
+
37
+ def bench_bloc_namespace
38
+ bloc_namespaces[0]
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require 'rake'
2
+ module BenchBloc
3
+ # Responsible for generating rake tasks from a hash and/or creating child Blocs
4
+ class Bloc
5
+ include Rake::DSL
6
+ attr_accessor :bloc_hash, :bloc_namespaces
7
+ def initialize bloc_hash
8
+ @bloc_hash, @bloc_namespaces = bloc_hash, []
9
+ end
10
+
11
+ def generate_bloc
12
+ bloc_namespaces.push(
13
+ Bloc::Namespace.new(
14
+ :bench_bloc,
15
+ bloc_hash
16
+ )
17
+ )
18
+ end
19
+
20
+ def rake_bloc
21
+ bloc_namespaces.each do |bn|
22
+ bn.rake_namespace
23
+ end
24
+ end
25
+
26
+ def [](namespace_key)
27
+ bench_bloc_namespace
28
+ .bloc_namespaces
29
+ .find { |bn| namespace_key == bn.namespace_key }
30
+ end
31
+
32
+ private
33
+ def is_task? obj
34
+ obj.keys.any?(:profile)
35
+ end
36
+
37
+ def bench_bloc_namespace
38
+ bloc_namespaces[0]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ module BenchBloc
2
+ class Bloc::Namespace < BenchBloc::Bloc
3
+ attr_reader :bloc_namespaces, :bloc_tasks, :namespace_key
4
+ def initialize namespace_key, bloc_hash
5
+ super(bloc_hash)
6
+ @namespace_key = namespace_key
7
+ @bloc_namespaces, @bloc_tasks = [], []
8
+ put_namespace
9
+ end
10
+
11
+ def put_namespace
12
+ bloc_hash.keys.each do |bh_key|
13
+ if is_task? bloc_hash[bh_key]
14
+ bloc_tasks.push(
15
+ BenchBloc::Bloc::Task.new(
16
+ bh_key,
17
+ bloc_hash[bh_key]
18
+ )
19
+ )
20
+ else
21
+ bloc_namespaces.push(
22
+ Bloc::Namespace.new(
23
+ bh_key,
24
+ bloc_hash[bh_key]
25
+ )
26
+ )
27
+ end
28
+ end
29
+ end
30
+
31
+ def rake_namespace
32
+ namespace namespace_key do
33
+ bloc_tasks.each do |bt|
34
+ bt.rake_task
35
+ end
36
+ bloc_namespaces.each do |bn|
37
+ bn.rake_namespace
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,47 @@
1
+ require 'bench_bloc'
2
+ module BenchBloc
3
+ class Bloc::Task < BenchBloc::Bloc
4
+ attr_reader :description,
5
+ :label,
6
+ :namespace,
7
+ :profile,
8
+ :title,
9
+ :to_profile
10
+
11
+ def initialize namespace, bloc_task
12
+ super(bloc_task)
13
+ @namespace = namespace
14
+ parse_bloc_task bloc_task
15
+ end
16
+
17
+ # TODO: Put into a TaskRunner class
18
+ def run_task
19
+ Benchmark.bm do |x|
20
+ [to_profile.call].flatten.each do |otp|
21
+ x.report(label.call(otp)) do
22
+ profile.call(otp)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def rake_task
29
+ desc description
30
+ task namespace => :environment do
31
+ BenchBloc::Logger.new(run_task, description).log_results
32
+ # run ruby-prof
33
+ # format_ruby_prof(run_ruby_prof(new_task[:prof], tp)) if @options[:ruby_prof] == true
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def parse_bloc_task bloc_task
40
+ @description = bloc_task[:description]
41
+ @label = bloc_task[:label]
42
+ @profile = bloc_task[:profile]
43
+ @title = bloc_task[:title]
44
+ @to_profile = bloc_task[:to_profile]
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ module BenchBloc
2
+ class Bloc::Namespace < BenchBloc::Bloc
3
+ attr_reader :bloc_namespaces, :bloc_tasks, :namespace_key
4
+ def initialize namespace_key, bloc_hash
5
+ super(bloc_hash)
6
+ @namespace_key = namespace_key
7
+ @bloc_namespaces, @bloc_tasks = [], []
8
+ put_namespace
9
+ end
10
+
11
+ def put_namespace
12
+ bloc_hash.keys.each do |bh_key|
13
+ if is_task? bloc_hash[bh_key]
14
+ bloc_tasks.push(
15
+ BenchBloc::Bloc::Task.new(
16
+ bh_key,
17
+ bloc_hash[bh_key]
18
+ )
19
+ )
20
+ else
21
+ bloc_namespaces.push(
22
+ Bloc::Namespace.new(
23
+ bh_key,
24
+ bloc_hash[bh_key]
25
+ )
26
+ )
27
+ end
28
+ end
29
+ end
30
+
31
+ def rake_namespace
32
+ namespace namespace_key do
33
+ bloc_tasks.each do |bt|
34
+ bt.rake_task
35
+ end
36
+ bloc_namespaces.each do |bn|
37
+ bn.rake_namespace
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ module BenchBloc
2
+ class Bloc::Task < BenchBloc::Bloc
3
+ attr_reader :description,
4
+ :label,
5
+ :namespace,
6
+ :profile,
7
+ :title,
8
+ :to_profile
9
+
10
+ def initialize namespace, bloc_task
11
+ super(bloc_task)
12
+ @namespace = namespace
13
+ parse_bloc_task bloc_task
14
+ end
15
+
16
+ def rake_task
17
+ desc description
18
+ task namespace => :environment do
19
+ to_profs = [to_profile.call].flatten
20
+ bm_results = bm_run_results self, to_profs
21
+ bm_log_results bm_results, description
22
+ # run ruby-prof
23
+ # format_ruby_prof(run_ruby_prof(new_task[:prof], tp)) if @options[:ruby_prof] == true
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def parse_bloc_task bloc_task
30
+ @description = bloc_task[:description]
31
+ @label = bloc_task[:label]
32
+ @profile = bloc_task[:profile]
33
+ @title = bloc_task[:title]
34
+ @to_profile = bloc_task[:to_profile]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ require 'bench_bloc'
2
+ module BenchBloc
3
+ class Formatter::Benchmark < BenchBloc::Formatter
4
+
5
+ def format_results
6
+ formatted_results = results.map { |res| format_result(res) }
7
+ header = "\n---\n\t#{title}\n"
8
+ summary = "\tTotal Time: #{summarize_real_time(results).round(2)} seconds\n\n"
9
+ final_results = header + summary + formatted_results.join("\n")
10
+ end
11
+
12
+ def summarize_real_time results
13
+ results.inject(0) do |agg, res|
14
+ agg + res.real
15
+ end
16
+ end
17
+
18
+ def format_result result
19
+ "\t\t#{result.label}\n\t\t\t#{result.real.round(2)} seconds"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ module BenchBloc
2
+ class Formatter
3
+ attr_reader :results, :title
4
+ def initialize results, title
5
+ @results, @title = results, title
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ require 'bench_bloc'
2
+ module BenchBloc
3
+ class Logger
4
+ attr_reader :results, :title
5
+ def initialize results, title
6
+ @results, @title = results, title
7
+ end
8
+
9
+ # TODO: Split into a BenchmarkLogger class
10
+ def log_results
11
+ results.sort! { |a, b| b.real <=> a.real }
12
+ formatted_results =
13
+ BenchBloc::Formatter::Benchmark.new(results, title).format_results
14
+ write_to_log formatted_results
15
+ end
16
+
17
+ def write_to_log results
18
+ if defined?(Rails)
19
+ # Rails Logger not working, naming conflict I think,
20
+ # or it needs to be required
21
+ # log = Rails::Logger.new("#{Rails.root}/log/benchmarks.log")
22
+ # log.info(results)
23
+ f = File.new("#{Rails.root}/log/benchmarks.log", "w")
24
+ f.puts(results)
25
+ f.close
26
+ else
27
+ f = File.new("benchmarks.log", "w")
28
+ f.puts(results)
29
+ f.close
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ module BenchBloc
2
+ class Raker
3
+ def bench_tasks
4
+ Rake.application.tasks.select do |task|
5
+ task.name.starts_with?("bench_bloc") &&
6
+ !task.name.ends_with?("_util") &&
7
+ task.name != "bench_bloc:all"
8
+ end
9
+ end
10
+
11
+ # def option_parser
12
+ # OptionParser.new do |opts|
13
+ # opts.banner = "Usage: rake bench_bloc:* [options]"
14
+ # opts.on("-r", "--ruby-prof", "Print a RubyProf report") do |rp|
15
+ # @options[:ruby_prof] = rp
16
+ # end
17
+ # end
18
+ # end
19
+
20
+ # def put_options_parser_task
21
+ # desc "Options parser for bench tasks"
22
+ # task parse_options_util: :environment do
23
+ # @options = {}
24
+ # option_parser.parse!
25
+ # option_parser.parse!
26
+ # end
27
+ # end
28
+
29
+ def put_clear_tests_task
30
+ desc "Clear Tests"
31
+ task clear_tests_util: :environment do
32
+ at_exit do
33
+ test_ts = Timesheet.where("general_remarks LIKE '%BENCHTEST%'")
34
+ puts "Clearing #{test_ts.count} test timesheets"
35
+ test_ts.destroy_all
36
+ end
37
+ end
38
+ end
39
+
40
+ def put_all_task
41
+ desc "Run all benchmarks"
42
+ task all: :environment do
43
+ bench_tasks.each(&:execute)
44
+ Rake::Task["bench_bloc:clear_tests_util"].invoke
45
+ end
46
+ end
47
+
48
+ def add_bench_hooks
49
+ bench_tasks.each do |task|
50
+ Rake::Task[task.name]
51
+ .enhance(['bench_bloc:parse_options_util', 'bench_bloc:clear_tests_util'])
52
+ end
53
+ end
54
+ end
55
+ end
@@ -2,14 +2,16 @@ require 'rake'
2
2
  require 'bench_bloc'
3
3
 
4
4
  # TODO: Look recursively in folders using 'bench_bloc/**/*.bloc.rb
5
- BLOC_FILES=FileList["bench_bloc/*.bloc.rb"]
5
+ BLOC_FILES = FileList["bench_bloc/*.bloc.rb"]
6
6
 
7
7
  include BenchBloc
8
8
 
9
9
  BLOC_FILES.each do |f|
10
- bloc = eval File.read(f)
11
- put_bloc_namespaces bloc
10
+ bench_bloc_hash = eval File.read(f)
11
+ bench_bloc = BenchBloc::Bloc.new(bench_bloc_hash)
12
+ bench_bloc.generate_bloc
13
+ bench_bloc.rake_bloc
12
14
  end
13
15
 
14
- add_bench_hooks
16
+ # add_bench_hooks
15
17
 
@@ -1,3 +1,3 @@
1
1
  module BenchBloc
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bench_bloc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-14 00:00:00.000000000 Z
11
+ date: 2020-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.0.2
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: 2.0.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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'
55
69
  description: Use a hash syntax to automatically generate rake tasks which run benchmarks
56
70
  on given blocks of code
57
71
  email:
@@ -65,6 +79,7 @@ files:
65
79
  - ".travis.yml"
66
80
  - CODE_OF_CONDUCT.md
67
81
  - Gemfile
82
+ - Gemfile.lock
68
83
  - LICENSE.txt
69
84
  - README.md
70
85
  - Rakefile
@@ -72,10 +87,18 @@ files:
72
87
  - bin/console
73
88
  - bin/setup
74
89
  - lib/bench_bloc.rb
75
- - lib/bench_bloc/helpers/benchmark_helpers.rb
76
- - lib/bench_bloc/helpers/rake_helpers.rb
90
+ - lib/bench_bloc/bloc.rb
91
+ - lib/bench_bloc/bloc/bloc.rb
92
+ - lib/bench_bloc/bloc/bloc_namespace.rb
93
+ - lib/bench_bloc/bloc/bloc_task.rb
94
+ - lib/bench_bloc/bloc_namespace.rb
95
+ - lib/bench_bloc/bloc_task.rb
96
+ - lib/bench_bloc/formatter/benchmark.rb
97
+ - lib/bench_bloc/formatter/formatter.rb
77
98
  - lib/bench_bloc/helpers/ruby_prof_helpers.rb
99
+ - lib/bench_bloc/logger/logger.rb
78
100
  - lib/bench_bloc/railtie.rb
101
+ - lib/bench_bloc/raker/raker.rb
79
102
  - lib/bench_bloc/tasks/gen_bench_bloc.rake
80
103
  - lib/bench_bloc/version.rb
81
104
  homepage: https://github.com/jdpaterson/bench_bloc
@@ -97,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
120
  - !ruby/object:Gem::Version
98
121
  version: '0'
99
122
  requirements: []
100
- rubyforge_project:
101
- rubygems_version: 2.7.6
123
+ rubygems_version: 3.0.3
102
124
  signing_key:
103
125
  specification_version: 4
104
126
  summary: Easily run benchmarks on Ruby methods
@@ -1,41 +0,0 @@
1
- module BenchBloc::BenchmarkHelpers
2
- def bm_log_results results, title
3
- results.sort! { |a,b| b.real <=> a.real }
4
- formatted_results = results.map { |res| bm_format_result(res) }
5
- header = "\n---\n\t#{title}\n"
6
- summary = "\tTotal Time: #{bm_summarize_real_time(results).round(2)} seconds\n\n"
7
- final_results = header + summary + formatted_results.join("\n")
8
- write_to_log final_results
9
- end
10
-
11
- def write_to_log results
12
- if defined?(Rails)
13
- log = Logger.new("#{Rails.root}/log/benchmarks.log")
14
- log.info(results)
15
- else
16
- f = File.new("benchmarks.log", "w")
17
- f.puts(results)
18
- f.close
19
- end
20
- end
21
-
22
- def bm_summarize_real_time results
23
- results.inject(0) do |agg, res|
24
- agg + res.real
25
- end
26
- end
27
-
28
- def bm_format_result result
29
- "\t\t#{result.label}\n\t\t\t#{result.real.round(2)} seconds"
30
- end
31
-
32
- def bm_run_results new_task, to_profs
33
- Benchmark.bm do |x|
34
- to_profs.each do |tp|
35
- x.report(new_task[:label].call(tp)) do
36
- new_task[:prof].call(tp)
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,94 +0,0 @@
1
- require 'optparse'
2
- require 'rake'
3
- module BenchBloc::RakeHelpers
4
- def bench_tasks
5
- Rake.application.tasks.select do |task|
6
- task.name.starts_with?("bench_bloc") &&
7
- !task.name.ends_with?("_util") &&
8
- task.name != "bench_bloc:all"
9
- end
10
- end
11
-
12
- def option_parser
13
- OptionParser.new do |opts|
14
- opts.banner = "Usage: rake bench_bloc:* [options]"
15
- opts.on("-r", "--ruby-prof", "Print a RubyProf report") do |rp|
16
- @options[:ruby_prof] = rp
17
- end
18
- end
19
- end
20
-
21
- def put_namespace key, namespace
22
- namespace key do
23
- namespace.keys.each do |ns_key|
24
- if is_task? namespace[ns_key]
25
- put_task ns_key, namespace[ns_key]
26
- else
27
- put_namespace ns_key, namespace[ns_key]
28
- end
29
- end
30
- end
31
- end
32
-
33
- # TODO: Add a ruby-prof method here if argument is passed
34
- def put_task key, new_task
35
- desc new_task[:desc]
36
- task key => :environment do
37
- to_profs = [new_task[:to_prof].call].flatten
38
- bm_results = bm_run_results new_task, to_profs
39
- bm_log_results bm_results, new_task[:desc]
40
- # run ruby-prof
41
- # format_ruby_prof(run_ruby_prof(new_task[:prof], tp)) if @options[:ruby_prof] == true
42
- end
43
- end
44
-
45
- def is_task? obj
46
- obj.keys.any?(:to_prof)
47
- end
48
-
49
- def put_options_parser_task
50
- desc "Options parser for bench tasks"
51
- task parse_options_util: :environment do
52
- @options = {}
53
- option_parser.parse!
54
- option_parser.parse!
55
- end
56
- end
57
-
58
- def put_clear_tests_task
59
- desc "Clear Tests"
60
- task clear_tests_util: :environment do
61
- at_exit do
62
- test_ts = Timesheet.where("general_remarks LIKE '%BENCHTEST%'")
63
- puts "Clearing #{test_ts.count} test timesheets"
64
- test_ts.destroy_all
65
- end
66
- end
67
- end
68
-
69
- def put_all_task
70
- desc "Run all benchmarks"
71
- task all: :environment do
72
- bench_tasks.each(&:execute)
73
- Rake::Task["bench_bloc:clear_tests_util"].invoke
74
- end
75
- end
76
-
77
- def add_bench_hooks
78
- bench_tasks.each do |task|
79
- Rake::Task[task.name]
80
- .enhance(['bench_bloc:parse_options_util', 'bench_bloc:clear_tests_util'])
81
- end
82
- end
83
-
84
- def put_bloc_namespaces bloc
85
- bloc.keys.each do |key|
86
- namespace :bench_bloc do
87
- put_namespace key, bloc[key]
88
- put_options_parser_task
89
- put_clear_tests_task
90
- put_all_task
91
- end
92
- end
93
- end
94
- end