factory_girl-benchmark 0.0.1

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
+ SHA1:
3
+ metadata.gz: f457a07c3ebf1a14034f8b1958303b38d216ce7c
4
+ data.tar.gz: b0380f100da4158d2e7a76d6a488427e232d0fbe
5
+ SHA512:
6
+ metadata.gz: 54b5d68e70a49dfdc70fddf22a6126685f611bcf3722347b47741f10dd00da9769ac4a294e39b0425c4502c7e889fca99e3aff1f852f25e3cc90009a1f40aa02
7
+ data.tar.gz: 07f3ce177c397553b51e88cd6a70996ef502f2923b2c0210d71cadb96abf9b8a2985dadf6159ca1430af490181263efcd46717073c6682c44cb66b343c14ed97
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in factory_girl-benchmark.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # FactoryGirl::Benchmark
2
+
3
+ benchmark your top-level factory_girl factories
4
+
5
+ ## Installation
6
+
7
+ ```rb
8
+ gem 'factory_girl-benchmark'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Here's an example of how we use it in our test helper:
14
+
15
+ ```rb
16
+ if ENV['FG_BENCH']
17
+ require 'factory_girl/benchmark'
18
+ FactoryGirl.singleton_class.prepend(FactoryGirl::Benchmark)
19
+ # Register before minitest to have minitest run first
20
+ at_exit { FactoryGirl::Benchmark.report }
21
+ end
22
+ ```
23
+
24
+ You could also use the example which will run all of your factories:
25
+
26
+ ```rb
27
+ FactoryGirl::Benchmark.benchmark!
28
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'factory_girl/benchmark/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "factory_girl-benchmark"
8
+ spec.version = FactoryGirl::Benchmark::VERSION
9
+ spec.authors = ["Josh Bodah"]
10
+ spec.email = ["jb3689@yahoo.com"]
11
+
12
+ spec.summary = %q{benchmark your top-level factory_girl factories}
13
+ spec.homepage = "https://github.com/backupify/factory_girl-benchmark"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'factory_girl'
21
+ spec.add_dependency 'colorize'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,108 @@
1
+ require 'factory_girl/benchmark/version'
2
+ require 'factory_girl'
3
+ require 'colorize'
4
+
5
+ module FactoryGirl
6
+ module Benchmark
7
+ %i{create build build_stubbed attributes_for build_list create_list build_pair create_pair}.each do |sym|
8
+ define_method(sym) do |*args, &block|
9
+ if FactoryGirl::Benchmark.is_benching?
10
+ super *args, &block
11
+ else
12
+ FactoryGirl::Benchmark.bm(sym, args) do
13
+ super *args, &block
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ class << self
20
+ # Example usage
21
+ def benchmark!
22
+ # Install
23
+ FactoryGirl.singleton_class.prepend(FactoryGirl::Benchmark)
24
+
25
+ # Run
26
+ FactoryGirl.find_definitions if FactoryGirl.factories.none?
27
+ FactoryGirl.factories.map(&:name).each do |f|
28
+ begin
29
+ FactoryGirl.create(f)
30
+ rescue => e
31
+ puts "Couldn't benchmark factory #{f} due to #{e}"
32
+ end
33
+ end
34
+
35
+ # Report
36
+ FactoryGirl::Benchmark.report
37
+ end
38
+
39
+ def benchmarks
40
+ @benchmarks ||= {}
41
+ end
42
+
43
+ def is_benching?
44
+ @benching
45
+ end
46
+
47
+ def bm(sym, args)
48
+ benching = true
49
+ rv = nil
50
+
51
+ bm = ::Benchmark.measure { rv = yield }
52
+
53
+ record = { sym: sym, bm: bm.real, args: args, bt: caller.select {|x| x[/#{Rails.root}/]} }
54
+ key = "FactoryGirl.#{sym}##{args.first}"
55
+ benchmarks[key] ||= []
56
+ benchmarks[key] << record
57
+
58
+ rv
59
+ ensure
60
+ benching = false
61
+ end
62
+
63
+ def benching=(o)
64
+ @benching = o
65
+ end
66
+
67
+ def report
68
+ puts 'Most frequent'
69
+ r = benchmarks.reduce([]) do |m, (k, _)|
70
+ m << { key: k, count: benchmarks[k].size }
71
+ end.sort {|a,b| b[:count] <=> a[:count]}
72
+ print_report(r)
73
+
74
+ puts 'Slowest instances'
75
+ r = benchmarks.each_value.to_a.flatten.sort {|a,b| b[:bm] <=> a[:bm]}
76
+ print_report(r)
77
+
78
+ puts 'Slowest total'
79
+ r = benchmarks.reduce([]) do |m, (k, _)|
80
+ m << { key: k, count: benchmarks[k].size, total_time: benchmarks[k].reduce(0) {|sum, bm| sum + bm[:bm]} }
81
+ end.sort {|a,b| b[:total_time] <=> a[:total_time]}
82
+ print_report(r)
83
+
84
+ puts 'Slowest average'
85
+ r = benchmarks.reduce([]) do |m, (k, _)|
86
+ total_time = benchmarks[k].reduce(0) {|sum, bm| sum + bm[:bm]}
87
+ count = benchmarks[k].size
88
+ m << { key: k, avg: total_time.to_f/count, count: count, total_time: total_time }
89
+ end.sort {|a,b| b[:avg] <=> a[:avg]}
90
+ print_report(r)
91
+ end
92
+
93
+ def print_report(arr)
94
+ require 'colorize'
95
+ arr.first(20).map {|h| puts "\t#{h}".colorize(color)}
96
+ puts
97
+ end
98
+
99
+ def color
100
+ @last_color = case @last_color
101
+ when nil then :yellow
102
+ when :yellow then :light_cyan
103
+ when :light_cyan then :yellow
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ module FactoryGirl
2
+ module Benchmark
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_girl-benchmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Bodah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_girl
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: colorize
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jb3689@yahoo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - factory_girl-benchmark.gemspec
96
+ - lib/factory_girl/benchmark.rb
97
+ - lib/factory_girl/benchmark/version.rb
98
+ homepage: https://github.com/backupify/factory_girl-benchmark
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.8
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: benchmark your top-level factory_girl factories
121
+ test_files: []