From a118afaffb78c5c16e885493ebc6ae306d3c6f6c Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 9 Jul 2023 22:12:32 +0100 Subject: [PATCH] Add profiling code to the json parser --- haversine_02/src/json/parser.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/haversine_02/src/json/parser.c b/haversine_02/src/json/parser.c index 98d0e43..6d8fa30 100644 --- a/haversine_02/src/json/parser.c +++ b/haversine_02/src/json/parser.c @@ -1,5 +1,7 @@ #include "json/parser.h" #include "aliases.h" +#include "profiler/ids.h" +#include "profiler/timer.h" #include "json/dstring.h" #include "json/json_entities.h" #include "json/lexer.h" @@ -21,6 +23,8 @@ INTERNAL jentity_t *add_value(parser_t *parser); INTERNAL void add_collection(parser_t *parser); jentity_t *load_json(const char *filepath) { + SAMPLE_START(PROFILER_ID_READ_JSON_FILE, "READ JSON FILE"); + FILE *fp = fopen(filepath, "r"); if (!fp) { @@ -40,6 +44,10 @@ jentity_t *load_json(const char *filepath) { fclose(fp); + SAMPLE_END(PROFILER_ID_READ_JSON_FILE); + + SAMPLE_START(PROFILER_ID_PARSER_SETUP, "JSON PARSER SETUP"); + lexer_t *lexer = NULL; parser_t *parser = NULL; @@ -55,15 +63,23 @@ jentity_t *load_json(const char *filepath) { return NULL; } + SAMPLE_END(PROFILER_ID_PARSER_SETUP); + + SAMPLE_START(PROFILER_ID_LEX_GET_TOKEN, "GET NEXT TOKEN"); lex_result_t result = get_next_token(lexer, json); + SAMPLE_END(PROFILER_ID_LEX_GET_TOKEN); if (result.error.errno) { printf("%s\n", result.error.msg); } else { while (result.token.type != TK_NO_TOKEN) { + SAMPLE_START(PROFILER_ID_PARSE_TOKEN, "PARSE TOKEN"); parse_token(parser, result.token); + SAMPLE_END(PROFILER_ID_PARSE_TOKEN); + SAMPLE_START(PROFILER_ID_LEX_GET_TOKEN, "GET NEXT TOKEN"); result = get_next_token(lexer, NULL); + SAMPLE_END(PROFILER_ID_LEX_GET_TOKEN); if (result.error.errno) { printf("%s\n", result.error.msg); @@ -74,10 +90,14 @@ jentity_t *load_json(const char *filepath) { jentity_t *root = parser->root; + SAMPLE_START(PROFILER_ID_PARSER_TEAR_DOWN, "PARSER TEAR DOWN"); + parser_free(&parser); lexer_free(&lexer); free(json); + SAMPLE_END(PROFILER_ID_PARSER_TEAR_DOWN); + return root; }