Skip to content

Instantly share code, notes, and snippets.

@noteflakes
Last active January 23, 2026 08:46
Show Gist options
  • Select an option

  • Save noteflakes/c58d4a5e7ab8f167f54bedc325fec7e4 to your computer and use it in GitHub Desktop.

Select an option

Save noteflakes/c58d4a5e7ab8f167f54bedc325fec7e4 to your computer and use it in GitHub Desktop.
Ruby OpenSSL custom BIO benchmark
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
gem 'openssl', path: '..'
gem 'test-unit'
gem 'test-unit-ruby-core'
gem 'uringmachine'
gem 'benchmark'
gem 'benchmark-ips'
gem 'localhost'
end
require 'uringmachine'
require 'benchmark/ips'
require 'localhost/authority'
authority = Localhost::Authority.fetch
ctx = authority.server_context
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
Socket.do_not_reverse_lookup = true
tcps = TCPServer.new("127.0.0.1", 0)
port = tcps.connect_address.ip_port
ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
Thread.new do
Thread.current.report_on_exception = false
loop do
begin
ssl = ssls.accept
rescue OpenSSL::SSL::SSLError, IOError, Errno::EBADF, Errno::EINVAL,
Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET
retry
end
Thread.new do
Thread.current.report_on_exception = false
begin
while line = ssl.gets
ssl.write(line)
end
ensure
ssl.close
end
true
end
end
end
@ssl_bio_default = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port)
@ssl_bio_default.sync_close = true
@ssl_bio_default.connect
@ssl_bio_io = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port)
@ssl_bio_io.sync_close = true
@ssl_bio_io.bio_method = @ssl_bio_io.to_io
@ssl_bio_io.connect
@ssl_bio_um = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port)
@ssl_bio_um.sync_close = true
um = UM.new
fd = @ssl_bio_um.to_io.fileno
um_bio_method = [
->(buf, maxlen) { um.recv(fd, buf, maxlen, 0) },
->(buf, len) { um.send(fd, buf, len, UM::MSG_WAITALL) }
]
@ssl_bio_um.bio_method = um_bio_method
@ssl_bio_um.connect
@msg = 'abc' * 1000
def do_io(ssl)
ssl.puts @msg
ssl.gets
end
Benchmark.ips do |x|
x.report('default') { do_io(@ssl_bio_default) }
x.report('BIO: IO') { do_io(@ssl_bio_io) }
x.report('BIO: UM') { do_io(@ssl_bio_um) }
x.compare!(order: :baseline)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment