Skip to content

Commit

Permalink
Add eg/benchmark.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Apr 8, 2024
1 parent 14a91d3 commit 4e48f8d
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions eg/bench.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- This script runs a simple benchmark of a CHECK constraint using either
-- the jsonschema or the pg_jsonschema extension. To test jsonschema for
-- 200_000 iterations:
--
-- psql -f eg/bench.sql --set extension=jsonschema --iterations 200_000
--
-- And with pg_jsonschema:
--
-- psql -f eg/bench.sql --set extension=pg_jsonschema --iterations 200_000
--
-- Borrowed from
-- https://github.com/supabase/pg_jsonschema?tab=readme-ov-file#benchmark

\set QUIET on
\unset ECHO
\pset pager off

\if :{?extension}
\else
\set extension jsonschema
\endif

\if :{?iterations}
\else
\set iterations 200_000
\endif

BEGIN;

SELECT '{
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "string"}
}
}' AS schema \gset

CREATE EXTENSION :extension;

CREATE TABLE bench_json(
meta JSON CHECK (json_matches_schema(:'schema'::json, meta))
);

CREATE TABLE bench_jsonb(
meta JSON CHECK (json_matches_schema(:'schema'::json, meta))
);

\timing on

\echo
\echo ######################################################################
\echo # Test :extension JSON validation for :iterations iterations
\echo ######################################################################

INSERT INTO bench_json(meta)
SELECT json_build_object( 'a', i, 'b', i::text )
FROM generate_series(1, :iterations) t(i);

\echo
\echo
\echo ######################################################################
\echo # Test :extension JSONB validation for :iterations iterations
\echo ######################################################################

INSERT INTO bench_jsonb(meta)
SELECT json_build_object( 'a', i, 'b', i::text )
FROM generate_series(1, :iterations) t(i);

\echo
\echo
\timing off
ROLLBACK;

0 comments on commit 4e48f8d

Please sign in to comment.