bench_bloc 0.1.8 → 0.1.9

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: 520cffeffd2d5dddf61b90a031c5b30989064fff2db5a149b6fa7bf01c3bd09e
4
- data.tar.gz: 75f44b181cbe5ffb56d86f6348a888e074dd2ee6772dfba94eb25e2241ddf611
3
+ metadata.gz: 2d58e13de32553297da5456ce97444c29c58b2771a0c4080714c6c08557cb231
4
+ data.tar.gz: fab6d9c3adda345068a3527d01688671459a5f4535e7b5d21ca763a85aa7d479
5
5
  SHA512:
6
- metadata.gz: ac717d9b240d34388bcbc88d7b0109ef079b720cd59633de6bd0e17d6608b10c10ca92de980e21110ba0e12fc590f382f638bc3b782aa903654fc8e893846507
7
- data.tar.gz: 7ad41b93f60a0bfec04f10f98f356ab721950bca8e3adc2b3fd5f8daeafae3e01303b176fd01cccbdcdaede679ac52976398d4d1235fd3251356e72fbb0f8752
6
+ metadata.gz: e2f60f261281bf39c8d0178e631d2c9834ad10ca463a08c1213ba78d8bf73ddcab23b7fa87eb06e0827e278e46eb947fbce8373fafaf3b5a4076dd7c4bc379fc
7
+ data.tar.gz: 0d6f30b7e49b98c8ba06b4c7e891a8bf902d1ba60ac432538e0fdb7bc6972586e8d56aafb4b6f449a9434574462e5fc4c3104f1b10d16046409dd898dc602141
data/.gitignore CHANGED
@@ -9,4 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
- bench_bloc-*.gem
12
+ bench_bloc-*.gem
13
+ benchmarks.log
@@ -0,0 +1,41 @@
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
+ Logger.new("#{__dir__}/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
@@ -0,0 +1,94 @@
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
@@ -0,0 +1,20 @@
1
+ # TODO: turn into a module
2
+ def run_ruby_prof lam, args
3
+ # RubyProf.start
4
+ # lam.call args
5
+ # RubyProf.end
6
+ end
7
+
8
+ def format_ruby_prof res
9
+ # "Formatted Ruby Prof Results: #{res}"
10
+ end
11
+
12
+ def log_ruby_prof_results
13
+ # prof = RubyProf::Profile.new()
14
+ # prof.exclude_common_methods!
15
+ # validator = ValidationRules::TsValidatorService.new(ts)
16
+ # complex_rules = validator.get_complex_validation_rules
17
+ # binding.pry
18
+ # results = prof.profile { validator.run_complex_vr(complex_rules[0]) }
19
+ # log_prof_results results
20
+ end
@@ -2,84 +2,16 @@
2
2
  require 'rake'
3
3
  require 'pry'
4
4
  require 'benchmark'
5
- require 'bench_bloc'
6
- require 'bench_bloc/rake_helper.rb'
5
+ require 'bench_bloc/helpers/benchmark_helpers'
6
+ require 'bench_bloc/helpers/rake_helpers.rb'
7
7
 
8
8
  # TODO: Look recursively in folders using 'bench_bloc/**/*.bloc.rb
9
- CONFIG_FILES=FileList["bench_bloc/*.bloc.rb"]
9
+ BLOC_FILES=FileList["bench_bloc/*.bloc.rb"]
10
10
 
11
- def put_namespace key, namespace
12
- namespace key do
13
- namespace.keys.each do |ns_key|
14
- if is_task? namespace[ns_key]
15
- put_task ns_key, namespace[ns_key]
16
- else
17
- put_namespace ns_key, namespace[ns_key]
18
- end
19
- end
20
- end
11
+ BLOC_FILES.each do |f|
12
+ bloc = eval File.read(f)
13
+ put_bloc_namespaces bloc
21
14
  end
22
15
 
23
- # TODO: Add a ruby-prof method here if argument is passed
24
- def put_task key, new_task
25
- desc new_task[:desc]
26
- task key => :environment do
27
- to_profs = [new_task[:to_prof].call].flatten
28
- results = Benchmark.bm do |x|
29
- to_profs.each do |tp|
30
- x.report(new_task[:label].call(tp)) do
31
- new_task[:prof].call(tp)
32
- end
33
- end
34
- end
35
- log_results results, "Benchmark Complex Validation Rules on Small Timesheet"
36
- end
37
- # format_ruby_prof(run_ruby_prof(new_task[:prof], tp)) if @options[:ruby_prof] == true
38
- end
39
-
40
- # def run_ruby_prof lam, args
41
- # RubyProf.start
42
- # lam.call args
43
- # RubyProf.end
44
- # end
45
-
46
- # def format_ruby_prof res
47
- # "Formatted Ruby Prof Results: #{res}"
48
- # end
49
-
50
- def is_task? obj
51
- obj.keys.any?(:to_prof)
52
- end
53
-
54
- CONFIG_FILES.each do |f|
55
- config = eval File.read(f)
56
- config.keys.each do |key|
57
- namespace :bench_bloc do
58
- put_namespace key, config[key]
59
- desc "Options parser for bench tasks"
60
- task parse_options_util: :environment do
61
- @options = {}
62
- option_parser.parse!
63
- option_parser.parse!
64
- end
65
- desc "Clear Tests"
66
- task clear_tests_util: :environment do
67
- at_exit do
68
- test_ts = Timesheet.where("general_remarks LIKE '%BENCHTEST%'")
69
- puts "Clearing #{test_ts.count} test timesheets"
70
- test_ts.destroy_all
71
- end
72
- end
73
- desc "Run all benchmarks"
74
- task all: :environment do
75
- bench_tasks.each(&:execute)
76
- Rake::Task["bench_bloc:clear_tests_util"].invoke
77
- end
78
- end
79
- end
80
- end
16
+ add_bench_hooks
81
17
 
82
- bench_tasks.each do |task|
83
- Rake::Task[task.name]
84
- .enhance(['bench_bloc:parse_options_util', 'bench_bloc:clear_tests_util'])
85
- end
@@ -1,3 +1,3 @@
1
1
  module BenchBloc
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
data/lib/bench_bloc.rb CHANGED
@@ -1,36 +1,12 @@
1
1
  require 'bench_bloc/version'
2
- require 'optparse'
2
+ require 'bench_bloc/helpers/benchmark_helpers'
3
+ require 'bench_bloc/helpers/rake_helpers'
4
+ require 'bench_bloc/helpers/ruby_prof_helpers'
3
5
  require 'bench_bloc/railtie' if defined?(Rails)
4
6
 
5
7
  module BenchBloc
6
8
  class Error < StandardError; end
7
-
8
- def log_results results, title
9
- log = Logger.new("../log/benchmarks.log")
10
- results.sort! { |a,b| b.real <=> a.real }
11
- formatted_results = results.map { |res| format_result(res) }
12
- header = "\n---\n\t#{title}\n"
13
- summary = "\tTotal Time: #{summarize_real_time(results).round(2)} seconds\n\n"
14
- log.info(header + summary + formatted_results.join("\n"))
15
- end
16
-
17
- def log_ruby_prof_results
18
- # prof = RubyProf::Profile.new()
19
- # prof.exclude_common_methods!
20
- # validator = ValidationRules::TsValidatorService.new(ts)
21
- # complex_rules = validator.get_complex_validation_rules
22
- # binding.pry
23
- # results = prof.profile { validator.run_complex_vr(complex_rules[0]) }
24
- # log_prof_results results
25
- end
26
-
27
- def summarize_real_time results
28
- results.inject(0) do |agg, res|
29
- agg + res.real
30
- end
31
- end
32
-
33
- def format_result result
34
- "\t\t#{result.label}\n\t\t\t#{result.real.round(2)} seconds"
35
- end
9
+ include BenchBloc::BenchmarkHelpers
10
+ include BenchBloc::RakeHelpers
11
+ include BenchBloc::RubyProfHelpers
36
12
  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.8
4
+ version: 0.1.9
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-12 00:00:00.000000000 Z
11
+ date: 2020-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,8 +72,10 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - lib/bench_bloc.rb
75
+ - lib/bench_bloc/helpers/benchmark_helpers.rb
76
+ - lib/bench_bloc/helpers/rake_helpers.rb
77
+ - lib/bench_bloc/helpers/ruby_prof_helpers.rb
75
78
  - lib/bench_bloc/railtie.rb
76
- - lib/bench_bloc/rake_helper.rb
77
79
  - lib/bench_bloc/tasks/gen_bench_bloc.rake
78
80
  - lib/bench_bloc/version.rb
79
81
  homepage: https://github.com/jdpaterson/bench_bloc
@@ -1,16 +0,0 @@
1
- def bench_tasks
2
- Rake.application.tasks.select do |task|
3
- task.name.starts_with?("bench_bloc") &&
4
- !task.name.ends_with?("_util") &&
5
- task.name != "bench_bloc:all"
6
- end
7
- end
8
-
9
- def option_parser
10
- OptionParser.new do |opts|
11
- opts.banner = "Usage: rake bench_bloc:* [options]"
12
- opts.on("-r", "--ruby-prof", "Print a RubyProf report") do |rp|
13
- @options[:ruby_prof] = rp
14
- end
15
- end
16
- end