Skip to content

Instantly share code, notes, and snippets.

@kusumandaru
Created January 3, 2021 10:31
Show Gist options
  • Save kusumandaru/0475e9e9f7c1cae17d7f668a224b6530 to your computer and use it in GitHub Desktop.
Save kusumandaru/0475e9e9f7c1cae17d7f668a224b6530 to your computer and use it in GitHub Desktop.
require 'benchmark'
RSpec.describe User, type: :model do
context '#benchmark create' do
let(:users) { FactoryBot.create_list(:user, 5_000) }
it 'return benchmark' do
benchmark = Benchmark.measure {
users
}
puts benchmark
end
end
context '#benchmark build' do
let(:users) { FactoryBot.build_list(:user, 5_000) }
it 'return benchmark' do
benchmark = Benchmark.measure {
users
}
puts benchmark
end
end
context '#benchmart items' do
input = ('a'..'z').map { |letter| [letter, letter] }.to_h
puts Benchmark.measure {
5_000_000.times do
input.map { |key, value| [key.to_sym, value] }.to_h
end
}
end
context '#benchmark iterate build' do
it 'return benchmark' do
benchmark = Benchmark.measure {
50_000.times do
FactoryBot.build(:user)
end
}
puts benchmark
end
end
context '#benchmark iterate create' do
it 'return benchmark' do
benchmark = Benchmark.measure {
50_000.times do
FactoryBot.create(:user)
end
}
puts benchmark
end
end
context '#benchmark cpu intensive task' do
it 'return benchmark' do
Benchmark.measure { 10.times { Fibonaci.new.fib(35) } }
end
end
context '#benchmark cpu intensive task on thread' do
it 'return benchmark' do
Benchmark.measure {
threads = []
10.times do
threads << Thread.new { Thread.current[:output] = Fibonaci.new.fib(35) }
end
threads.each { |thread| thread.join }
}
end
end
context '#benchmark cpu intensive task on thread' do
it 'return benchmark' do
Benchmark.measure {
read_stream, write_stream = IO.pipe
10.times do
Process.fork do
write_stream.puts Fibonaci.new.fib(35)
end
end
Process.waitall
write_stream.close
results = read_stream.read
read_stream.close
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment