Skip to content

Commit

Permalink
Benchmark with no validation
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Apr 9, 2024
1 parent c02daf1 commit 4a803d5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

######################################################################
Expand All @@ -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
---------------------

Expand Down
19 changes: 17 additions & 2 deletions eg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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
Expand Down
25 changes: 15 additions & 10 deletions eg/bench.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
\unset ECHO
\pset pager off

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

\if :{?iterations}
\else
\set iterations 200_000
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;

0 comments on commit 4a803d5

Please sign in to comment.