stable_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: 16437ef249a5c2ac5fb5191c05027e0c6c44c49bbf67474f423cbf548983349a
4
+ data.tar.gz: '0954a34a0d551bbcf65b107eebf54b48ea4068f4bdf1e94767dd0390b1022d02'
5
+ SHA512:
6
+ metadata.gz: 70d79de08dde779a5533725b63bfd2d66dc451c79a8a2e4bc327e5ba98f5905a4653358090330c965079070cd035c9d0bcaa034d9f205a04ad990d4cc7664e56
7
+ data.tar.gz: 488c8a2ef2fdeb8cd89bee5d1fb28873d40b4ece0b370c4f57c58f43c168bd3e5d2fcc46d705122206ef1586e502db560948c1e2412362afafc4a955df41f87a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Robb Shecter
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,36 @@
1
+ # Stable RSpec Profile
2
+
3
+
4
+ TODO: Delete this and the text below, and describe your gem
5
+
6
+ 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/stable_profile`. To experiment with that code, run `bin/console` for an interactive prompt.
7
+
8
+ ## Installation
9
+
10
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Development
25
+
26
+ 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.
27
+
28
+ 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).
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stable_profile.
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StableProfile
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+ require 'ruby-progressbar'
6
+
7
+ require_relative "stable_profile/version"
8
+
9
+ module StableProfile
10
+ class Error < StandardError; end
11
+
12
+ # TODO: Create command line options:
13
+ # --iterations 10
14
+ # --top-slowest-examples 10
15
+
16
+ class String
17
+ def bold
18
+ "\e[1m#{self}\e[22m"
19
+ end
20
+ end
21
+
22
+ module_function
23
+
24
+ # How many items to output in each category.
25
+ TOP_SLOWEST_EXAMPLES = 5
26
+
27
+ # The more iterations you run, the more accurate the results will be.
28
+ # 20 seems like plenty, but it could take a while depending on the
29
+ # size of your test suite.
30
+ ITERATIONS = 4
31
+
32
+ # It's a slow one if it showed up in at least half the profile runs.
33
+ MINIMUM_SAMPLE_SIZE = ITERATIONS / 2
34
+ DECIMAL_PLACES = 4
35
+
36
+ OUTPUT_DIR = 'tmp/multi_profile'
37
+
38
+
39
+ def run
40
+ # Erase and Create the output directory
41
+ FileUtils.rm_rf(OUTPUT_DIR)
42
+ FileUtils.mkdir_p(OUTPUT_DIR)
43
+
44
+ # Run the specs ITERATIONS times, each time with a different random seed
45
+ progressbar = ProgressBar.create(title: 'Running profiles', total: ITERATIONS, format: '%t: |%B| %p%% %a')
46
+ ITERATIONS.times do |i|
47
+ system("rspec --profile 50 --order random --format json > #{OUTPUT_DIR}/multi_profile_#{i+1}.json")
48
+ progressbar.increment
49
+ end
50
+
51
+ # Read the results from the JSON files into an array.
52
+ outputs = Dir.glob("#{OUTPUT_DIR}/*.json").map { |file| JSON.parse(File.read(file)) }
53
+
54
+ # Extract the JSON profile structures from the full results.
55
+ profile_blocks = outputs.map { |output| output.fetch('profile') }
56
+ examples = profile_blocks.map { |profile| profile.fetch('examples') }.flatten
57
+ groups = profile_blocks.map { |profile| profile.fetch('groups') }.flatten
58
+
59
+ # Create a hash, keyed by "id", with the times spent in each example.
60
+ example_times = examples.each_with_object({}) do |example, hash|
61
+ hash[example.fetch('id')] ||= {run_times: [], average_time: 0, example: example}
62
+ hash[example.fetch('id')][:run_times] << example.fetch('run_time')
63
+ end
64
+
65
+ # Create a hash, keyed by "location", with the times spent in each group.
66
+ group_times = groups.each_with_object({}) do |group, hash|
67
+ hash[group.fetch('location')] ||= {run_times: [], average_time: 0, group: group}
68
+ hash[group.fetch('location')][:run_times] << group.fetch('total_time')
69
+ end
70
+
71
+ # Calculate the average time spent in each example.
72
+ example_times.each do |id, record|
73
+ example_times[id][:average_time] = (record[:run_times].inject(:+) / record[:run_times].size).round(DECIMAL_PLACES)
74
+ end
75
+
76
+ # Calculate the average time spent in each group.
77
+ group_times.each do |id, record|
78
+ group_times[id][:average_time] = (record[:run_times].inject(:+) / record[:run_times].size).round(DECIMAL_PLACES)
79
+ end
80
+
81
+ # Mimic RSpec profile output
82
+ puts
83
+ puts "Top #{TOP_SLOWEST_EXAMPLES} slowest examples:"
84
+ count = 0
85
+ example_times.sort_by { |id, record| record[:average_time] }.reverse.each do |id, record|
86
+ next if count == TOP_SLOWEST_EXAMPLES
87
+ next if record[:run_times].size < MINIMUM_SAMPLE_SIZE
88
+ count += 1
89
+
90
+ example = record.fetch(:example)
91
+
92
+ puts " #{example['full_description']}"
93
+ puts " #{record[:average_time]} seconds".bold.ljust(27) + " (N=#{record[:run_times].size})".ljust(7) + " #{example['file_path']}:#{example['line_number']}"
94
+ end
95
+
96
+ puts
97
+ puts "Top #{TOP_SLOWEST_EXAMPLES} slowest example groups:"
98
+ count = 0
99
+ group_times.sort_by { |id, record| record[:average_time] }.reverse.each do |id, record|
100
+ next if count == TOP_SLOWEST_EXAMPLES
101
+ next if record[:run_times].size < MINIMUM_SAMPLE_SIZE
102
+ count += 1
103
+
104
+ group = record.fetch(:group)
105
+
106
+ puts " #{group['description']}"
107
+ puts " #{record[:average_time]} seconds".bold.ljust(27) + " (N=#{record[:run_times].size})".ljust(7) + " #{group['location']}"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,4 @@
1
+ module StableProfile
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stable_profile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robb Shecter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-progressbar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.13.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.13.0
27
+ description: Repeatedly run --profile, averaging the results.
28
+ email:
29
+ - robb@public.law
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/stable_profile.rb
39
+ - lib/stable_profile/version.rb
40
+ - sig/stable_profile.rbs
41
+ homepage:
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.4.21
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Runs RSpec profile with predictable results.
64
+ test_files: []