require_bench 1.0.4.pre.alpha.7 → 1.0.5

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.
@@ -1,37 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # std libs
4
- require 'forwardable'
4
+ require "forwardable"
5
5
 
6
- # third party libs
7
- # You will need to have gem "colorize" installed!
8
- # NOTE: You will need to require "colorized_string" in your own code,
9
- # in order for this alternate Printer class to load
6
+ # internal colorizer (replaces the GPL-2.0-licensed colorize gem)
7
+ require "require_bench/colors"
8
+
9
+ # Default Printer, unless NO_COLOR is set (see lib/require_bench.rb).
10
10
 
11
11
  class Printer
12
12
  attr_accessor :colors, :color
13
13
 
14
14
  extend Forwardable
15
+
15
16
  def_delegators :@colors, :rotate!, :first
16
17
  def initialize
17
- @colors = ColorizedString.colors.dup.reject { |x| x.match?(/black|white/) }
18
+ @colors = RequireBench::Colors::NAMES.dup
18
19
  end
19
20
 
20
21
  # Log statement when a file starts loading
21
22
  def out_start(file, type)
22
- printf "🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} 📖 %s 🚥\n", file
23
+ printf("🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} 📖 %s 🚥\n", file)
23
24
  end
24
25
 
25
26
  # Log statement when a file completed loading
26
27
  def out_consume(seconds, file, type)
27
- printf "🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} ☑️ %10f %s 🚥\n", seconds, file
28
+ printf("🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} ☑️ %10f %s 🚥\n", seconds, file)
28
29
  rotate!
29
30
  end
30
31
 
31
32
  # Log statement when a file raises an error while loading
32
33
  def out_err(error, file, type)
33
- printf "🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
34
- file
34
+ printf(
35
+ "🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
36
+ file
37
+ )
35
38
  rotate!
36
39
  end
37
40
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RequireBench
4
+ # Minimal internal ANSI colorizer for the color printer.
5
+ #
6
+ # Replaces the GPL-2.0-licensed colorize gem, which was previously the only
7
+ # way to enable colored output. The color printer is the default; see
8
+ # lib/require_bench.rb (set NO_COLOR for the plain printer).
9
+ module Colors
10
+ # ANSI SGR color codes. Black and white are intentionally omitted:
11
+ # they are illegible on common terminal themes.
12
+ CODES = {
13
+ red: 31,
14
+ green: 32,
15
+ yellow: 33,
16
+ blue: 34,
17
+ magenta: 35,
18
+ cyan: 36,
19
+ light_red: 91,
20
+ light_green: 92,
21
+ light_yellow: 93,
22
+ light_blue: 94,
23
+ light_magenta: 95,
24
+ light_cyan: 96
25
+ }.freeze
26
+
27
+ # Ordered color names the color printer rotates through.
28
+ NAMES = CODES.keys.freeze
29
+
30
+ # Wrap a string in the ANSI escape sequence for the named color.
31
+ #
32
+ # @param string [String] the text to colorize
33
+ # @param name [Symbol] one of {NAMES}
34
+ # @return [String] the colorized text
35
+ # @raise [KeyError] when the color name is unknown
36
+ def self.colorize(string, name)
37
+ "\e[#{CODES.fetch(name)}m#{string}\e[0m"
38
+ end
39
+ end
40
+ end
@@ -3,17 +3,19 @@
3
3
  class Printer
4
4
  # Log statement when a file starts loading
5
5
  def out_start(file, type)
6
- printf "🚥 [RequireBench-#{type}] 📖 %s 🚥\n", file
6
+ printf("🚥 [RequireBench-#{type}] 📖 %s 🚥\n", file)
7
7
  end
8
8
 
9
9
  # Log statement when a file completed loading
10
10
  def out_consume(seconds, file, type)
11
- printf "🚥 [RequireBench-#{type}] ☑️ %10f %s 🚥\n", seconds, file
11
+ printf("🚥 [RequireBench-#{type}] ☑️ %10f %s 🚥\n", seconds, file)
12
12
  end
13
13
 
14
14
  # Log statement when a file raises an error while loading
15
15
  def out_err(error, file, type)
16
- printf "🚥 [RequireBench-#{type}] ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
17
- file
16
+ printf(
17
+ "🚥 [RequireBench-#{type}] ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
18
+ file
19
+ )
18
20
  end
19
21
  end
@@ -5,12 +5,12 @@
5
5
  # require 'require_bench/tasks'
6
6
  # will give you the require_bench tasks.
7
7
  # Do it as early as possible during bootstrapping!
8
- require 'require_bench'
8
+ require "require_bench"
9
9
 
10
10
  namespace :require_bench do
11
- desc 'Print timings while booting app, determine slowest loading files'
11
+ desc "Print timings while booting app, determine slowest loading files"
12
12
  task hello: :environment do
13
- tot = 0.0
13
+ tot = 0.0
14
14
  vals = RequireBench::TIMINGS.to_a
15
15
  vals.sort_by! { |a| a[1] }.reverse!
16
16
  if !vals.empty?
@@ -19,8 +19,8 @@ namespace :require_bench do
19
19
  tot += a[1]
20
20
  printf("%2d. %10f %s\n", index + 1, a[1], a[0])
21
21
  end
22
- puts '=========='
23
- printf("%10f %s\n", tot, 'TOTAL')
22
+ puts "=========="
23
+ printf("%10f %s\n", tot, "TOTAL")
24
24
  else
25
25
  puts %(
26
26
  require_bench did not track any requires, because it was required too late, of the configuration excluded everything.
@@ -1,5 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequireBench
4
- VERSION = '1.0.4-alpha.7'
4
+ # Version namespace for this gem.
5
+ module Version
6
+ # Current gem version.
7
+ VERSION = "1.0.5"
8
+ end
9
+ # Current gem version exposed at the traditional constant location.
10
+ VERSION = Version::VERSION # Traditional Constant Location
5
11
  end
data/lib/require_bench.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "require_bench/version"
4
+
3
5
  REQUIRE_BENCH_ENABLED = ENV.fetch('REQUIRE_BENCH', 'false').casecmp?('true')
4
6
 
5
7
  # STD Libs
@@ -8,11 +10,25 @@ if REQUIRE_BENCH_ENABLED
8
10
  require 'timeout'
9
11
  end
10
12
 
13
+ # external libs
14
+ require "version_gem"
15
+
11
16
  # This Gem
12
- require 'require_bench/version'
13
17
 
14
18
  # Namespace for this gem
15
19
  module RequireBench
20
+ # Keep the unwrapped methods out of Kernel's method table. The old alias-based
21
+ # hook was global and could be redefined by another load of this file.
22
+ ORIGINAL_KERNEL_METHODS = {
23
+ require: Kernel.instance_method(:require),
24
+ load: Kernel.instance_method(:load)
25
+ }.freeze
26
+ SEMAPHORE_KEY = :__require_bench_semaphore
27
+
28
+ def self.semaphore_active?
29
+ Thread.current[SEMAPHORE_KEY] == true
30
+ end
31
+
16
32
  if REQUIRE_BENCH_ENABLED
17
33
  TIMINGS = Hash.new { |h, k| h[k] = 0.0 }
18
34
  skips = ENV['REQUIRE_BENCH_SKIP_PATTERN']
@@ -33,10 +49,12 @@ module RequireBench
33
49
  preferred_grouping = precedence.first
34
50
  prefer_not_path = preferred_grouping != 'path' # path correlates to default behavior of regexp matching
35
51
 
36
- if defined?(ColorizedString)
37
- require 'require_bench/color_printer'
38
- else
52
+ no_color = ENV.key?('NO_COLOR') && !ENV['NO_COLOR'].empty?
53
+ if no_color
39
54
  require 'require_bench/printer'
55
+ else
56
+ require 'require_bench/colors'
57
+ require 'require_bench/color_printer'
40
58
  end
41
59
  PRINTER = Printer.new
42
60
 
@@ -91,7 +109,8 @@ module RequireBench
91
109
  TRACKED_METHODS = tracked_methods
92
110
 
93
111
  def consume_with_timing(type, file, *args)
94
- $require_bench_semaphore = true
112
+ previous_semaphore = Thread.current[SEMAPHORE_KEY]
113
+ Thread.current[SEMAPHORE_KEY] = true
95
114
  short_type = type[0]
96
115
  ret = nil
97
116
  # Not sure if this is actually a useful abstraction...
@@ -126,57 +145,72 @@ module RequireBench
126
145
  RequireBench::TIMINGS[prefix] += seconds
127
146
  ret
128
147
  ensure
129
- $require_bench_semaphore = nil
148
+ Thread.current[SEMAPHORE_KEY] = previous_semaphore
130
149
  end
131
150
  module_function :consume_with_timing
132
151
  end
133
- end
134
-
135
- if REQUIRE_BENCH_ENABLED
136
- # A Kernel hack that adds require timing to find require problems in app.
137
- module Kernel
138
- alias require_without_timing require
139
- alias load_without_timing load
140
152
 
141
- def require(file)
142
- _require_bench_consume_file('require', file)
143
- end
153
+ if REQUIRE_BENCH_ENABLED
154
+ # A Kernel prepend keeps one stable interception point and lets us call the
155
+ # original methods without mutating Kernel with aliases.
156
+ module KernelHook
157
+ def require(file)
158
+ _require_bench_consume_file('require', file)
159
+ end
144
160
 
145
- def load(file, *args)
146
- _require_bench_consume_file('load', file, *args)
147
- end
161
+ def load(file, *args)
162
+ _require_bench_consume_file('load', file, *args)
163
+ end
148
164
 
149
- def _require_bench_consume_file(type, file, *args)
150
- file_path = file.to_s
151
- # byebug if file_path.match?(/no_group_fox/)
165
+ def require_without_timing(file, *args)
166
+ RequireBench::ORIGINAL_KERNEL_METHODS[:require].bind(self).call(file, *args)
167
+ end
152
168
 
153
- # Global $ variable, which is always truthy while inside the hack, is to
154
- # prevent a scenario that might result in infinite recursion.
155
- return send("#{type}_without_timing", file_path, *args) if $require_bench_semaphore
169
+ def load_without_timing(file, *args)
170
+ RequireBench::ORIGINAL_KERNEL_METHODS[:load].bind(self).call(file, *args)
171
+ end
156
172
 
157
- short_type = type[0]
158
- measure = RequireBench::INCLUDE_PATTERN && file_path.match?(RequireBench::INCLUDE_PATTERN)
159
- skippy = RequireBench::SKIP_PATTERN && file_path.match?(RequireBench::SKIP_PATTERN)
160
- RequireBench::PRINTER.out_start(file, short_type) if RequireBench::LOG_START
161
- if RequireBench::RESCUED_CLASSES.any?
162
- begin
173
+ def _require_bench_consume_file(type, file, *args)
174
+ file_path = file.to_s
175
+ return send("#{type}_without_timing", file_path, *args) if RequireBench.semaphore_active?
176
+
177
+ short_type = type[0]
178
+ measure = RequireBench::INCLUDE_PATTERN && file_path.match?(RequireBench::INCLUDE_PATTERN)
179
+ skippy = RequireBench::SKIP_PATTERN && file_path.match?(RequireBench::SKIP_PATTERN)
180
+ RequireBench::PRINTER.out_start(file, short_type) if RequireBench::LOG_START
181
+ if RequireBench::RESCUED_CLASSES.any?
182
+ begin
183
+ _require_bench_file(type, measure, skippy, file_path, *args)
184
+ rescue *RequireBench::RESCUED_CLASSES => e
185
+ RequireBench::PRINTER.out_err(e, file, short_type, *args)
186
+ end
187
+ else
163
188
  _require_bench_file(type, measure, skippy, file_path, *args)
164
- rescue *RequireBench::RESCUED_CLASSES => e
165
- RequireBench::PRINTER.out_error(e, file, short_type, *args)
166
189
  end
167
- else
168
- _require_bench_file(type, measure, skippy, file_path, *args)
169
190
  end
170
- end
171
191
 
172
- def _require_bench_file(type, measure, skippy, file_path, *args)
173
- if !measure && skippy
174
- send("#{type}_without_timing", file_path, *args)
175
- elsif RequireBench::INCLUDE_PATTERN.nil? || measure
176
- RequireBench.consume_with_timing(type, file_path, *args)
177
- else
178
- send("#{type}_without_timing", file_path, *args)
192
+ def _require_bench_file(type, measure, skippy, file_path, *args)
193
+ if !measure && skippy
194
+ send("#{type}_without_timing", file_path, *args)
195
+ elsif RequireBench::INCLUDE_PATTERN.nil? || measure
196
+ RequireBench.consume_with_timing(type, file_path, *args)
197
+ else
198
+ send("#{type}_without_timing", file_path, *args)
199
+ end
179
200
  end
201
+
202
+ private :require, :load, :require_without_timing, :load_without_timing,
203
+ :_require_bench_consume_file, :_require_bench_file
180
204
  end
205
+
206
+ Kernel.prepend(KernelHook) unless Kernel.ancestors.include?(KernelHook)
207
+ # Before Ruby 3.0, prepending to Kernel does not reach Object (or any class that
208
+ # already included Kernel), so the hook never engaged. Prepend to Object as well;
209
+ # on Ruby >= 3.0 Object already sees the hook through Kernel, making this a no-op.
210
+ Object.prepend(KernelHook) unless Object.ancestors.include?(KernelHook)
181
211
  end
182
212
  end
213
+
214
+ RequireBench::Version.class_eval do
215
+ extend VersionGem::Basic
216
+ end
@@ -0,0 +1,6 @@
1
+ module RequireBench
2
+ module Version
3
+ VERSION: String
4
+ end
5
+ VERSION: String
6
+ end
data.tar.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ U|e�� �C��ƺ.�3�
2
+ �*�� Z��J'������o�ЌV�j1=�V�f�q������x��Հ�te�0�D�mTj��$N
3
+ ��O4��������6�g�#�R%\�=�ACb���l�$4�(gc��ܴ#.�ܴ�T.���P��6���X���a��2-\�F ��q3�F`���>k�U�Sy��!�y*�I0��a* 9~���(/�6��ݙ��E��֪�����Ɛ��H�6������� %`�Q�L�+8�� <�*޾��e(F�� a�k��hAˆ:�Kܞ�=���혌j���f�O� ���֤u��s0�谚�ђ�H�T7���m ֟Ҍ(q����J�;qf$#i��J$���}j�v (���