Skip to content

Instantly share code, notes, and snippets.

@mostlyobvious
Created January 31, 2024 20:57
Show Gist options
  • Save mostlyobvious/63c0dbfe1a108c9aa6044a103a35c0a4 to your computer and use it in GitHub Desktop.
Save mostlyobvious/63c0dbfe1a108c9aa6044a103a35c0a4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "sequel"
gem "pg"
gem "concurrent-ruby"
gem "benchmark-ips"
end
require "benchmark/ips"
require "concurrent-ruby"
require "sequel"
require "json"
require "securerandom"
DB =
Sequel.connect(
ENV.fetch("DATABASE_URL"),
max_connections: 5,
preconnect: :concurrently
)
DB.create_table?(:log) do
primary_key :id
column :data, "jsonb", null: false
column :type, "varchar", null: false
column :evid, "uuid", null: false
column :time, "timestamp", null: false
end
baseline =
lambda do
DB[:log].insert(
data: JSON.dump({ "kaka" => "dudu" }),
type: "kaka.dudu",
evid: SecureRandom.uuid,
time: Sequel.lit("NOW()")
)
end
locking =
lambda do
DB.transaction do
DB["SELECT pg_advisory_xact_lock(1845240511599988039)"]
DB[:log].insert(
data: JSON.dump({ "kaka" => "dudu" }),
type: "kaka.dudu",
evid: SecureRandom.uuid,
time: Sequel.lit("NOW()")
)
end
end
examples = { baseline: baseline, locking: locking }
Benchmark.ips do |x|
examples.each do |name, block|
x.report(name) do
pool = Concurrent::FixedThreadPool.new(5)
pool.post { block.call }
pool.shutdown
pool.wait_for_termination
end
end
x.compare
end
@mostlyobvious
Copy link
Author

Warming up --------------------------------------
            baseline   952.000 i/100ms
             locking   795.000 i/100ms
Calculating -------------------------------------
            baseline      9.931k (± 2.2%) i/s -     50.456k in   5.083241s
             locking      7.997k (± 1.2%) i/s -     40.545k in   5.070526s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment