require_bench 1.0.4 → 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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +144 -4
- data/LICENSE.md +11 -0
- data/README.md +459 -138
- data/lib/require_bench/color_printer.rb +10 -9
- data/lib/require_bench/colors.rb +40 -0
- data/lib/require_bench/printer.rb +1 -1
- data/lib/require_bench/version.rb +5 -1
- data/lib/require_bench.rb +70 -43
- data/sig/require_bench.rbs +6 -0
- data.tar.gz.sig +0 -0
- metadata +120 -112
- metadata.gz.sig +0 -0
- data/CODE_OF_CONDUCT.md +0 -84
- data/CONTRIBUTING.md +0 -38
- data/LICENSE.txt +0 -7
- data/SECURITY.md +0 -13
|
@@ -3,36 +3,37 @@
|
|
|
3
3
|
# std libs
|
|
4
4
|
require "forwardable"
|
|
5
5
|
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
#
|
|
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 =
|
|
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("🚥 #{
|
|
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("🚥 #{
|
|
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
34
|
printf(
|
|
34
|
-
"🚥 #{
|
|
35
|
-
file
|
|
35
|
+
"🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
|
|
36
|
+
file
|
|
36
37
|
)
|
|
37
38
|
rotate!
|
|
38
39
|
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
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module RequireBench
|
|
4
|
+
# Version namespace for this gem.
|
|
4
5
|
module Version
|
|
5
|
-
|
|
6
|
+
# Current gem version.
|
|
7
|
+
VERSION = "1.0.5"
|
|
6
8
|
end
|
|
9
|
+
# Current gem version exposed at the traditional constant location.
|
|
10
|
+
VERSION = Version::VERSION # Traditional Constant Location
|
|
7
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
|
|
@@ -12,10 +14,21 @@ end
|
|
|
12
14
|
require "version_gem"
|
|
13
15
|
|
|
14
16
|
# This Gem
|
|
15
|
-
require_relative 'require_bench/version'
|
|
16
17
|
|
|
17
18
|
# Namespace for this gem
|
|
18
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
|
+
|
|
19
32
|
if REQUIRE_BENCH_ENABLED
|
|
20
33
|
TIMINGS = Hash.new { |h, k| h[k] = 0.0 }
|
|
21
34
|
skips = ENV['REQUIRE_BENCH_SKIP_PATTERN']
|
|
@@ -36,10 +49,12 @@ module RequireBench
|
|
|
36
49
|
preferred_grouping = precedence.first
|
|
37
50
|
prefer_not_path = preferred_grouping != 'path' # path correlates to default behavior of regexp matching
|
|
38
51
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
else
|
|
52
|
+
no_color = ENV.key?('NO_COLOR') && !ENV['NO_COLOR'].empty?
|
|
53
|
+
if no_color
|
|
42
54
|
require 'require_bench/printer'
|
|
55
|
+
else
|
|
56
|
+
require 'require_bench/colors'
|
|
57
|
+
require 'require_bench/color_printer'
|
|
43
58
|
end
|
|
44
59
|
PRINTER = Printer.new
|
|
45
60
|
|
|
@@ -94,7 +109,8 @@ module RequireBench
|
|
|
94
109
|
TRACKED_METHODS = tracked_methods
|
|
95
110
|
|
|
96
111
|
def consume_with_timing(type, file, *args)
|
|
97
|
-
|
|
112
|
+
previous_semaphore = Thread.current[SEMAPHORE_KEY]
|
|
113
|
+
Thread.current[SEMAPHORE_KEY] = true
|
|
98
114
|
short_type = type[0]
|
|
99
115
|
ret = nil
|
|
100
116
|
# Not sure if this is actually a useful abstraction...
|
|
@@ -129,58 +145,69 @@ module RequireBench
|
|
|
129
145
|
RequireBench::TIMINGS[prefix] += seconds
|
|
130
146
|
ret
|
|
131
147
|
ensure
|
|
132
|
-
|
|
148
|
+
Thread.current[SEMAPHORE_KEY] = previous_semaphore
|
|
133
149
|
end
|
|
134
150
|
module_function :consume_with_timing
|
|
135
151
|
end
|
|
136
|
-
end
|
|
137
152
|
|
|
138
|
-
if REQUIRE_BENCH_ENABLED
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
_require_bench_consume_file('require', file)
|
|
146
|
-
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
|
|
147
160
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
def load(file, *args)
|
|
162
|
+
_require_bench_consume_file('load', file, *args)
|
|
163
|
+
end
|
|
151
164
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
165
|
+
def require_without_timing(file, *args)
|
|
166
|
+
RequireBench::ORIGINAL_KERNEL_METHODS[:require].bind(self).call(file, *args)
|
|
167
|
+
end
|
|
155
168
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
169
|
+
def load_without_timing(file, *args)
|
|
170
|
+
RequireBench::ORIGINAL_KERNEL_METHODS[:load].bind(self).call(file, *args)
|
|
171
|
+
end
|
|
159
172
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
|
166
188
|
_require_bench_file(type, measure, skippy, file_path, *args)
|
|
167
|
-
rescue *RequireBench::RESCUED_CLASSES => e
|
|
168
|
-
RequireBench::PRINTER.out_error(e, file, short_type, *args)
|
|
169
189
|
end
|
|
170
|
-
else
|
|
171
|
-
_require_bench_file(type, measure, skippy, file_path, *args)
|
|
172
190
|
end
|
|
173
|
-
end
|
|
174
191
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
|
182
200
|
end
|
|
201
|
+
|
|
202
|
+
private :require, :load, :require_without_timing, :load_without_timing,
|
|
203
|
+
:_require_bench_consume_file, :_require_bench_file
|
|
183
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)
|
|
184
211
|
end
|
|
185
212
|
end
|
|
186
213
|
|
data.tar.gz.sig
CHANGED
|
Binary file
|