mila 0.0.0.1
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/lib/mila/benchmark/mixin.rb +132 -0
- data/lib/mila/benchmark/reporter_proxy.rb +27 -0
- data/lib/mila/benchmark.rb +5 -0
- data/lib/mila/version.rb +5 -0
- data/lib/mila.rb +13 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2b89383fbb003a741def05de5f34ca654362851881183115b833f11a30a4b25
|
4
|
+
data.tar.gz: 7474ae5557ff89035b779d40e2ef4649cf0c8557ab23e76e425b976e4915812f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f5b9f2acebf66b415e83acc4355ddeec1de1be1b44fcb8e1c38070944179498d00510a6142cf3b49e8543f5288bac946d6ef827db04721d08896a03ca563f3f1
|
7
|
+
data.tar.gz: f69f7d69e03430ee8bb5bad41bffbcb14687aa1b7c7c6aa467947a21c618a92d9a73e93682376c1fe219b14de4c5970790075410d8518599e52bfc0ad597fa27
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module Mila
|
2
|
+
module Benchmark
|
3
|
+
module Mixin
|
4
|
+
|
5
|
+
module JS
|
6
|
+
module Body
|
7
|
+
class Inline
|
8
|
+
def initialize(body = '')
|
9
|
+
@body = body
|
10
|
+
end
|
11
|
+
|
12
|
+
def open(&block)
|
13
|
+
@body = block.call
|
14
|
+
end
|
15
|
+
|
16
|
+
def entrypoint_name
|
17
|
+
raise 'Cannot call entrypoint_name on inline code'
|
18
|
+
end
|
19
|
+
|
20
|
+
def load
|
21
|
+
@body
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class V8Session
|
28
|
+
def initialize
|
29
|
+
@queue = Queue.new
|
30
|
+
@thread = Thread.new { create_context }
|
31
|
+
@body = nil
|
32
|
+
@has_source = false
|
33
|
+
end
|
34
|
+
|
35
|
+
def context
|
36
|
+
@context ||= @queue.pop
|
37
|
+
end
|
38
|
+
|
39
|
+
def eval_inline(&block)
|
40
|
+
@body = JS::Body::Inline.new
|
41
|
+
@body.open(&block)
|
42
|
+
add_source(@body)
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_bundleable_lib(lib)
|
46
|
+
raise ArgumentError if lib.nil?
|
47
|
+
|
48
|
+
@body = JS::Body::Library.new(lib)
|
49
|
+
add_source(@body)
|
50
|
+
end
|
51
|
+
|
52
|
+
def call(*args)
|
53
|
+
case args
|
54
|
+
in [function_name, args_array]
|
55
|
+
function_name = function_name.to_s
|
56
|
+
in [args_array]
|
57
|
+
function_name = @body.entrypoint_name
|
58
|
+
else
|
59
|
+
raise ArgumentError, 'Invalid arguments'
|
60
|
+
end
|
61
|
+
context.call(function_name, args_array)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def add_source(body)
|
67
|
+
raise ArgumentError, 'Cannot add source to a session with a body' if @has_source
|
68
|
+
|
69
|
+
context.eval(body.load)
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_context
|
73
|
+
@queue << MiniRacer::Context.new
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def benchmark(label = 'benchmark', &block)
|
78
|
+
param_count = block.arity
|
79
|
+
|
80
|
+
if param_count == 0
|
81
|
+
return simple_benchmark(label, &block)
|
82
|
+
end
|
83
|
+
|
84
|
+
reporter_benchmark(&block)
|
85
|
+
end
|
86
|
+
|
87
|
+
def ips(time = 5, warmup = 2, quiet = false, &block)
|
88
|
+
puts build_header(block)
|
89
|
+
require 'benchmark/ips'
|
90
|
+
::Benchmark.ips(time: time, warmup: warmup, quiet: quiet) do |job|
|
91
|
+
proxy = ReporterProxy.new(job)
|
92
|
+
block.call(proxy)
|
93
|
+
job.compare!
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def reporter_benchmark(&block)
|
100
|
+
benchmark_results = nil
|
101
|
+
enum = Enumerator.new do |y|
|
102
|
+
benchmark_results = ::Benchmark.bm do |reporter|
|
103
|
+
proxy = ReporterProxy.new(reporter, yielder: y)
|
104
|
+
block.call(proxy)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
print build_header(block)
|
109
|
+
enum.count #eager load the enum
|
110
|
+
puts benchmark_results
|
111
|
+
enum.rewind
|
112
|
+
enum
|
113
|
+
end
|
114
|
+
|
115
|
+
def build_header(block)
|
116
|
+
source_location = block.binding.source_location
|
117
|
+
file = source_location[0]
|
118
|
+
line = source_location[1]
|
119
|
+
"Benchmarking #{file}:#{line}"
|
120
|
+
end
|
121
|
+
|
122
|
+
def simple_benchmark(label, &block)
|
123
|
+
result = nil
|
124
|
+
time = ::Benchmark.measure(label) do
|
125
|
+
result = block.call
|
126
|
+
end
|
127
|
+
puts time
|
128
|
+
result
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mila
|
2
|
+
module Benchmark
|
3
|
+
class ReporterProxy < SimpleDelegator
|
4
|
+
def initialize(obj, yielder: nil)
|
5
|
+
super(obj)
|
6
|
+
@count = 1
|
7
|
+
@yielder = yielder
|
8
|
+
end
|
9
|
+
|
10
|
+
def report(name = "trial #{@count}", &block)
|
11
|
+
__getobj__.report(name) do
|
12
|
+
begin
|
13
|
+
result = block.call
|
14
|
+
@yielder << result if @yielder
|
15
|
+
rescue => e
|
16
|
+
real_backtrace = e.backtrace.reject { |line| line =~ /lib\/mila/ }
|
17
|
+
e.set_backtrace(real_backtrace)
|
18
|
+
raise e
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@count += 1
|
22
|
+
end
|
23
|
+
|
24
|
+
alias r report
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/mila/version.rb
ADDED
data/lib/mila.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'zeitwerk'
|
5
|
+
|
6
|
+
autoload :MiniRacer, 'mini_racer'
|
7
|
+
autoload :Benchmark, 'benchmark'
|
8
|
+
loader = Zeitwerk::Loader.for_gem
|
9
|
+
loader.setup
|
10
|
+
|
11
|
+
module Mila
|
12
|
+
class Error < StandardError; end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mila
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- " David Gillis"
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-03-05 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: benchmark-ips
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: zeitwerk
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
email:
|
41
|
+
- david@flipmine.com
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- lib/mila.rb
|
47
|
+
- lib/mila/benchmark.rb
|
48
|
+
- lib/mila/benchmark/mixin.rb
|
49
|
+
- lib/mila/benchmark/reporter_proxy.rb
|
50
|
+
- lib/mila/version.rb
|
51
|
+
homepage: https://github.com/gillisd/mila
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
allowed_push_host: https://rubygems.org
|
56
|
+
homepage_uri: https://github.com/gillisd/mila
|
57
|
+
source_code_uri: https://github.com/gillisd/mila
|
58
|
+
changelog_uri: https://github.com/gillisd/mila
|
59
|
+
rubygems_mfa_required: 'true'
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 2.7.5
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.6.5
|
75
|
+
specification_version: 4
|
76
|
+
summary: A 'personal assistant' supplying utilities and libraries for general Ruby
|
77
|
+
and Bash use.
|
78
|
+
test_files: []
|