Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ YYJSON_LDFLAGS=$(shell pkg-config --libs yyjson)

include config.mk

all: test-json-c-get-a test-json-update test-json-filter test-json-subpath test-json-c-array-root test-json-filter-and-missing-key test-json-get-array-big-index test-json-union test-json-audit-bugs test-json-crash-vectors test-json-my-fuzz test-json-regex-overflow
all: test-json-c-get-a test-json-update test-json-filter test-json-subpath test-json-c-array-root test-json-filter-and-missing-key test-json-get-array-big-index test-json-union test-json-audit-bugs test-json-crash-vectors test-json-my-fuzz test-json-regex-overflow test-json-long-deep-keys

YYJSON_TESTS=test-yyjson

Expand Down Expand Up @@ -55,6 +55,9 @@ test-json-my-fuzz: tests/json-c/my_fuzz.c csonpath_json-c.h csonpath_my_fuzzing.
test-json-regex-overflow: tests/json-c/regex-overflow.c csonpath_json-c.h csonpath.h csonpath_do.h
$(CC) tests/json-c/regex-overflow.c $(EXTRA_FILES) $(JSON_C_CFLAGS) $(CFLAGS) -Wno-format -I./ -o test-json-regex-overflow $(JSON_C_LDFLAGS) $(LDFLAGS)

test-json-long-deep-keys: tests/json-c/long-deep-keys.c csonpath_json-c.h csonpath.h csonpath_do.h
$(CC) tests/json-c/long-deep-keys.c $(EXTRA_FILES) $(JSON_C_CFLAGS) $(CFLAGS) -Wno-format -I./ -o test-json-long-deep-keys $(JSON_C_LDFLAGS) $(LDFLAGS)

test-yyjson: tests/yyjson/test-yyjson.c csonpath_yyjson_const.h csonpath.h csonpath_do.h
$(CC) tests/yyjson/test-yyjson.c $(EXTRA_FILES) $(YYJSON_CFLAGS) $(CFLAGS) -Wno-format -I./ -o test-yyjson $(YYJSON_LDFLAGS) $(LDFLAGS)

Expand All @@ -64,7 +67,7 @@ test-yyjson-mixed: tests/yyjson/test-yyjson-mixed.c csonpath_yyjson.h csonpath_y
test-multi-backend-prefix: tests/multi-backend-prefix.c csonpath_json-c.h csonpath_yyjson.h csonpath_yyjson_const.h csonpath_yyjson_mut.h csonpath.h csonpath_do.h
$(CC) tests/multi-backend-prefix.c $(EXTRA_FILES) $(JSON_C_CFLAGS) $(YYJSON_CFLAGS) $(CFLAGS) -Wno-format -I./ -o test-multi-backend-prefix $(JSON_C_LDFLAGS) $(YYJSON_LDFLAGS) $(LDFLAGS)

tests-c: test-json-c-get-a test-json-update test-json-filter test-json-subpath test-json-c-array-root test-json-filter-and-missing-key test-json-get-array-big-index test-json-union test-json-audit-bugs test-json-crash-vectors test-yyjson test-yyjson-mixed test-json-my-fuzz test-multi-backend-prefix test-json-regex-overflow
tests-c: test-json-c-get-a test-json-update test-json-filter test-json-subpath test-json-c-array-root test-json-filter-and-missing-key test-json-get-array-big-index test-json-union test-json-audit-bugs test-json-crash-vectors test-yyjson test-yyjson-mixed test-json-my-fuzz test-multi-backend-prefix test-json-regex-overflow test-json-long-deep-keys
./test-json-c-get-a
./test-json-update
./test-json-filter
Expand All @@ -80,6 +83,7 @@ tests-c: test-json-c-get-a test-json-update test-json-filter test-json-subpath t
./test-json-my-fuzz
./test-multi-backend-prefix
./test-json-regex-overflow
./test-json-long-deep-keys

csonpath: cli/csonpath_cli.c csonpath_json-c.h csonpath.h csonpath_do.h
$(CC) cli/csonpath_cli.c $(EXTRA_FILES) $(JSON_C_CFLAGS) $(CFLAGS) -I./ -o csonpath $(JSON_C_LDFLAGS) $(LDFLAGS)
Expand Down
5 changes: 4 additions & 1 deletion bench/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ bench_regexs: bench-json-c-gcc-pcrepos bench-json-c-gcc-tinyre bench-json-c-gcc-


clean:
rm -vf bench-json-c-tcc bench-json-c-gcc bench-json-c-clang bench-yyjson bench-json-c-gcc-pcrepos bench-json-c-gcc-tinyre bench-json-c-gcc-pcre
rm -vf bench-json-c-tcc bench-json-c-gcc bench-json-c-clang bench-yyjson bench-json-c-gcc-pcrepos bench-json-c-gcc-tinyre bench-json-c-gcc-pcre bench-long-deep-keys

