test_performance.c
Overview
This source file defines a performance test for a JSON parsing library. The primary objective is to measure and report the time taken to repeatedly parse a JSON data file using the json_parse function and to validate the correctness of this operation through assertions. The test is designed to run a large number of iterations (TEST_COUNT), which can be adjusted by defining the LONG_TEST macro. The file includes a single test function test_c_json_parser and a main function that initializes the test framework, runs the test, and finalizes the testing process.
Detailed Description of Components
Macros
TEST_COUNT
Defines the number of iterations the JSON parsing test will run.If
LONG_TESTis defined,TEST_COUNTis set to 1,000,000 (one million).Otherwise, it defaults to 100,000.
This macro enables scaling the test for either quick runs or extended performance benchmarking.
Functions
TEST(test_c_json_parser)
A test function encapsulated within the testing framework's TEST macro, designed to execute the JSON parsing performance test.
Functionality:
Loads JSON data from a file
"test/test.json"usingutils_get_test_json_data.Asserts that the loaded JSON pointer is not null to ensure test data availability.
Initializes a
json_valuestruct to zero before parsing.Records the start time using
utils_get_time().Runs a loop
TEST_COUNTtimes, and in each iteration:Resets the
json_valuestruct.Parses the JSON data into the
json_valuestructure usingjson_parse.Frees any allocated memory in
json_valueusingjson_free.
Records the end time after all iterations.
Prints the elapsed time between start and end using
utils_print_time_diff.Frees the JSON data buffer.
Ends the test using
END_TEST.
Parameters:
None (defined via the
TESTmacro, which internally handles test registration).
Return Value:
None (void). Uses framework macros for test control.
Usage Example:
test_c_json_parser();
int main(void)
Entry point for running the performance tests.
Functionality:
Initializes the testing framework (
TEST_INITIALIZE).Declares a test suite named
"performance tests"(TEST_SUITE).Calls the
test_c_json_parserfunction to execute the performance test.Finalizes the testing framework (
TEST_FINALIZE).Returns 0 on completion.
Usage Example:
int result = main();
Implementation Details and Algorithms
Repeated Parsing and Timing:
The core algorithm involves repeatedly parsing the same JSON input to measure the performance of the JSON parser. The test uses a high iteration count (TEST_COUNT) to amortize timing overhead and obtain statistically meaningful performance data.Memory Management:
Each iteration cleans up thejson_valuestructure after parsing to prevent memory leaks, ensuring that each parse operation starts with a clean state. This is crucial for accurate performance measurement and resource management.Timing Measurement:
The timing usesutils_get_time()to capture timestamps before and after the loop, andutils_print_time_diff()to output the elapsed time. This facilitates benchmarking the parser's speed.Test Data Loading:
JSON test data is loaded once from a file and reused in all iterations, preventing file I/O overhead from influencing the timing results.
Interactions with Other Parts of the System
Test Framework:
The file relies on a test framework that provides macros such asTEST,ASSERT_PTR_NOT_NULL,TEST_INITIALIZE,TEST_SUITE,TEST_FINALIZE, andEND_TEST. These macros handle test registration, assertion checking, and test lifecycle management.JSON Parsing Library:
Uses thejson_parsefunction to parse JSON data andjson_freeto release resources allocated during parsing. Thejson_valuedata structure represents the parsed JSON.Utility Functions:
utils_get_test_json_data: Loads JSON data from a test file.utils_get_time: Retrieves a timestamp for performance measurement.utils_print_time_diff: Prints the elapsed time between two timestamps.
File Dependency:
Includes the header"../test/test.h", which presumably declares testing macros and utility functions.
Visual Diagram: Function Flow and Relationships
flowchart TD
A["main()"] --> B[TEST_INITIALIZE]
B --> C["TEST_SUITE("performance tests")"]
C --> D["test_c_json_parser()"]
D --> E["utils_get_test_json_data()"]
D --> F["json_parse()"]
D --> G["json_free()"]
D --> H["utils_get_time()"]
D --> I["utils_print_time_diff()"]
I --> J["free(json)"]
J --> K[END_TEST]
K --> L[TEST_FINALIZE]
main()initializes the test framework and runs the performance test.test_c_json_parser()fetches JSON data, performs repeated parsing and cleanup, times the operation, and reports results.Utility functions are used for data retrieval, timing, and output.
The test framework macros manage test lifecycle events.
This documentation describes the structure and workflow of the test_performance.c file, focusing on its role in benchmarking JSON parsing performance within a controlled test environment. For additional context on the test framework and JSON parsing library, refer to their respective documentation.