Created
January 31, 2024 20:57
-
-
Save mostlyobvious/63c0dbfe1a108c9aa6044a103a35c0a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Author
mostlyobvious
commented
Jan 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment