factory_bot_profile 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: d2edd21eb175a104b5ba7ab51adff07b1925d6d42a6c64ca5208f11d8dc3e4d6
4
+ data.tar.gz: 178a9cdace27bb6af0c568fa574a02e680af692fd105a55faacf79401f5e9a47
5
+ SHA512:
6
+ metadata.gz: 178512dae69fcfc88995f67caa1b025484ecfa2e4ea9be844619107c069285e25714fd806b516cfd58517e536b9ef7a546f5f6cd1ad663f6ecf17703dd8d210f
7
+ data.tar.gz: fcf87824d84100ce20355a21a391bd17ddebbb80b81830940a635ccfa9ecd2f1098bfbb51a63bcc36a1ddae36fb71c00c8618d15ad13118416c96328a3ecbcc0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-19
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at danieljamescolson@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 13.0"
8
+ gem "rspec", "~> 3.0"
9
+ gem "standard", "~> 1.3"
10
+ gem "timecop"
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ factory_bot_profile (0.1.0)
5
+ factory_bot (>= 6.2.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.0.4)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ ast (2.4.2)
16
+ concurrent-ruby (1.1.10)
17
+ diff-lcs (1.5.0)
18
+ factory_bot (6.2.1)
19
+ activesupport (>= 5.0.0)
20
+ i18n (1.12.0)
21
+ concurrent-ruby (~> 1.0)
22
+ json (2.6.2)
23
+ minitest (5.16.3)
24
+ parallel (1.22.1)
25
+ parser (3.1.2.1)
26
+ ast (~> 2.4.1)
27
+ rainbow (3.1.1)
28
+ rake (13.0.6)
29
+ regexp_parser (2.6.0)
30
+ rexml (3.2.5)
31
+ rspec (3.11.0)
32
+ rspec-core (~> 3.11.0)
33
+ rspec-expectations (~> 3.11.0)
34
+ rspec-mocks (~> 3.11.0)
35
+ rspec-core (3.11.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-expectations (3.11.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.11.0)
40
+ rspec-mocks (3.11.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.11.0)
43
+ rspec-support (3.11.1)
44
+ rubocop (1.35.1)
45
+ json (~> 2.3)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.1.2.1)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.20.1, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 1.4.0, < 3.0)
54
+ rubocop-ast (1.23.0)
55
+ parser (>= 3.1.1.0)
56
+ rubocop-performance (1.14.3)
57
+ rubocop (>= 1.7.0, < 2.0)
58
+ rubocop-ast (>= 0.4.0)
59
+ ruby-progressbar (1.11.0)
60
+ standard (1.16.1)
61
+ rubocop (= 1.35.1)
62
+ rubocop-performance (= 1.14.3)
63
+ timecop (0.9.5)
64
+ tzinfo (2.0.5)
65
+ concurrent-ruby (~> 1.0)
66
+ unicode-display_width (2.3.0)
67
+
68
+ PLATFORMS
69
+ x86_64-darwin-21
70
+ x86_64-linux
71
+
72
+ DEPENDENCIES
73
+ factory_bot_profile!
74
+ rake (~> 13.0)
75
+ rspec (~> 3.0)
76
+ standard (~> 1.3)
77
+ timecop
78
+
79
+ BUNDLED WITH
80
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Daniel Colson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # FactoryBotProfile
2
+
3
+ FactoryBotProfile identifies expensive factories and heavy [factory\_bot][] usage.
4
+ Use this information to speed up your test suite!
5
+
6
+ [factory\_bot]: https://github.com/thoughtbot/factory_bot
7
+
8
+ ## Installation
9
+
10
+ Install the gem and add to the application's Gemfile by executing:
11
+
12
+ $ bundle add factory_bot_profile
13
+
14
+ If bundler is not being used to manage dependencies, install the gem by executing:
15
+
16
+ $ gem install factory_bot_profile
17
+
18
+ ## Usage
19
+
20
+ The primary API for using this library is to wrap the code you want to profile
21
+ with the `.reporting` method:
22
+
23
+ ```rb
24
+ require "factory_bot_profile"
25
+
26
+ FactoryBotProfile.reporting do
27
+ # Code that uses factory_bot
28
+ end
29
+ ```
30
+
31
+ This will profile every call to factory\_bot inside the block, and then print
32
+ the results to stdout after the code inside the block finishes.
33
+
34
+ If you need more control over where to start and stop profiling, and when to
35
+ report the results, use the `.subscribe` and `.report` methods:
36
+
37
+ ```rb
38
+ subscription = FactoryBotProfile.subscribe
39
+
40
+ # Code that uses factory_bot
41
+
42
+ subscription.unsubscribe
43
+ FactoryBotProfile.report(subscription.stats)
44
+ ```
45
+
46
+ ## Why not FactoryProf?
47
+
48
+ [FactoryProf][] is another fantastic tool for profiling factories, and it
49
+ includes a really neat flamegraph feature that this library doesn't have (at
50
+ least not yet). So why did I write this library?
51
+
52
+ FactoryProf profiles factories by monkey patching factory\_bot. It's also part
53
+ of the larger [TestProf][] library, which includes a number of other monkey
54
+ patches, and it's difficult to load FactoryProf without bringing in other parts
55
+ of the library as well. These monkey patches can make it difficult to add
56
+ FactoryProf to an application.
57
+
58
+ FactoryBotProfile, on the other hand, uses factory\_bot's built-in
59
+ [instrumentation][] to build the profile. My hope is that using built-in
60
+ factory\_bot features, and avoiding monkey patches or private APIs should make
61
+ this library fairly stable.
62
+
63
+ [FactoryProf]: https://test-prof.evilmartians.io/#/profilers/factory_prof?id=factoryprof
64
+ [TestProf]: https://test-prof.evilmartians.io/
65
+ [instrumentation]: https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#activesupport-instrumentation
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/composerinteralia/factory_bot_profile. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/composerinteralia/factory_bot_profile/blob/main/CODE_OF_CONDUCT.md).
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the FactoryBotProfile project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/composerinteralia/factory_bot_profile/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/factory_bot_profile/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "factory_bot_profile"
7
+ spec.version = FactoryBotProfile::VERSION
8
+ spec.authors = ["Daniel Colson"]
9
+ spec.email = ["danieljamescolson@gmail.com"]
10
+
11
+ spec.summary = "Profiling for factory_bot"
12
+ spec.homepage = "https://github.com/composerinterali/factory_bot_profile"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(__dir__) do
19
+ `git ls-files -z`.split("\x0").reject do |f|
20
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
21
+ end
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "factory_bot", ">= 6.2.1"
28
+ end
@@ -0,0 +1,64 @@
1
+ require_relative "stat"
2
+
3
+ module FactoryBotProfile
4
+ class AggregateStats
5
+ def initialize
6
+ @by_factory = stats_hash
7
+ end
8
+
9
+ def collect(frame)
10
+ @by_factory[frame.name].increment(frame)
11
+ end
12
+
13
+ def total_time
14
+ @by_factory.values.map(&:total_self_time).sum
15
+ end
16
+
17
+ def highest_count(n = 1)
18
+ take_factory_values_by(:count, n)
19
+ end
20
+
21
+ def highest_total_time(n = 1)
22
+ take_factory_values_by(:total_time, n)
23
+ end
24
+
25
+ def highest_self_time(n = 1)
26
+ take_factory_values_by(:total_self_time, n)
27
+ end
28
+
29
+ def highest_average_time(n = 1)
30
+ take_factory_values_by(:average_time, n)
31
+ end
32
+
33
+ def marshal_dump
34
+ # Cannot dump hash with a default proc, so make a copy with no default
35
+ # Need to disable standard because to_h is not the same thing in this case
36
+ Hash[@by_factory] # rubocop:disable Style/HashConversion
37
+ end
38
+
39
+ def marshal_load(by_factory)
40
+ @by_factory = stats_hash.merge!(by_factory)
41
+ end
42
+
43
+ def merge!(other)
44
+ other.each_factory do |name, stat|
45
+ @by_factory[name].merge!(stat)
46
+ end
47
+ self
48
+ end
49
+
50
+ def each_factory(...)
51
+ @by_factory.each(...)
52
+ end
53
+
54
+ private
55
+
56
+ def stats_hash
57
+ Hash.new { |h, k| h[k] = FactoryStat.new(k) }
58
+ end
59
+
60
+ def take_factory_values_by(stat, n)
61
+ @by_factory.values.sort_by(&stat).last(n).reverse
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,35 @@
1
+ module FactoryBotProfile
2
+ class Frame
3
+ attr_accessor :name, :start, :child_time
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @start = Time.now
8
+ @child_time = Hash.new { 0 }
9
+ end
10
+
11
+ def observe_child(frame)
12
+ @child_time[frame.name] += frame.self_time
13
+ frame.child_time.each do |factory_name, time|
14
+ @child_time[factory_name] += time
15
+ end
16
+ end
17
+
18
+ def duration
19
+ raise "self time called before frame was stopped" unless @duration
20
+ @duration
21
+ end
22
+
23
+ def finish!
24
+ @duration = Time.now - @start
25
+ end
26
+
27
+ def self_time
28
+ duration - total_child_time
29
+ end
30
+
31
+ def total_child_time
32
+ @child_time.values.sum
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,44 @@
1
+ module FactoryBotProfile
2
+ module Report
3
+ class SimpleReport
4
+ def initialize(stats, io: $stdout)
5
+ @stats = stats
6
+ @io = io
7
+ end
8
+
9
+ def deliver
10
+ io.puts
11
+ io.puts "++++++++ factory_bot stats:"
12
+ io.puts
13
+ io.puts " Spent #{stats.total_time.round(2)} seconds in factory_bot"
14
+ io.puts
15
+ io.puts " Factories taking the most time overall:"
16
+ stats.highest_total_time(3).each do |stat|
17
+ io.puts " - :#{stat.name} factory took #{stat.total_time.round(2)} seconds overall (ran #{stat.count} times)"
18
+ stat.child_stats_by_total_time.reverse_each do |child_stat|
19
+ io.puts " - #{child_stat.total_time.round(2)} seconds spent in :#{child_stat.name} (ran #{child_stat.count}/#{stat.count} times)"
20
+ end
21
+ io.puts
22
+ end
23
+ io.puts
24
+ io.puts " Slowest factories on average:"
25
+ stats.highest_average_time(3).each do |stat|
26
+ io.puts " - :#{stat.name} factory took #{stat.average_time.round(2)} seconds on average (ran #{stat.count} times)"
27
+ stat.child_stats_by_average_time.reverse_each do |child_stat|
28
+ io.puts " - #{child_stat.average_time.round(2)} seconds spent in :#{child_stat.name} on average (ran #{child_stat.count}/#{stat.count} times)"
29
+ end
30
+ io.puts
31
+ end
32
+ io.puts
33
+ io.puts " Most used factories:"
34
+ stats.highest_count(5).each do |stat|
35
+ io.puts " - :#{stat.name} factory ran #{stat.count} times"
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :stats, :io
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ require_relative "frame"
2
+
3
+ module FactoryBotProfile
4
+ class Stack
5
+ def initialize
6
+ @stack = []
7
+ end
8
+
9
+ def <<(name)
10
+ @stack << Frame.new(name)
11
+ end
12
+
13
+ def pop
14
+ @stack.pop.tap do |frame|
15
+ frame.finish!
16
+ @stack.last.observe_child(frame) unless @stack.empty?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,86 @@
1
+ module FactoryBotProfile
2
+ class Stat
3
+ attr_reader :name, :count, :total_time
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @count = 0
8
+ @total_time = 0
9
+ end
10
+
11
+ def average_time
12
+ total_time / count
13
+ end
14
+
15
+ def merge!(other)
16
+ raise "attempting to merge unrelated stats" if name != other.name
17
+
18
+ @count += other.count
19
+ @total_time += other.total_time
20
+ end
21
+ end
22
+
23
+ class ChildStat < Stat
24
+ def increment(time)
25
+ @count += 1
26
+ @total_time += time
27
+ end
28
+ end
29
+
30
+ class FactoryStat < Stat
31
+ attr_reader :child_stats
32
+
33
+ def initialize(name)
34
+ super
35
+ @child_stats = child_stats_hash
36
+ end
37
+
38
+ def increment(frame)
39
+ @count += 1
40
+ @total_time += frame.duration
41
+
42
+ frame.child_time.each do |name, time|
43
+ @child_stats[name].increment(time)
44
+ end
45
+ end
46
+
47
+ def total_self_time
48
+ total_time - total_child_time
49
+ end
50
+
51
+ def total_child_time
52
+ child_stats.values.map(&:total_time).sum
53
+ end
54
+
55
+ def child_stats_by_total_time
56
+ child_stats.values.sort_by(&:total_time)
57
+ end
58
+
59
+ def child_stats_by_average_time
60
+ child_stats.values.sort_by(&:average_time)
61
+ end
62
+
63
+ def merge!(other)
64
+ super
65
+
66
+ other.child_stats.each do |name, stat|
67
+ @child_stats[name].merge!(stat)
68
+ end
69
+ end
70
+
71
+ def marshal_dump
72
+ [@name, @count, @total_time, Hash[@child_stats]] # rubocop:disable Style/HashConversion
73
+ end
74
+
75
+ def marshal_load(data)
76
+ @name, @count, @total_time, child_stats = data
77
+ @child_stats = child_stats_hash.merge!(child_stats)
78
+ end
79
+
80
+ private
81
+
82
+ def child_stats_hash
83
+ Hash.new { |h, k| h[k] = ChildStat.new(k) }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "stack"
2
+
3
+ module FactoryBotProfile
4
+ class Subscriber
5
+ def initialize(stats)
6
+ @stack = Stack.new
7
+ @stats = stats
8
+ end
9
+
10
+ def start(_, _, payload)
11
+ @stack << payload[:name]
12
+ end
13
+
14
+ def finish(*)
15
+ @stats.collect(@stack.pop)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module FactoryBotProfile
2
+ class Subscription
3
+ attr_reader :stats
4
+
5
+ def initialize(stats)
6
+ @stats = stats
7
+ end
8
+
9
+ def subscribe
10
+ @subscription = ActiveSupport::Notifications.subscribe(
11
+ "factory_bot.run_factory",
12
+ FactoryBotProfile::Subscriber.new(stats)
13
+ )
14
+ self
15
+ end
16
+
17
+ def unsubscribe
18
+ ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
19
+ @subscription = nil
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBotProfile
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/notifications"
4
+
5
+ require_relative "factory_bot_profile/version"
6
+ require_relative "factory_bot_profile/aggregate_stats"
7
+ require_relative "factory_bot_profile/subscriber"
8
+ require_relative "factory_bot_profile/subscription"
9
+ require_relative "factory_bot_profile/report/simple_report"
10
+
11
+ module FactoryBotProfile
12
+ def self.reporting
13
+ subscription = subscribe
14
+ result = yield
15
+ report(subscription.stats)
16
+ result
17
+ ensure
18
+ subscription.unsubscribe
19
+ end
20
+
21
+ def self.subscribe(stats = AggregateStats.new)
22
+ Subscription.new(stats).subscribe
23
+ end
24
+
25
+ def self.report(stats, reporter: Report::SimpleReport, io: $stdout)
26
+ reporter.new(stats, io: io).deliver if stats
27
+ end
28
+
29
+ def self.merge_and_report(all_stats, reporter: Report::SimpleReport, io: $stdout)
30
+ merged_stats = all_stats.reduce(AggregateStats.new, &:merge!)
31
+ report(merged_stats, reporter: reporter, io: io)
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_bot_profile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Colson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_bot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.2.1
27
+ description:
28
+ email:
29
+ - danieljamescolson@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".standard.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - factory_bot_profile.gemspec
44
+ - lib/factory_bot_profile.rb
45
+ - lib/factory_bot_profile/aggregate_stats.rb
46
+ - lib/factory_bot_profile/frame.rb
47
+ - lib/factory_bot_profile/report/simple_report.rb
48
+ - lib/factory_bot_profile/stack.rb
49
+ - lib/factory_bot_profile/stat.rb
50
+ - lib/factory_bot_profile/subscriber.rb
51
+ - lib/factory_bot_profile/subscription.rb
52
+ - lib/factory_bot_profile/version.rb
53
+ homepage: https://github.com/composerinterali/factory_bot_profile
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.0.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.3.7
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Profiling for factory_bot
76
+ test_files: []