bench-long-deep-keys: bench-long-deep-keys.c
gcc bench-long-deep-keys.c -O3 -g -I../ $(shell pkg-config --cflags json-c) -o bench-long-deep-keys $(shell pkg-config --libs json-c)

bench-json-c-tcc: bench-json-c.c
tcc bench-json-c.c -O3 -g -I../ $(shell pkg-config --cflags json-c) -o bench-json-c-tcc $(shell pkg-config --libs json-c)
Expand Down
129 changes: 129 additions & 0 deletions bench/bench-long-deep-keys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "csonpath_json-c.h"

#define N_OBJECTS 1000
#define DEPTH 10
#define KEY_LEN 100
#define ITERS 1000

static double now_seconds(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
}

static json_object *build_data(void)
{
json_object *root = json_object_new_object();
char long_key[KEY_LEN + 1];
int i, j;

memset(long_key, 'k', KEY_LEN);
long_key[KEY_LEN] = '\0';

for (i = 0; i < N_OBJECTS; ++i) {
json_object *obj = json_object_new_object();
json_object *cur = obj;
char obj_name[16];

for (j = 0; j < DEPTH; ++j) {
json_object *next = json_object_new_object();
json_object_object_add(cur, long_key, next);
cur = next;
}
json_object_object_add(cur, "leaf", json_object_new_string("deep value"));

json_object *arr = json_object_new_array();
for (j = 0; j < 5; ++j) {
json_object *item = json_object_new_object();
json_object *inner = json_object_new_object();
json_object_object_add(inner, "kk", json_object_new_string("array value"));
json_object_object_add(item, "k", inner);
json_object_array_add(arr, item);
}
json_object_object_add(cur, "arr", arr);

snprintf(obj_name, sizeof(obj_name), "obj%d", i);
json_object_object_add(root, obj_name, obj);
}

return root;
}

static void build_dot_path(char *buf, size_t buf_size, const char *long_key)
{
int i;
snprintf(buf, buf_size, "$.obj0");
for (i = 0; i < DEPTH; ++i) {
strncat(buf, ".", buf_size - strlen(buf) - 1);
strncat(buf, long_key, buf_size - strlen(buf) - 1);
}
strncat(buf, ".leaf", buf_size - strlen(buf) - 1);
}

static void build_bracket_path(char *buf, size_t buf_size, const char *long_key)
{
int i;
snprintf(buf, buf_size, "$['obj0']");
for (i = 0; i < DEPTH; ++i) {
strncat(buf, "['", buf_size - strlen(buf) - 1);
strncat(buf, long_key, buf_size - strlen(buf) - 1);
strncat(buf, "']", buf_size - strlen(buf) - 1);
}
strncat(buf, "['leaf']", buf_size - strlen(buf) - 1);
}

static void bench_query(const char *name, const char *query, json_object *root)
{
struct csonpath *p = csonpath_new(query);
size_t count = 0;
double start, elapsed;
int i;

if (!p) {
fprintf(stderr, "failed to compile: %s\n", query);
return;
}

start = now_seconds();
for (i = 0; i < ITERS; ++i) {
struct json_object *ret = csonpath_find_all(p, root);
count = ret ? json_object_array_length(ret) : 0;
json_object_put(ret);
}
elapsed = now_seconds() - start;

printf("Query: %s\n", name);
printf("Path: %s\n", query);
printf("Results: %zu, in %d loop, Time: %.6f seconds\n\n", count, ITERS, elapsed);

csonpath_destroy(p);
}

int main(void)
{
json_object *root = build_data();
char long_key[KEY_LEN + 1];
char path_dot[4096];
char path_bracket[4096];
char path_recursive[KEY_LEN + 5];

memset(long_key, 'k', KEY_LEN);
long_key[KEY_LEN] = '\0';

build_dot_path(path_dot, sizeof(path_dot), long_key);
build_bracket_path(path_bracket, sizeof(path_bracket), long_key);
snprintf(path_recursive, sizeof(path_recursive), "$..%s", long_key);

bench_query("dot long/deep", path_dot, root);
bench_query("bracket long/deep", path_bracket, root);
bench_query("recursive descent long key", path_recursive, root);
bench_query("wildcard array", "$..arr[*].k.kk", root);

json_object_put(root);
return 0;
}
2 changes: 1 addition & 1 deletion cli/csonpath_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "json.h"
#include "csonpath_json-c.h"

#define VERSION "0.19.1"
#define VERSION "0.20.0"

#define FLAG_JQ_LIKE (1U << 0)
#define FLAG_RAW (1U << 1)
Expand Down
Loading
Loading