diff options
author | Andrew Dunstan <andrew@dunslane.net> | 2024-03-10 23:10:14 -0400 |
---|---|---|
committer | Andrew Dunstan <andrew@dunslane.net> | 2024-04-04 06:46:40 -0400 |
commit | 3311ea86edc7a689614bad754e17371865cdc11f (patch) | |
tree | 7c9d55385afb9b21a8c790a64b1b9ea8eedff90e /src/test/modules/test_json_parser/test_json_parser_perf.c | |
parent | 585df02b445f63167f145685e045e5b6074a5a30 (diff) | |
download | postgresql-3311ea86edc7a689614bad754e17371865cdc11f.tar.gz postgresql-3311ea86edc7a689614bad754e17371865cdc11f.zip |
Introduce a non-recursive JSON parser
This parser uses an explicit prediction stack, unlike the present
recursive descent parser where the parser state is represented on the
call stack. This difference makes the new parser suitable for use in
incremental parsing of huge JSON documents that cannot be conveniently
handled piece-wise by the recursive descent parser. One potential use
for this will be in parsing large backup manifests associated with
incremental backups.
Because this parser is somewhat slower than the recursive descent
parser, it is not replacing that parser, but is an additional parser
available to callers.
For testing purposes, if the build is done with -DFORCE_JSON_PSTACK, all
JSON parsing is done with the non-recursive parser, in which case only
trivial regression differences in error messages should be observed.
Author: Andrew Dunstan
Reviewed-By: Jacob Champion
Discussion: https://postgr.es/m/7b0a51d6-0d9d-7366-3a1a-f74397a02f55@dunslane.net
Diffstat (limited to 'src/test/modules/test_json_parser/test_json_parser_perf.c')
-rw-r--r-- | src/test/modules/test_json_parser/test_json_parser_perf.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/test/modules/test_json_parser/test_json_parser_perf.c b/src/test/modules/test_json_parser/test_json_parser_perf.c new file mode 100644 index 00000000000..517dc8529ac --- /dev/null +++ b/src/test/modules/test_json_parser/test_json_parser_perf.c @@ -0,0 +1,86 @@ +/*------------------------------------------------------------------------- + * + * test_json_parser_perf.c + * Performancet est program for both flavors of the JSON parser + * + * Copyright (c) 2023, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_json_parser/test_json_parser_perf.c + * + * This progam tests either the standard (recursive descent) JSON parser + * or the incremental (table driven) parser, but without breaking the input + * into chunks in the latter case. Thus it can be used to compare the pure + * parsing speed of the two parsers. If the "-i" option is used, then the + * table driven parser is used. Otherwise, the recursive descent parser is + * used. + * + * The remaining arguments are the number of parsing iterations to be done + * and the file containing the JSON input. + * + *------------------------------------------------------------------------- + */ + +#include "postgres_fe.h" +#include "common/jsonapi.h" +#include "lib/stringinfo.h" +#include "mb/pg_wchar.h" +#include <stdio.h> +#include <string.h> + +int +main(int argc, char **argv) +{ + /* max delicious line length is less than this */ + char buff[6001]; + FILE *json_file; + JsonParseErrorType result; + JsonLexContext *lex; + StringInfoData json; + int n_read; + int iter; + int use_inc = 0; + + initStringInfo(&json); + + if (strcmp(argv[1], "-i") == 0) + { + use_inc = 1; + argv++; + } + + sscanf(argv[1], "%d", &iter); + + json_file = fopen(argv[2], "r"); + while ((n_read = fread(buff, 1, 6000, json_file)) > 0) + { + appendBinaryStringInfo(&json, buff, n_read); + } + fclose(json_file); + for (int i = 0; i < iter; i++) + { + if (use_inc) + { + lex = makeJsonLexContextIncremental(NULL, PG_UTF8, false); + result = pg_parse_json_incremental(lex, &nullSemAction, + json.data, json.len, + true); + freeJsonLexContext(lex); + } + else + { + lex = makeJsonLexContextCstringLen(NULL, json.data, json.len, + PG_UTF8, false); + result = pg_parse_json(lex, &nullSemAction); + freeJsonLexContext(lex); + } + if (result != JSON_SUCCESS) + { + fprintf(stderr, + "unexpected result %d (expecting %d) on parse\n", + result, JSON_SUCCESS); + exit(1); + } + } + exit(0); +} |