From 4a803d5498803ef576f0ed915a6ce340451b6245 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Tue, 9 Apr 2024 13:35:04 -0400 Subject: [PATCH] Benchmark with no validation --- README.md | 24 +++++++++++++++++++++--- eg/README.md | 19 +++++++++++++++++-- eg/bench.sql | 25 +++++++++++++++---------- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6bc56c6..ccf551a 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,8 @@ Benchmark A quick benchmark in [`eg/bench.sql](eg/bench.sql) compares the performance for a simple validation a check constraint between the jsonschema and -[pg_jsonschema]. Example running PostgreSQL 16 on an M3 Max MacBook Pro with -32G of RAM: +[pg_jsonschema]. Example testing `pg_jsonschema` with PostgreSQL 16 on an M3 +Max MacBook Pro with 32G of RAM: ``` console $ psql -Xf eg/bench.sql --set extension=pg_jsonschema @@ -98,13 +98,15 @@ $ psql -Xf eg/bench.sql --set extension=pg_jsonschema ###################################################################### Time: 8170.906 ms (00:08.171) - ###################################################################### # Test pg_jsonschema JSONB validation for 200_000 iterations ###################################################################### Time: 8215.921 ms (00:08.216) +``` +Testing `jsonschema`: +``` console $ psql -Xf eg/bench.sql --set extension=jsonschema ###################################################################### @@ -118,6 +120,22 @@ Time: 3356.828 ms (00:03.357) Time: 3428.245 ms (00:03.428) ``` +And a control test with no validation: + +``` console +❯ psql -Xf eg/bench.sql + +###################################################################### +# Test without JSON validation for 200_000 iterations +###################################################################### +Time: 474.688 ms + +###################################################################### +# Test without JSONB validation for 200_000 iterations +###################################################################### +Time: 462.265 ms +``` + Copyright and License --------------------- diff --git a/eg/README.md b/eg/README.md index 99408f6..bf5b84e 100644 --- a/eg/README.md +++ b/eg/README.md @@ -28,10 +28,18 @@ Use Cases This file contains a simple benchmark SQL script that demonstrates the performance of jsonschema vs [pg_jsonschema]. It requires that each be built -and installed. To test jsonschema, run: +and installed. Parameters: + +* `extension`: The extension to test, `jsonschema` or `pg_jsonschema`. + If not set there will be no validation. +* `iterations`: Number of iterations of the task to run (inserting rowed + into a table). Defaults to `200_000`. + + To test jsonschema, run: ```sh -psql -Xf eg/bench.sql +psql -Xf eg/bench.sql --set extension=jsonschema + ``` And to test [pg_jsonschema]: @@ -40,6 +48,13 @@ And to test [pg_jsonschema]: psql -Xf eg/bench.sql --set extension=pg_jsonschema ``` +Omit the `extension` parameter to see the performance with no schema +validation: + +```sh +psql -Xf eg/bench.sql +``` + Set `iterations` to change the number of iterations (defaults to 200,000): ```sh diff --git a/eg/bench.sql b/eg/bench.sql index a9c11fb..a862bfe 100644 --- a/eg/bench.sql +++ b/eg/bench.sql @@ -15,11 +15,6 @@ \unset ECHO \pset pager off -\if :{?extension} -\else - \set extension jsonschema -\endif - \if :{?iterations} \else \set iterations 200_000 @@ -35,18 +30,31 @@ SELECT '{ } }' AS schema \gset +\if :{?extension} CREATE EXTENSION :extension; +\endif CREATE TABLE bench_json( - meta JSON CHECK (json_matches_schema(:'schema'::json, meta)) + meta JSON +\if :{?extension} + CHECK (json_matches_schema(:'schema'::json, meta)) +\endif ); CREATE TABLE bench_jsonb( - meta JSON CHECK (json_matches_schema(:'schema'::json, meta)) + meta JSON +\if :{?extension} + CHECK (json_matches_schema(:'schema'::json, meta)) +\endif ); \timing on +\if :{?extension} +\else + \set extension without +\endif + \echo \echo ###################################################################### \echo # Test :extension JSON validation for :iterations iterations @@ -56,7 +64,6 @@ 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 @@ -66,7 +73,5 @@ 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;