Skip to content

Instantly share code, notes, and snippets.

@fudanchii
Last active March 28, 2019 09:40
Show Gist options
  • Select an option

  • Save fudanchii/6f7b4e0e8eb841554319b35c8c0d7af2 to your computer and use it in GitHub Desktop.

Select an option

Save fudanchii/6f7b4e0e8eb841554319b35c8c0d7af2 to your computer and use it in GitHub Desktop.
rbnacl mem benchmark
require "bundler/setup"
require "benchmark/memory"
require "base64"
require 'rbnacl'
a = nil
b = nil
def file_read_chunks(filename, chunksize)
counter = 0
File.open(filename) do |file|
until file.eof?
yield file.read(chunksize)
counter += 1
if counter == 10
GC.start
counter = 0
end
end
end
end
Benchmark.memory do |x|
x.report("with update") do
blake2b = RbNaCl::Hash::Blake2b.new
file_read_chunks("./_random.txt", 64*1024) { |chunk| blake2b << chunk }
a = blake2b.digest
end
x.report("slurp") { b = RbNaCl::Hash.blake2b(File.read("./_random.txt")) }
x.compare!
end
puts "is hash equal?\n#{Base64.strict_encode64(a)}\n#{Base64.strict_encode64(b)}"
#! /usr/bin/env bash
[[ -f _random.txt ]] || {
printf "This script will write a random 1GB file to the current dir,\n"
printf "You have 10 seconds to press Ctrl-C if you want to stop now...\n"
sleep 10
echo "writing test file..."
openssl rand -out _random.txt -base64 $(( 2**30 * 3/4 ))
}
ruby bench.rb
source "https://rubygems.org"
gem 'rbnacl'
gem 'allocation_stats'
gem 'benchmark-memory'
Calculating -------------------------------------
with update 8.456k memsize ( 0.000 retained)
5.000 objects ( 0.000 retained)
1.000 strings ( 0.000 retained)
slurp 1.091B memsize ( 85.000 retained)
11.000 objects ( 1.000 retained)
3.000 strings ( 1.000 retained)
Comparison:
with update: 8456 allocated
slurp: 1090527670 allocated - 128964.96x more
is hash equal?
p7OrzT87bgTbcjY7eN++aN0zlB/GEy5jphFHW7zWVTWIGBzX2YcXUsKZJTS+Dn3OZpvJhhlOfWrTpwgEpTTK7A==
p7OrzT87bgTbcjY7eN++aN0zlB/GEy5jphFHW7zWVTWIGBzX2YcXUsKZJTS+Dn3OZpvJhhlOfWrTpwgEpTTK7A==
require "bundler/setup"
require "rbnacl"
require "allocation_stats"
stats = AllocationStats.trace do
RbNaCl::Hash.blake2b(File.read("./_random.txt"))
end
puts stats.allocations(alias_paths: true).to_text
require "bundler/setup"
require "rbnacl"
require "allocation_stats"
def file_read_chunks(filename, chunksize)
counter = 0
File.open(filename) do |file|
until file.eof?
yield file.read(chunksize)
counter += 1
if counter == 10
GC.start
counter = 0
end
end
end
end
stats = AllocationStats.trace do
blake2b = RbNaCl::Hash::Blake2b.new
file_read_chunks("./_random.txt", 64*1024) { |chunk| blake2b << chunk }
a = blake2b.digest
end
puts stats.allocations(alias_paths: true).to_text
$ bundle exec ruby stats.rb
sourcefile sourceline class_path method_id memsize class
--------------------------------------------- ---------- ---------- --------- ---------- ------
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 63 digest 40 Array
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 63 digest 40 Array
<GEM:rbnacl-6.0.1>/lib/rbnacl/util.rb 23 String * 105 String
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash.rb 65 blake2b 192 Hash
stats.rb 6 read 1090519081 String
stats.rb 6 read 232 File
stats.rb 6 read 40 String
stats.rb 6 40 String
$ bundle exec ruby stats_chunk.rb
sourcefile sourceline class_path method_id memsize class
--------------------------------------------- ---------- --------------------- ---------- ------- ----------------------------
stats_chunk.rb 9 IO read 65577 String
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 142 FFI::Struct initialize 40 FFI::MemoryPointer
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 142 Class new 40 RbNaCl::Hash::Blake2b::State
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 167 RbNaCl::Hash::Blake2b digest 40 Array
stats_chunk.rb 7 File initialize 40 String
stats_chunk.rb 7 open 8424 File
stats_chunk.rb 21 40 String
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 111 Class new 120 RbNaCl::Hash::Blake2b
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 109 new 192 Hash
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 167 RbNaCl::Hash::Blake2b digest 40 Array
<GEM:rbnacl-6.0.1>/lib/rbnacl/hash/blake2b.rb 154 RbNaCl::Hash::Blake2b update 40 Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment