From 1017bf6f42e5a7f70d0c9dff384a688fee8e66b1 Mon Sep 17 00:00:00 2001 From: Watson Date: Wed, 8 Nov 2023 01:03:56 +0900 Subject: [PATCH] Add benchmark --- example/benchmark_insert.rb | 154 ++++++++++++++++++++++++++++++++++++ example/benchmark_select.rb | 142 +++++++++++++++++++++++++++++++++ ilios.gemspec | 2 +- 3 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 example/benchmark_insert.rb create mode 100644 example/benchmark_select.rb diff --git a/example/benchmark_insert.rb b/example/benchmark_insert.rb new file mode 100644 index 0000000..fb6f06f --- /dev/null +++ b/example/benchmark_insert.rb @@ -0,0 +1,154 @@ +require 'bundler/inline' +gemfile do + source 'https://rubygems.org' + gem 'benchmark-ips' + gem 'sorted_set' + gem 'cassandra-driver', require: 'cassandra' + gem 'ilios' +end + + +Ilios::Cassandra.config = { + keyspace: 'ilios', + hosts: ['127.0.0.1'], +} + +# Create new table +statement = Ilios::Cassandra.session.prepare(<<~CQL) + DROP TABLE IF EXISTS ilios.benchmark_insert +CQL +Ilios::Cassandra.session.execute(statement) + +statement = Ilios::Cassandra.session.prepare(<<~CQL) + CREATE TABLE IF NOT EXISTS ilios.benchmark_insert ( + id bigint, + message text, + created_at timestamp, + PRIMARY KEY (id) + ) WITH compaction = { 'class' : 'LeveledCompactionStrategy' } + AND gc_grace_seconds = 691200; +CQL +Ilios::Cassandra.session.execute(statement) + + +class BenchmarkCassandra + def initialize + @cluster = Cassandra.cluster + @keyspace = 'ilios' + @session = @cluster.connect(@keyspace) + end + + def run_execute(x) + x.report('cassandra-driver:execute') do + @session.execute(statement, { + arguments: { + id: Random.rand(2**40), + message: 'hello', + created_at: Time.now + } + }) + end + end + + def run_execute_async(x) + x.report('cassandra-driver:execute_async') do + future = @session.execute_async(statement, { + arguments: { + id: Random.rand(2**40), + message: 'hello', + created_at: Time.now + } + }) + future.on_success do |rows| + end + end + end + + def statement + @statement ||= @session.prepare(<<~CQL) + INSERT INTO ilios.benchmark_insert ( + id, + message, + created_at + ) VALUES (?, ?, ?) + CQL + end +end + +class BenchmarkIlios + def run_execute(x) + x.report('ilios:execute') do + statement.bind({ + id: Random.rand(2**40), + message: 'hello', + created_at: Time.now + }) + Ilios::Cassandra.session.execute(statement) + end + end + + def run_execute_async(x) + x.report('ilios:execute_async') do + statement.bind({ + id: Random.rand(2**40), + message: 'hello', + created_at: Time.now + }) + future = Ilios::Cassandra.session.execute_async(statement) + future.on_success do |results| + results.each do |row| + end + end + end + end + + def statement + @statement ||= Ilios::Cassandra.session.prepare(<<-CQL) + INSERT INTO ilios.benchmark_insert ( + id, + message, + created_at + ) VALUES (?, ?, ?) + CQL + end +end + +Benchmark.ips do |x| + BenchmarkCassandra.new.run_execute(x) + BenchmarkCassandra.new.run_execute_async(x) +end + +sleep 10 + +puts "" +Benchmark.ips do |x| + BenchmarkIlios.new.run_execute(x) + BenchmarkIlios.new.run_execute_async(x) +end + +=begin +## Environment +- OS: Manjaro Linux x86_64 +- CPU: AMD Ryzen 9 7940HS +- Compiler: gcc 13.2.1 +- Ruby: ruby 3.2.2 + +## Results +Warming up -------------------------------------- +cassandra-driver:execute + 304.000 i/100ms +cassandra-driver:execute_async + 3.679k i/100ms +Calculating ------------------------------------- +cassandra-driver:execute + 3.332k (±34.1%) i/s - 10.032k in 5.040480s +cassandra-driver:execute_async + 34.484k (±26.6%) i/s - 150.839k in 5.003236s + +Warming up -------------------------------------- + ilios:execute 457.000 i/100ms + ilios:execute_async 17.478k i/100ms +Calculating ------------------------------------- + ilios:execute 4.622k (± 1.6%) i/s - 23.307k in 5.044216s + ilios:execute_async 167.010k (± 3.4%) i/s - 838.944k in 5.029271s +=end diff --git a/example/benchmark_select.rb b/example/benchmark_select.rb new file mode 100644 index 0000000..6726b0c --- /dev/null +++ b/example/benchmark_select.rb @@ -0,0 +1,142 @@ +require 'bundler/inline' +gemfile do + source 'https://rubygems.org' + gem 'benchmark-ips' + gem 'sorted_set' + gem 'cassandra-driver', require: 'cassandra' + gem 'ilios' +end + + +Ilios::Cassandra.config = { + keyspace: 'ilios', + hosts: ['127.0.0.1'], +} + +# Create new table +statement = Ilios::Cassandra.session.prepare(<<~CQL) + DROP TABLE IF EXISTS ilios.benchmark_select +CQL +Ilios::Cassandra.session.execute(statement) + +statement = Ilios::Cassandra.session.prepare(<<~CQL) + CREATE TABLE IF NOT EXISTS ilios.benchmark_select ( + id bigint, + message text, + created_at timestamp, + PRIMARY KEY (id) + ) WITH compaction = { 'class' : 'LeveledCompactionStrategy' } + AND gc_grace_seconds = 691200; +CQL +Ilios::Cassandra.session.execute(statement) + +# Prepare data +statement = Ilios::Cassandra.session.prepare(<<-CQL) + INSERT INTO ilios.benchmark_select ( + id, + message, + created_at + ) VALUES (?, ?, ?) +CQL + +1000.times do + statement.bind({ + id: Random.rand(2**40), + message: 'hello', + created_at: Time.now + }) + Ilios::Cassandra.session.execute(statement) +end + + +class BenchmarkCassandra + def initialize + @cluster = Cassandra.cluster + @keyspace = 'ilios' + @session = @cluster.connect(@keyspace) + end + + def run_execute(x) + x.report('cassandra-driver:execute') do + @session.execute(statement) + end + end + + def run_execute_async(x) + x.report('cassandra-driver:execute_async') do + future = @session.execute_async(statement) + future.on_success do |rows| + end + end + end + + def statement + @statement ||= @session.prepare(<<~CQL) + SELECT * FROM ilios.benchmark_select + CQL + end +end + +class BenchmarkIlios + def run_execute(x) + x.report('ilios:execute') do + Ilios::Cassandra.session.execute(statement) + end + end + + def run_execute_async(x) + x.report('ilios:execute_async') do + future = Ilios::Cassandra.session.execute_async(statement) + future.on_success do |results| + results.each do |row| + end + end + end + end + + def statement + @statement ||= Ilios::Cassandra.session.prepare(<<-CQL) + SELECT * FROM ilios.benchmark_select + CQL + end +end + +Benchmark.ips do |x| + BenchmarkCassandra.new.run_execute(x) + BenchmarkCassandra.new.run_execute_async(x) +end + +sleep 10 + +puts "" +Benchmark.ips do |x| + BenchmarkIlios.new.run_execute(x) + BenchmarkIlios.new.run_execute_async(x) +end + +=begin +## Environment +- OS: Manjaro Linux x86_64 +- CPU: AMD Ryzen 9 7940HS +- Compiler: gcc 13.2.1 +- Ruby: ruby 3.2.2 + +## Results +Warming up -------------------------------------- +cassandra-driver:execute + 304.000 i/100ms +cassandra-driver:execute_async + 3.679k i/100ms +Calculating ------------------------------------- +cassandra-driver:execute + 3.332k (±34.1%) i/s - 10.032k in 5.040480s +cassandra-driver:execute_async + 34.484k (±26.6%) i/s - 150.839k in 5.003236s + +Warming up -------------------------------------- + ilios:execute 15.000 i/100ms + ilios:execute_async 4.000 i/100ms +Calculating ------------------------------------- + ilios:execute 156.303 (±35.2%) i/s - 615.000 in 5.033882s + ilios:execute_async 428.707k (±35.4%) i/s - 16.716k in 4.949610s +=end diff --git a/ilios.gemspec b/ilios.gemspec index 8dc1de5..e166c67 100644 --- a/ilios.gemspec +++ b/ilios.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |spec| spec.files = Dir.chdir(__dir__) do `git ls-files -z`.split("\x0").reject do |f| - (f == __FILE__) || f.match(%r{\A(?:(?:test)/|\.(?:git|editorconfig|rubocop.*))}) + (f == __FILE__) || f.match(%r{\A(?:(?:test|example)/|\.(?:git|editorconfig|rubocop.*))}) end end spec.require_paths = ['lib']