rbs 0.2.0
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 +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +15 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +9 -0
- data/COPYING +56 -0
- data/Gemfile +6 -0
- data/README.md +93 -0
- data/Rakefile +142 -0
- data/bin/annotate-with-rdoc +157 -0
- data/bin/console +14 -0
- data/bin/query-rdoc +103 -0
- data/bin/setup +10 -0
- data/bin/sort +89 -0
- data/bin/test_runner.rb +16 -0
- data/docs/CONTRIBUTING.md +97 -0
- data/docs/sigs.md +148 -0
- data/docs/stdlib.md +152 -0
- data/docs/syntax.md +528 -0
- data/exe/rbs +7 -0
- data/lib/rbs.rb +64 -0
- data/lib/rbs/ast/annotation.rb +27 -0
- data/lib/rbs/ast/comment.rb +27 -0
- data/lib/rbs/ast/declarations.rb +395 -0
- data/lib/rbs/ast/members.rb +362 -0
- data/lib/rbs/buffer.rb +50 -0
- data/lib/rbs/builtin_names.rb +55 -0
- data/lib/rbs/cli.rb +558 -0
- data/lib/rbs/constant.rb +26 -0
- data/lib/rbs/constant_table.rb +150 -0
- data/lib/rbs/definition.rb +170 -0
- data/lib/rbs/definition_builder.rb +919 -0
- data/lib/rbs/environment.rb +281 -0
- data/lib/rbs/environment_loader.rb +136 -0
- data/lib/rbs/environment_walker.rb +124 -0
- data/lib/rbs/errors.rb +187 -0
- data/lib/rbs/location.rb +102 -0
- data/lib/rbs/method_type.rb +123 -0
- data/lib/rbs/namespace.rb +91 -0
- data/lib/rbs/parser.y +1344 -0
- data/lib/rbs/prototype/rb.rb +553 -0
- data/lib/rbs/prototype/rbi.rb +587 -0
- data/lib/rbs/prototype/runtime.rb +381 -0
- data/lib/rbs/substitution.rb +46 -0
- data/lib/rbs/test.rb +26 -0
- data/lib/rbs/test/errors.rb +61 -0
- data/lib/rbs/test/hook.rb +294 -0
- data/lib/rbs/test/setup.rb +58 -0
- data/lib/rbs/test/spy.rb +325 -0
- data/lib/rbs/test/test_helper.rb +183 -0
- data/lib/rbs/test/type_check.rb +254 -0
- data/lib/rbs/type_name.rb +70 -0
- data/lib/rbs/types.rb +936 -0
- data/lib/rbs/variance_calculator.rb +138 -0
- data/lib/rbs/vendorer.rb +47 -0
- data/lib/rbs/version.rb +3 -0
- data/lib/rbs/writer.rb +269 -0
- data/lib/ruby/signature.rb +7 -0
- data/rbs.gemspec +46 -0
- data/stdlib/abbrev/abbrev.rbs +60 -0
- data/stdlib/base64/base64.rbs +71 -0
- data/stdlib/benchmark/benchmark.rbs +372 -0
- data/stdlib/builtin/array.rbs +1997 -0
- data/stdlib/builtin/basic_object.rbs +280 -0
- data/stdlib/builtin/binding.rbs +177 -0
- data/stdlib/builtin/builtin.rbs +45 -0
- data/stdlib/builtin/class.rbs +145 -0
- data/stdlib/builtin/comparable.rbs +116 -0
- data/stdlib/builtin/complex.rbs +400 -0
- data/stdlib/builtin/constants.rbs +37 -0
- data/stdlib/builtin/data.rbs +5 -0
- data/stdlib/builtin/deprecated.rbs +2 -0
- data/stdlib/builtin/dir.rbs +413 -0
- data/stdlib/builtin/encoding.rbs +607 -0
- data/stdlib/builtin/enumerable.rbs +404 -0
- data/stdlib/builtin/enumerator.rbs +260 -0
- data/stdlib/builtin/errno.rbs +781 -0
- data/stdlib/builtin/errors.rbs +582 -0
- data/stdlib/builtin/exception.rbs +194 -0
- data/stdlib/builtin/false_class.rbs +40 -0
- data/stdlib/builtin/fiber.rbs +68 -0
- data/stdlib/builtin/fiber_error.rbs +12 -0
- data/stdlib/builtin/file.rbs +1076 -0
- data/stdlib/builtin/file_test.rbs +59 -0
- data/stdlib/builtin/float.rbs +696 -0
- data/stdlib/builtin/gc.rbs +243 -0
- data/stdlib/builtin/hash.rbs +1029 -0
- data/stdlib/builtin/integer.rbs +707 -0
- data/stdlib/builtin/io.rbs +683 -0
- data/stdlib/builtin/kernel.rbs +576 -0
- data/stdlib/builtin/marshal.rbs +161 -0
- data/stdlib/builtin/match_data.rbs +271 -0
- data/stdlib/builtin/math.rbs +369 -0
- data/stdlib/builtin/method.rbs +185 -0
- data/stdlib/builtin/module.rbs +1104 -0
- data/stdlib/builtin/nil_class.rbs +82 -0
- data/stdlib/builtin/numeric.rbs +409 -0
- data/stdlib/builtin/object.rbs +824 -0
- data/stdlib/builtin/proc.rbs +429 -0
- data/stdlib/builtin/process.rbs +1227 -0
- data/stdlib/builtin/random.rbs +267 -0
- data/stdlib/builtin/range.rbs +226 -0
- data/stdlib/builtin/rational.rbs +424 -0
- data/stdlib/builtin/rb_config.rbs +57 -0
- data/stdlib/builtin/regexp.rbs +1083 -0
- data/stdlib/builtin/ruby_vm.rbs +14 -0
- data/stdlib/builtin/signal.rbs +55 -0
- data/stdlib/builtin/string.rbs +1901 -0
- data/stdlib/builtin/string_io.rbs +284 -0
- data/stdlib/builtin/struct.rbs +40 -0
- data/stdlib/builtin/symbol.rbs +228 -0
- data/stdlib/builtin/thread.rbs +1108 -0
- data/stdlib/builtin/thread_group.rbs +23 -0
- data/stdlib/builtin/time.rbs +1047 -0
- data/stdlib/builtin/trace_point.rbs +290 -0
- data/stdlib/builtin/true_class.rbs +46 -0
- data/stdlib/builtin/unbound_method.rbs +153 -0
- data/stdlib/builtin/warning.rbs +17 -0
- data/stdlib/coverage/coverage.rbs +62 -0
- data/stdlib/csv/csv.rbs +773 -0
- data/stdlib/erb/erb.rbs +392 -0
- data/stdlib/find/find.rbs +40 -0
- data/stdlib/ipaddr/ipaddr.rbs +247 -0
- data/stdlib/json/json.rbs +335 -0
- data/stdlib/pathname/pathname.rbs +1093 -0
- data/stdlib/prime/integer-extension.rbs +23 -0
- data/stdlib/prime/prime.rbs +188 -0
- data/stdlib/securerandom/securerandom.rbs +9 -0
- data/stdlib/set/set.rbs +301 -0
- data/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +292 -0
data/rbs.gemspec
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "rbs/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "rbs"
|
|
8
|
+
spec.version = RBS::VERSION
|
|
9
|
+
spec.authors = ["Soutaro Matsumoto"]
|
|
10
|
+
spec.email = ["matsumoto@soutaro.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Type signature for Ruby.}
|
|
13
|
+
spec.description = %q{RBS is the language for type signatures for Ruby and standard library definitions.}
|
|
14
|
+
spec.homepage = "https://github.com/ruby/rbs"
|
|
15
|
+
spec.licenses = ['BSD-2-Clause', 'Ruby']
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/ruby/rbs.git"
|
|
22
|
+
spec.metadata["changelog_uri"] = "https://github.com/ruby/rbs/blob/master/CHANGELOG.md"
|
|
23
|
+
else
|
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
25
|
+
"public gem pushes."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
32
|
+
end
|
|
33
|
+
spec.files << "lib/rbs/parser.rb"
|
|
34
|
+
spec.bindir = "exe"
|
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
36
|
+
spec.require_paths = ["lib"]
|
|
37
|
+
|
|
38
|
+
spec.add_development_dependency "bundler"
|
|
39
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
|
40
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
41
|
+
spec.add_development_dependency "racc", "~> 1.4.16"
|
|
42
|
+
spec.add_development_dependency "rubocop"
|
|
43
|
+
spec.add_development_dependency "rubocop-rubycw"
|
|
44
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.3.6"
|
|
45
|
+
spec.add_development_dependency "json", "~> 2.3.0"
|
|
46
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Calculates the set of unambiguous abbreviations for a given set of strings.
|
|
2
|
+
#
|
|
3
|
+
# require 'abbrev'
|
|
4
|
+
# require 'pp'
|
|
5
|
+
#
|
|
6
|
+
# pp Abbrev.abbrev(['ruby'])
|
|
7
|
+
# #=> {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}
|
|
8
|
+
#
|
|
9
|
+
# pp Abbrev.abbrev(%w{ ruby rules })
|
|
10
|
+
#
|
|
11
|
+
# *Generates:*
|
|
12
|
+
# { "ruby" => "ruby",
|
|
13
|
+
# "rub" => "ruby",
|
|
14
|
+
# "rules" => "rules",
|
|
15
|
+
# "rule" => "rules",
|
|
16
|
+
# "rul" => "rules" }
|
|
17
|
+
#
|
|
18
|
+
# It also provides an array core extension, Array#abbrev.
|
|
19
|
+
#
|
|
20
|
+
# pp %w{ summer winter }.abbrev
|
|
21
|
+
#
|
|
22
|
+
# *Generates:*
|
|
23
|
+
# { "summer" => "summer",
|
|
24
|
+
# "summe" => "summer",
|
|
25
|
+
# "summ" => "summer",
|
|
26
|
+
# "sum" => "summer",
|
|
27
|
+
# "su" => "summer",
|
|
28
|
+
# "s" => "summer",
|
|
29
|
+
# "winter" => "winter",
|
|
30
|
+
# "winte" => "winter",
|
|
31
|
+
# "wint" => "winter",
|
|
32
|
+
# "win" => "winter",
|
|
33
|
+
# "wi" => "winter",
|
|
34
|
+
# "w" => "winter" }
|
|
35
|
+
#
|
|
36
|
+
module Abbrev
|
|
37
|
+
# Given a set of strings, calculate the set of unambiguous abbreviations for
|
|
38
|
+
# those strings, and return a hash where the keys are all the possible
|
|
39
|
+
# abbreviations and the values are the full strings.
|
|
40
|
+
#
|
|
41
|
+
# Thus, given `words` is "car" and "cone", the keys pointing to "car" would be
|
|
42
|
+
# "ca" and "car", while those pointing to "cone" would be "co", "con", and
|
|
43
|
+
# "cone".
|
|
44
|
+
#
|
|
45
|
+
# require 'abbrev'
|
|
46
|
+
#
|
|
47
|
+
# Abbrev.abbrev(%w{ car cone })
|
|
48
|
+
# #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
|
|
49
|
+
#
|
|
50
|
+
# The optional `pattern` parameter is a pattern or a string. Only input strings
|
|
51
|
+
# that match the pattern or start with the string are included in the output
|
|
52
|
+
# hash.
|
|
53
|
+
#
|
|
54
|
+
# Abbrev.abbrev(%w{car box cone crab}, /b/)
|
|
55
|
+
# #=> {"box"=>"box", "bo"=>"box", "b"=>"box", "crab" => "crab"}
|
|
56
|
+
#
|
|
57
|
+
# Abbrev.abbrev(%w{car box cone}, 'ca')
|
|
58
|
+
# #=> {"car"=>"car", "ca"=>"car"}
|
|
59
|
+
def self?.abbrev: (Array[String], ?String | Regexp | nil) -> Hash[String, String]
|
|
60
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# The Base64 module provides for the encoding (#encode64, #strict_encode64,
|
|
2
|
+
# #urlsafe_encode64) and decoding (#decode64, #strict_decode64,
|
|
3
|
+
# #urlsafe_decode64) of binary data using a Base64 representation.
|
|
4
|
+
#
|
|
5
|
+
# ## Example
|
|
6
|
+
#
|
|
7
|
+
# A simple encoding and decoding.
|
|
8
|
+
#
|
|
9
|
+
# require "base64"
|
|
10
|
+
#
|
|
11
|
+
# enc = Base64.encode64('Send reinforcements')
|
|
12
|
+
# # -> "U2VuZCByZWluZm9yY2VtZW50cw==\n"
|
|
13
|
+
# plain = Base64.decode64(enc)
|
|
14
|
+
# # -> "Send reinforcements"
|
|
15
|
+
#
|
|
16
|
+
# The purpose of using base64 to encode data is that it translates any binary
|
|
17
|
+
# data into purely printable characters.
|
|
18
|
+
#
|
|
19
|
+
module Base64
|
|
20
|
+
# Returns the Base64-decoded version of `str`. This method complies with RFC
|
|
21
|
+
# 2045. Characters outside the base alphabet are ignored.
|
|
22
|
+
#
|
|
23
|
+
# require 'base64'
|
|
24
|
+
# str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
|
|
25
|
+
# 'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
|
|
26
|
+
# 'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
|
|
27
|
+
# puts Base64.decode64(str)
|
|
28
|
+
#
|
|
29
|
+
# *Generates:*
|
|
30
|
+
#
|
|
31
|
+
# This is line one
|
|
32
|
+
# This is line two
|
|
33
|
+
# This is line three
|
|
34
|
+
# And so on...
|
|
35
|
+
def self?.decode64: (String str) -> String
|
|
36
|
+
|
|
37
|
+
# Returns the Base64-encoded version of `bin`. This method complies with RFC
|
|
38
|
+
# 2045. Line feeds are added to every 60 encoded characters.
|
|
39
|
+
#
|
|
40
|
+
# require 'base64'
|
|
41
|
+
# Base64.encode64("Now is the time for all good coders\nto learn Ruby")
|
|
42
|
+
#
|
|
43
|
+
# *Generates:*
|
|
44
|
+
#
|
|
45
|
+
# Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
|
|
46
|
+
# UnVieQ==
|
|
47
|
+
def self?.encode64: (String bin) -> String
|
|
48
|
+
|
|
49
|
+
# Returns the Base64-decoded version of `str`. This method complies with RFC
|
|
50
|
+
# 4648. ArgumentError is raised if `str` is incorrectly padded or contains
|
|
51
|
+
# non-alphabet characters. Note that CR or LF are also rejected.
|
|
52
|
+
def self?.strict_decode64: (String str) -> String
|
|
53
|
+
|
|
54
|
+
# Returns the Base64-encoded version of `bin`. This method complies with RFC
|
|
55
|
+
# 4648. No line feeds are added.
|
|
56
|
+
def self?.strict_encode64: (String bin) -> String
|
|
57
|
+
|
|
58
|
+
# Returns the Base64-decoded version of `str`. This method complies with ``Base
|
|
59
|
+
# 64 Encoding with URL and Filename Safe Alphabet'' in RFC 4648. The alphabet
|
|
60
|
+
# uses '-' instead of '+' and '_' instead of '/'.
|
|
61
|
+
#
|
|
62
|
+
# The padding character is optional. This method accepts both correctly-padded
|
|
63
|
+
# and unpadded input. Note that it still rejects incorrectly-padded input.
|
|
64
|
+
def self?.urlsafe_decode64: (String str) -> String
|
|
65
|
+
|
|
66
|
+
# Returns the Base64-encoded version of `bin`. This method complies with ``Base
|
|
67
|
+
# 64 Encoding with URL and Filename Safe Alphabet'' in RFC 4648. The alphabet
|
|
68
|
+
# uses '-' instead of '+' and '_' instead of '/'. Note that the result can still
|
|
69
|
+
# contain '='. You can remove the padding by setting `padding` as false.
|
|
70
|
+
def self?.urlsafe_encode64: (String bin, ?padding: bool) -> String
|
|
71
|
+
end
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# The Benchmark module provides methods to measure and report the time used to
|
|
2
|
+
# execute Ruby code.
|
|
3
|
+
#
|
|
4
|
+
# * Measure the time to construct the string given by the expression
|
|
5
|
+
# `"a"*1_000_000_000`:
|
|
6
|
+
#
|
|
7
|
+
# require 'benchmark'
|
|
8
|
+
#
|
|
9
|
+
# puts Benchmark.measure { "a"*1_000_000_000 }
|
|
10
|
+
#
|
|
11
|
+
# On my machine (OSX 10.8.3 on i5 1.7 GHz) this generates:
|
|
12
|
+
#
|
|
13
|
+
# 0.350000 0.400000 0.750000 ( 0.835234)
|
|
14
|
+
#
|
|
15
|
+
# This report shows the user CPU time, system CPU time, the sum of the user
|
|
16
|
+
# and system CPU times, and the elapsed real time. The unit of time is
|
|
17
|
+
# seconds.
|
|
18
|
+
#
|
|
19
|
+
# * Do some experiments sequentially using the #bm method:
|
|
20
|
+
#
|
|
21
|
+
# require 'benchmark'
|
|
22
|
+
#
|
|
23
|
+
# n = 5000000
|
|
24
|
+
# Benchmark.bm do |x|
|
|
25
|
+
# x.report { for i in 1..n; a = "1"; end }
|
|
26
|
+
# x.report { n.times do ; a = "1"; end }
|
|
27
|
+
# x.report { 1.upto(n) do ; a = "1"; end }
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# The result:
|
|
31
|
+
#
|
|
32
|
+
# user system total real
|
|
33
|
+
# 1.010000 0.000000 1.010000 ( 1.014479)
|
|
34
|
+
# 1.000000 0.000000 1.000000 ( 0.998261)
|
|
35
|
+
# 0.980000 0.000000 0.980000 ( 0.981335)
|
|
36
|
+
#
|
|
37
|
+
# * Continuing the previous example, put a label in each report:
|
|
38
|
+
#
|
|
39
|
+
# require 'benchmark'
|
|
40
|
+
#
|
|
41
|
+
# n = 5000000
|
|
42
|
+
# Benchmark.bm(7) do |x|
|
|
43
|
+
# x.report("for:") { for i in 1..n; a = "1"; end }
|
|
44
|
+
# x.report("times:") { n.times do ; a = "1"; end }
|
|
45
|
+
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
|
46
|
+
# end
|
|
47
|
+
#
|
|
48
|
+
#
|
|
49
|
+
# The result:
|
|
50
|
+
#
|
|
51
|
+
# user system total real
|
|
52
|
+
# for: 1.010000 0.000000 1.010000 ( 1.015688)
|
|
53
|
+
# times: 1.000000 0.000000 1.000000 ( 1.003611)
|
|
54
|
+
# upto: 1.030000 0.000000 1.030000 ( 1.028098)
|
|
55
|
+
#
|
|
56
|
+
# * The times for some benchmarks depend on the order in which items are run.
|
|
57
|
+
# These differences are due to the cost of memory allocation and garbage
|
|
58
|
+
# collection. To avoid these discrepancies, the #bmbm method is provided.
|
|
59
|
+
# For example, to compare ways to sort an array of floats:
|
|
60
|
+
#
|
|
61
|
+
# require 'benchmark'
|
|
62
|
+
#
|
|
63
|
+
# array = (1..1000000).map { rand }
|
|
64
|
+
#
|
|
65
|
+
# Benchmark.bmbm do |x|
|
|
66
|
+
# x.report("sort!") { array.dup.sort! }
|
|
67
|
+
# x.report("sort") { array.dup.sort }
|
|
68
|
+
# end
|
|
69
|
+
#
|
|
70
|
+
# The result:
|
|
71
|
+
#
|
|
72
|
+
# Rehearsal -----------------------------------------
|
|
73
|
+
# sort! 1.490000 0.010000 1.500000 ( 1.490520)
|
|
74
|
+
# sort 1.460000 0.000000 1.460000 ( 1.463025)
|
|
75
|
+
# -------------------------------- total: 2.960000sec
|
|
76
|
+
#
|
|
77
|
+
# user system total real
|
|
78
|
+
# sort! 1.460000 0.000000 1.460000 ( 1.460465)
|
|
79
|
+
# sort 1.450000 0.010000 1.460000 ( 1.448327)
|
|
80
|
+
#
|
|
81
|
+
# * Report statistics of sequential experiments with unique labels, using the
|
|
82
|
+
# #benchmark method:
|
|
83
|
+
#
|
|
84
|
+
# require 'benchmark'
|
|
85
|
+
# include Benchmark # we need the CAPTION and FORMAT constants
|
|
86
|
+
#
|
|
87
|
+
# n = 5000000
|
|
88
|
+
# Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
|
|
89
|
+
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
|
|
90
|
+
# tt = x.report("times:") { n.times do ; a = "1"; end }
|
|
91
|
+
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
|
92
|
+
# [tf+tt+tu, (tf+tt+tu)/3]
|
|
93
|
+
# end
|
|
94
|
+
#
|
|
95
|
+
# The result:
|
|
96
|
+
#
|
|
97
|
+
# user system total real
|
|
98
|
+
# for: 0.950000 0.000000 0.950000 ( 0.952039)
|
|
99
|
+
# times: 0.980000 0.000000 0.980000 ( 0.984938)
|
|
100
|
+
# upto: 0.950000 0.000000 0.950000 ( 0.946787)
|
|
101
|
+
# >total: 2.880000 0.000000 2.880000 ( 2.883764)
|
|
102
|
+
# >avg: 0.960000 0.000000 0.960000 ( 0.961255)
|
|
103
|
+
#
|
|
104
|
+
#
|
|
105
|
+
module Benchmark
|
|
106
|
+
# Invokes the block with a Benchmark::Report object, which may be used to
|
|
107
|
+
# collect and report on the results of individual benchmark tests. Reserves
|
|
108
|
+
# `label_width` leading spaces for labels on each line. Prints `caption` at the
|
|
109
|
+
# top of the report, and uses `format` to format each line. Returns an array of
|
|
110
|
+
# Benchmark::Tms objects.
|
|
111
|
+
#
|
|
112
|
+
# If the block returns an array of Benchmark::Tms objects, these will be used to
|
|
113
|
+
# format additional lines of output. If `labels` parameter are given, these are
|
|
114
|
+
# used to label these extra lines.
|
|
115
|
+
#
|
|
116
|
+
# *Note*: Other methods provide a simpler interface to this one, and are
|
|
117
|
+
# suitable for nearly all benchmarking requirements. See the examples in
|
|
118
|
+
# Benchmark, and the #bm and #bmbm methods.
|
|
119
|
+
#
|
|
120
|
+
# Example:
|
|
121
|
+
#
|
|
122
|
+
# require 'benchmark'
|
|
123
|
+
# include Benchmark # we need the CAPTION and FORMAT constants
|
|
124
|
+
#
|
|
125
|
+
# n = 5000000
|
|
126
|
+
# Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
|
|
127
|
+
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
|
|
128
|
+
# tt = x.report("times:") { n.times do ; a = "1"; end }
|
|
129
|
+
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
|
130
|
+
# [tf+tt+tu, (tf+tt+tu)/3]
|
|
131
|
+
# end
|
|
132
|
+
#
|
|
133
|
+
# Generates:
|
|
134
|
+
#
|
|
135
|
+
# user system total real
|
|
136
|
+
# for: 0.970000 0.000000 0.970000 ( 0.970493)
|
|
137
|
+
# times: 0.990000 0.000000 0.990000 ( 0.989542)
|
|
138
|
+
# upto: 0.970000 0.000000 0.970000 ( 0.972854)
|
|
139
|
+
# >total: 2.930000 0.000000 2.930000 ( 2.932889)
|
|
140
|
+
# >avg: 0.976667 0.000000 0.976667 ( 0.977630)
|
|
141
|
+
#
|
|
142
|
+
def self?.benchmark: (String caption, ?Integer? label_width, ?String? format, *String labels) { (Report report) -> (Array[Tms] | void) } -> ::Array[Benchmark::Tms]
|
|
143
|
+
|
|
144
|
+
# A simple interface to the #benchmark method, #bm generates sequential reports
|
|
145
|
+
# with labels. `label_width` and `labels` parameters have the same meaning as
|
|
146
|
+
# for #benchmark.
|
|
147
|
+
#
|
|
148
|
+
# require 'benchmark'
|
|
149
|
+
#
|
|
150
|
+
# n = 5000000
|
|
151
|
+
# Benchmark.bm(7) do |x|
|
|
152
|
+
# x.report("for:") { for i in 1..n; a = "1"; end }
|
|
153
|
+
# x.report("times:") { n.times do ; a = "1"; end }
|
|
154
|
+
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
|
155
|
+
# end
|
|
156
|
+
#
|
|
157
|
+
# Generates:
|
|
158
|
+
#
|
|
159
|
+
# user system total real
|
|
160
|
+
# for: 0.960000 0.000000 0.960000 ( 0.957966)
|
|
161
|
+
# times: 0.960000 0.000000 0.960000 ( 0.960423)
|
|
162
|
+
# upto: 0.950000 0.000000 0.950000 ( 0.954864)
|
|
163
|
+
#
|
|
164
|
+
def self?.bm: (?Integer label_width, *String labels) { (Report report) -> void } -> ::Array[Benchmark::Tms]
|
|
165
|
+
|
|
166
|
+
# Sometimes benchmark results are skewed because code executed earlier
|
|
167
|
+
# encounters different garbage collection overheads than that run later. #bmbm
|
|
168
|
+
# attempts to minimize this effect by running the tests twice, the first time as
|
|
169
|
+
# a rehearsal in order to get the runtime environment stable, the second time
|
|
170
|
+
# for real. GC.start is executed before the start of each of the real timings;
|
|
171
|
+
# the cost of this is not included in the timings. In reality, though, there's
|
|
172
|
+
# only so much that #bmbm can do, and the results are not guaranteed to be
|
|
173
|
+
# isolated from garbage collection and other effects.
|
|
174
|
+
#
|
|
175
|
+
# Because #bmbm takes two passes through the tests, it can calculate the
|
|
176
|
+
# required label width.
|
|
177
|
+
#
|
|
178
|
+
# require 'benchmark'
|
|
179
|
+
#
|
|
180
|
+
# array = (1..1000000).map { rand }
|
|
181
|
+
#
|
|
182
|
+
# Benchmark.bmbm do |x|
|
|
183
|
+
# x.report("sort!") { array.dup.sort! }
|
|
184
|
+
# x.report("sort") { array.dup.sort }
|
|
185
|
+
# end
|
|
186
|
+
#
|
|
187
|
+
# Generates:
|
|
188
|
+
#
|
|
189
|
+
# Rehearsal -----------------------------------------
|
|
190
|
+
# sort! 1.440000 0.010000 1.450000 ( 1.446833)
|
|
191
|
+
# sort 1.440000 0.000000 1.440000 ( 1.448257)
|
|
192
|
+
# -------------------------------- total: 2.890000sec
|
|
193
|
+
#
|
|
194
|
+
# user system total real
|
|
195
|
+
# sort! 1.460000 0.000000 1.460000 ( 1.458065)
|
|
196
|
+
# sort 1.450000 0.000000 1.450000 ( 1.455963)
|
|
197
|
+
#
|
|
198
|
+
# #bmbm yields a Benchmark::Job object and returns an array of Benchmark::Tms
|
|
199
|
+
# objects.
|
|
200
|
+
#
|
|
201
|
+
def self?.bmbm: (?Integer width) { (Job job) -> void } -> ::Array[Benchmark::Tms]
|
|
202
|
+
|
|
203
|
+
# Returns the time used to execute the given block as a Benchmark::Tms object.
|
|
204
|
+
# Takes `label` option.
|
|
205
|
+
#
|
|
206
|
+
# require 'benchmark'
|
|
207
|
+
#
|
|
208
|
+
# n = 1000000
|
|
209
|
+
#
|
|
210
|
+
# time = Benchmark.measure do
|
|
211
|
+
# n.times { a = "1" }
|
|
212
|
+
# end
|
|
213
|
+
# puts time
|
|
214
|
+
#
|
|
215
|
+
# Generates:
|
|
216
|
+
#
|
|
217
|
+
# 0.220000 0.000000 0.220000 ( 0.227313)
|
|
218
|
+
#
|
|
219
|
+
def self?.measure: (?String label) { () -> void } -> Benchmark::Tms
|
|
220
|
+
|
|
221
|
+
# Returns the elapsed real time used to execute the given block.
|
|
222
|
+
#
|
|
223
|
+
def self?.realtime: () { () -> void } -> Float
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
Benchmark::BENCHMARK_VERSION: String
|
|
227
|
+
|
|
228
|
+
# The default caption string (heading above the output times).
|
|
229
|
+
#
|
|
230
|
+
Benchmark::CAPTION: String
|
|
231
|
+
|
|
232
|
+
# The default format string used to display times. See also
|
|
233
|
+
# Benchmark::Tms#format.
|
|
234
|
+
#
|
|
235
|
+
Benchmark::FORMAT: String
|
|
236
|
+
|
|
237
|
+
class Benchmark::Job
|
|
238
|
+
# Prints the `label` and measured time for the block,
|
|
239
|
+
# formatted by `format`. See Tms#format for the
|
|
240
|
+
# formatting rules.
|
|
241
|
+
def item: (?String label) { () -> void } -> self
|
|
242
|
+
|
|
243
|
+
# An array of 2-element arrays, consisting of label and block pairs.
|
|
244
|
+
def list: () -> ::Array[untyped]
|
|
245
|
+
|
|
246
|
+
alias report item
|
|
247
|
+
|
|
248
|
+
# Length of the widest label in the #list.
|
|
249
|
+
def width: () -> Integer
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
class Benchmark::Report
|
|
253
|
+
# Prints the `label` and measured time for the block,
|
|
254
|
+
# formatted by `format`. See Tms#format for the
|
|
255
|
+
# formatting rules.
|
|
256
|
+
def item: (?String label, *untyped format) { () -> void } -> Tms
|
|
257
|
+
|
|
258
|
+
# An array of Benchmark::Tms objects representing each item.
|
|
259
|
+
def list: () -> ::Array[Benchmark::Tms]
|
|
260
|
+
|
|
261
|
+
alias report item
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# A data object, representing the times associated with a benchmark measurement.
|
|
265
|
+
#
|
|
266
|
+
class Benchmark::Tms
|
|
267
|
+
# Returns a new Tms object obtained by memberwise multiplication of the
|
|
268
|
+
# individual times for this Tms object by `x`.
|
|
269
|
+
#
|
|
270
|
+
def *: (untyped x) -> untyped
|
|
271
|
+
|
|
272
|
+
# Returns a new Tms object obtained by memberwise summation of the individual
|
|
273
|
+
# times for this Tms object with those of the `other` Tms object. This method
|
|
274
|
+
# and #/() are useful for taking statistics.
|
|
275
|
+
#
|
|
276
|
+
def +: (untyped other) -> untyped
|
|
277
|
+
|
|
278
|
+
# Returns a new Tms object obtained by memberwise subtraction of the individual
|
|
279
|
+
# times for the `other` Tms object from those of this Tms object.
|
|
280
|
+
#
|
|
281
|
+
def -: (untyped other) -> untyped
|
|
282
|
+
|
|
283
|
+
# Returns a new Tms object obtained by memberwise division of the individual
|
|
284
|
+
# times for this Tms object by `x`. This method and #+() are useful for taking
|
|
285
|
+
# statistics.
|
|
286
|
+
#
|
|
287
|
+
def /: (untyped x) -> untyped
|
|
288
|
+
|
|
289
|
+
# Returns a new Tms object whose times are the sum of the times for this Tms
|
|
290
|
+
# object, plus the time required to execute the code block (`blk`).
|
|
291
|
+
#
|
|
292
|
+
def add: () { (*untyped) -> untyped } -> untyped
|
|
293
|
+
|
|
294
|
+
# An in-place version of #add. Changes the times of this Tms object by making it
|
|
295
|
+
# the sum of the times for this Tms object, plus the time required to execute
|
|
296
|
+
# the code block (`blk`).
|
|
297
|
+
#
|
|
298
|
+
def add!: () { (*untyped) -> untyped } -> untyped
|
|
299
|
+
|
|
300
|
+
# System CPU time of children
|
|
301
|
+
#
|
|
302
|
+
def cstime: () -> Float
|
|
303
|
+
|
|
304
|
+
# User CPU time of children
|
|
305
|
+
#
|
|
306
|
+
def cutime: () -> Float
|
|
307
|
+
|
|
308
|
+
# Returns the contents of this Tms object as a formatted string, according to a
|
|
309
|
+
# `format` string like that passed to Kernel.format. In addition, #format
|
|
310
|
+
# accepts the following extensions:
|
|
311
|
+
#
|
|
312
|
+
# `%u`
|
|
313
|
+
# : Replaced by the user CPU time, as reported by Tms#utime.
|
|
314
|
+
# `%y`
|
|
315
|
+
# : Replaced by the system CPU time, as reported by #stime (Mnemonic: y of
|
|
316
|
+
# "s*y*stem")
|
|
317
|
+
# `%U`
|
|
318
|
+
# : Replaced by the children's user CPU time, as reported by Tms#cutime
|
|
319
|
+
# `%Y`
|
|
320
|
+
# : Replaced by the children's system CPU time, as reported by Tms#cstime
|
|
321
|
+
# `%t`
|
|
322
|
+
# : Replaced by the total CPU time, as reported by Tms#total
|
|
323
|
+
# `%r`
|
|
324
|
+
# : Replaced by the elapsed real time, as reported by Tms#real
|
|
325
|
+
# `%n`
|
|
326
|
+
# : Replaced by the label string, as reported by Tms#label (Mnemonic: n of
|
|
327
|
+
# "*n*ame")
|
|
328
|
+
#
|
|
329
|
+
#
|
|
330
|
+
# If `format` is not given, FORMAT is used as default value, detailing the user,
|
|
331
|
+
# system and real elapsed time.
|
|
332
|
+
#
|
|
333
|
+
def format: (?String format, *untyped args) -> String
|
|
334
|
+
|
|
335
|
+
# Label
|
|
336
|
+
#
|
|
337
|
+
def label: () -> String
|
|
338
|
+
|
|
339
|
+
# Elapsed real time
|
|
340
|
+
#
|
|
341
|
+
def real: () -> Float
|
|
342
|
+
|
|
343
|
+
# System CPU time
|
|
344
|
+
#
|
|
345
|
+
def stime: () -> Float
|
|
346
|
+
|
|
347
|
+
# Returns a new 6-element array, consisting of the label, user CPU time, system
|
|
348
|
+
# CPU time, children's user CPU time, children's system CPU time and elapsed
|
|
349
|
+
# real time.
|
|
350
|
+
#
|
|
351
|
+
def to_a: () -> untyped
|
|
352
|
+
|
|
353
|
+
# Same as #format.
|
|
354
|
+
#
|
|
355
|
+
def to_s: () -> String
|
|
356
|
+
|
|
357
|
+
# Total time, that is `utime` + `stime` + `cutime` + `cstime`
|
|
358
|
+
#
|
|
359
|
+
def total: () -> Float
|
|
360
|
+
|
|
361
|
+
# User CPU time
|
|
362
|
+
#
|
|
363
|
+
def utime: () -> Float
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
# Default caption, see also Benchmark::CAPTION
|
|
367
|
+
#
|
|
368
|
+
Benchmark::Tms::CAPTION: String
|
|
369
|
+
|
|
370
|
+
# Default format string, see also Benchmark::FORMAT
|
|
371
|
+
#
|
|
372
|
+
Benchmark::Tms::FORMAT: String
|