+
+# Copyright (c) 2021-2024, PostgreSQL Global Development Group
+
+# Test success or failure of the incremental (table-driven) JSON parser
+# for a variety of small inputs.
+
use strict;
use warnings;
use File::Temp qw(tempfile);
+my $dir = PostgreSQL::Test::Utils::tempdir;
+
sub test
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $exe = "test_json_parser_incremental";
my $chunk = length($json);
+ # Test the input with chunk sizes from max(input_size, 64) down to 1
+
if ($chunk > 64)
{
$chunk = 64;
}
- my ($fh, $fname) = tempfile(UNLINK => 1);
+ my ($fh, $fname) = tempfile(DIR => $dir);
print $fh "$json";
close($fh);
- foreach my $size (reverse(1..$chunk))
+ foreach my $size (reverse(1 .. $chunk))
{
- my ($stdout, $stderr) = run_command( [$exe, "-c", $size, $fname] );
+ my ($stdout, $stderr) = run_command([ $exe, "-c", $size, $fname ]);
if (defined($params{error}))
{
- unlike($stdout, qr/SUCCESS/, "$name, chunk size $size: test fails");
- like($stderr, $params{error}, "$name, chunk size $size: correct error output");
+ unlike($stdout, qr/SUCCESS/,
+ "$name, chunk size $size: test fails");
+ like($stderr, $params{error},
+ "$name, chunk size $size: correct error output");
}
else
{
- like($stdout, qr/SUCCESS/, "$name, chunk size $size: test succeeds");
+ like($stdout, qr/SUCCESS/,
+ "$name, chunk size $size: test succeeds");
is($stderr, "", "$name, chunk size $size: no error output");
}
}
test("interrupted escapes", '"\\\\\\"\\\\\\\\\\"\\\\"');
test("whitespace", ' "" ');
-test("unclosed empty object", "{", error => qr/input string ended unexpectedly/);
+test("unclosed empty object",
+ "{", error => qr/input string ended unexpectedly/);
test("bad key", "{{", error => qr/Expected string or "}", but found "\{"/);
test("bad key", "{{}", error => qr/Expected string or "}", but found "\{"/);
-test("numeric key", "{1234: 2}", error => qr/Expected string or "}", but found "1234"/);
-test("second numeric key", '{"a": "a", 1234: 2}', error => qr/Expected string, but found "1234"/);
-test("unclosed object with pair", '{"key": "value"', error => qr/input string ended unexpectedly/);
-test("missing key value", '{"key": }', error => qr/Expected JSON value, but found "}"/);
-test("missing colon", '{"key" 12345}', error => qr/Expected ":", but found "12345"/);
-test("missing comma", '{"key": 12345 12345}', error => qr/Expected "," or "}", but found "12345"/);
-test("overnested array", "[" x 6401, error => qr/maximum permitted depth is 6400/);
-test("overclosed array", "[]]", error => qr/Expected end of input, but found "]"/);
-test("unexpected token in array", "[ }}} ]", error => qr/Expected array element or "]", but found "}"/);
+test("numeric key", "{1234: 2}",
+ error => qr/Expected string or "}", but found "1234"/);
+test(
+ "second numeric key",
+ '{"a": "a", 1234: 2}',
+ error => qr/Expected string, but found "1234"/);
+test(
+ "unclosed object with pair",
+ '{"key": "value"',
+ error => qr/input string ended unexpectedly/);
+test("missing key value",
+ '{"key": }', error => qr/Expected JSON value, but found "}"/);
+test(
+ "missing colon",
+ '{"key" 12345}',
+ error => qr/Expected ":", but found "12345"/);
+test(
+ "missing comma",
+ '{"key": 12345 12345}',
+ error => qr/Expected "," or "}", but found "12345"/);
+test("overnested array",
+ "[" x 6401, error => qr/maximum permitted depth is 6400/);
+test("overclosed array",
+ "[]]", error => qr/Expected end of input, but found "]"/);
+test("unexpected token in array",
+ "[ }}} ]", error => qr/Expected array element or "]", but found "}"/);
test("junk punctuation", "[ ||| ]", error => qr/Token "|" is invalid/);
-test("missing comma in array", "[123 123]", error => qr/Expected "," or "]", but found "123"/);
+test("missing comma in array",
+ "[123 123]", error => qr/Expected "," or "]", but found "123"/);
test("misspelled boolean", "tru", error => qr/Token "tru" is invalid/);
-test("misspelled boolean in array", "[tru]", error => qr/Token "tru" is invalid/);
-test("smashed top-level scalar", "12zz", error => qr/Token "12zz" is invalid/);
-test("smashed scalar in array", "[12zz]", error => qr/Token "12zz" is invalid/);
-test("unknown escape sequence", '"hello\vworld"', error => qr/Escape sequence "\\v" is invalid/);
-test("unescaped control", "\"hello\tworld\"", error => qr/Character with value 0x09 must be escaped/);
-test("incorrect escape count", '"\\\\\\\\\\\\\\"', error => qr/Token ""\\\\\\\\\\\\\\"" is invalid/);
+test(
+ "misspelled boolean in array",
+ "[tru]",
+ error => qr/Token "tru" is invalid/);
+test("smashed top-level scalar", "12zz",
+ error => qr/Token "12zz" is invalid/);
+test(
+ "smashed scalar in array",
+ "[12zz]",
+ error => qr/Token "12zz" is invalid/);
+test(
+ "unknown escape sequence",
+ '"hello\vworld"',
+ error => qr/Escape sequence "\\v" is invalid/);
+test("unescaped control",
+ "\"hello\tworld\"",
+ error => qr/Character with value 0x09 must be escaped/);
+test(
+ "incorrect escape count",
+ '"\\\\\\\\\\\\\\"',
+ error => qr/Token ""\\\\\\\\\\\\\\"" is invalid/);
done_testing();
+# Copyright (c) 2021-2024, PostgreSQL Global Development Group
+
+# Test the JSON parser performance tester. Here we are just checking that
+# the performance tester can run, both with the standard parser and the
+# incremental parser. An actual performance test will run with thousands
+# of iterations onstead of just one.
+
use strict;
use warnings;
my $contents = slurp_file($test_file);
-my ($fh, $fname) = tempfile(UNLINK => 1);
+my $dir = PostgreSQL::Test::Utils::tempdir;
+my ($fh, $fname) = tempfile(DIR => $dir);
# repeat the input json file 50 times in an array
-print $fh, '[', $contents , ",$contents" x 49 , ']';
+print $fh, '[', $contents, ",$contents" x 49, ']';
close($fh);
# but only do one iteration
-my ($result) = run_log([ $exe, "1", $fname ] );
+my ($result) = run_log([ $exe, "1", $fname ]);
-ok($result == 0, "perf test runs with RD parser");
+ok($result == 0, "perf test runs with recursive descent parser");
-$result = run_log([ $exe, "-i" , "1", $fname ]);
+$result = run_log([ $exe, "-i", "1", $fname ]);
ok($result == 0, "perf test runs with table driven parser");
* If the "-c SIZE" option is provided, that chunk size is used instead
* of the default of 60.
*
+ * If the -s flag is given, the program does semantic processing. This should
+ * just mirror back the json, albeit with white space changes.
+ *
* The argument specifies the file containing the JSON input.
*
*-------------------------------------------------------------------------
#include <unistd.h>
#include "common/jsonapi.h"
+#include "common/logging.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "pg_getopt.h"
case 'c': /* chunksize */
sscanf(optarg, "%zu", &chunk_size);
if (chunk_size > BUFSIZE)
- {
- fprintf(stderr, "chunk size cannot exceed %d\n", BUFSIZE);
- exit(1);
- }
+ pg_fatal("chunk size cannot exceed %d", BUFSIZE);
break;
case 's': /* do semantic processing */
testsem = &sem;
makeJsonLexContextIncremental(&lex, PG_UTF8, need_strings);
initStringInfo(&json);
- json_file = fopen(testfile, "r");
- fstat(fileno(json_file), &statbuf);
+ if ((json_file = fopen(testfile, "r")) == NULL)
+ pg_fatal("error opening input: %m");
+
+ if (fstat(fileno(json_file), &statbuf) != 0)
+ pg_fatal("error statting input: %m");
+
bytes_left = statbuf.st_size;
for (;;)
{
+ /* We will break when there's nothing left to read */
+
+ if (bytes_left < chunk_size)
+ chunk_size = bytes_left;
+
n_read = fread(buff, 1, chunk_size, json_file);
+ if (n_read < chunk_size)
+ pg_fatal("error reading input file: %d", ferror(json_file));
+
appendBinaryStringInfo(&json, buff, n_read);
/*