forked from kostya/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.rb
64 lines (54 loc) · 1.58 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'base64'
require 'socket'
def notify(msg)
Socket.tcp('localhost', 9001) { |s| s.puts msg }
rescue SystemCallError
# standalone usage
end
if __FILE__ == $PROGRAM_NAME
[['hello', 'aGVsbG8='], ['world', 'd29ybGQ=']].each do |(src, dst)|
encoded = Base64.strict_encode64(src)
if encoded != dst
warn "#{encoded} != #{dst}"
exit(1)
end
decoded = Base64.strict_decode64(dst)
if decoded != src
warn "#{decoded} != #{src}"
exit(1)
end
end
STR_SIZE = 131_072
TRIES = 8192
str = 'a' * STR_SIZE
str2 = Base64.strict_encode64(str)
str3 = Base64.strict_decode64(str2)
engine = RUBY_ENGINE
if engine == 'truffleruby'
desc = RUBY_DESCRIPTION
if desc.include?('Native')
engine = 'Ruby/truffleruby'
elsif desc.include?('JVM')
engine = 'Ruby/truffleruby (JVM)'
end
elsif engine == 'ruby' && RubyVM::RJIT.enabled?
engine = 'Ruby (--jit)'
end
notify("#{engine}\t#{Process.pid}")
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
s_encoded = 0
TRIES.times do
s_encoded += Base64.strict_encode64(str).bytesize
end
t_encoded = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
s_decoded = 0
TRIES.times do
s_decoded += Base64.strict_decode64(str2).bytesize
end
t_decoded = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t
notify('stop')
puts "encode #{str[0..3]}... to #{str2[0..3]}...: #{s_encoded}, #{t_encoded}"
puts "decode #{str2[0..3]}... to #{str3[0..3]}...: #{s_decoded}, #{t_decoded}"
end