# perf_test.py
## Overview
The `perf_test.py` file implements a basic testing framework focused on validating JSON parsing correctness and measuring the performance of JSON parsing operations in a repetitive manner. It includes:
* A simple test harness for running unit tests with pass/fail reporting.
* Helper functions for formatting time durations.
* Specific test cases for parsing JSON files to verify correctness.
* A performance test that parses a JSON file repeatedly to benchmark parsing speed.
* Console output formatting features including color-coded pass/fail messages (enabled only if output is a terminal).
This file is primarily designed to run as a standalone script and can be integrated into a larger test suite or development workflow to ensure JSON parsing functionality and performance meet expectations.
---
## Global Variables and Configuration
* `tests_run` (int): Counts the total number of tests executed.
* `tests_passed` (int): Counts the number of tests that passed.
* `LINE_WIDTH` (int): Width for console output separators.
* `TEST_COUNT` (int): Number of iterations for the performance test (default 100,000).
* Color constants (`GREEN`, `RED`, `RESET`): ANSI escape codes for colored output enabled conditionally if stdout is a TTY.
---
## Helper Functions
### `format_time(seconds)`
Formats a floating-point number of seconds into a string of the form `hh:mm:ss.mmm`.
* **Parameters:**
* `seconds` (float): The duration in seconds.
* **Returns:**
* `str`: Formatted time string.
* **Usage Example:**
```python
elapsed = 123.456
print(format_time(elapsed)) # Output: 00:02:03.456
Implementation Details:
Extracts hours, minutes, seconds, and milliseconds.
Uses integer division and modulo operations for time unit extraction.
Test Harness Functions
initialize_tests()
Prints header lines and a message to indicate the start of the unit tests.
Parameters: None
Returns: None
finalize_tests()
Prints summary information after all tests have run:
Total tests run.
Total tests passed.
A summary message indicating if all tests passed or some failed, with color coding.
Parameters: None
Returns: None
run_test(name, func, *args)
Runs a single test function and prints its status.
Parameters:
name(str): The descriptive name of the test.func(callable): The test function to run.*args: Arguments to pass to the test function.
Returns:
bool:Trueif the test passed;Falseif it failed.
Behavior:
Increments
tests_run.Executes
funcwith*args.If no exception occurs, increments
tests_passedand prints a "PASSED" message.If an exception occurs, prints a "FAILED" message with the error.
Usage Example:
def sample_test(x): assert x == 5 run_test("sample test", sample_test, 5)
Test Case Definitions
parse_json_file(file_name)
Parses a JSON file to verify its syntactical correctness by loading it once.
Parameters:
file_name(str): The filename located in thetestsubdirectory of the current working directory.
Returns: None
Exceptions: Raises any exceptions from JSON parsing or file I/O.
Usage Example:
parse_json_file('test.json')Implementation Details:
Opens the file in binary mode.
Reads entire content into memory.
Uses
json.loads()to parse the content.
run_perf_test(file_name, test_count)
Runs a performance test by parsing the same JSON file multiple times.
Parameters:
file_name(str): The filename located in thetestsubdirectory.test_count(int): The number of times to parse the JSON content.
Returns: None
Exceptions: Raises exceptions on file I/O or JSON parsing failure.
Usage Example:
run_perf_test('test.json', 100000)Implementation Details:
Reads the entire file once into memory.
Parses the JSON content repeatedly
test_counttimes to simulate load.Does not store or use parsing results to avoid side effects.
Main Execution Flow
When run as a script, the file performs the following steps:
Calls
initialize_tests()to print the test header.Runs a unit test using
run_test():"test_json_parsing"on'test.json'.
Prints a separator and header for performance tests.
Measures the time taken to run
run_perf_test()on'test.json'forTEST_COUNTiterations.Prints the formatted execution time.
Runs a dummy test
"test_c_json_parser"solely to integrate with the test summary.Handles any exceptions during performance testing and logs a failed test with the same test name if needed.
Calls
finalize_tests()to print the test summary.
Interactions with Other Parts of the System
Reads JSON test files from a
testdirectory relative to the current working directory.Relies on the built-in
jsonmodule for parsing.Uses standard output for test progress and results reporting.
Designed to be executed directly or integrated into automated test workflows.
The test file (
test.json) must be present in thetestdirectory for successful execution.
Implementation Notes and Algorithms
The test harness uses simple exception handling to detect test failures.
Output alignment in
run_testmimics C-style test output formatting for consistency.Performance tests avoid repeated file I/O by reading the file once.
Time formatting uses integer arithmetic to split seconds into conventional time units.
Conditional color output improves readability in terminal environments without breaking non-TTY outputs.
The default number of iterations for the performance test is set to 100,000 for balanced benchmarking and execution time.
The dummy test
"test_c_json_parser"is used to represent the performance test outcome in the test summary without performing additional parsing logic.
Diagram: File Structure and Function Relationships
flowchart TD
A[initialize_tests] --> B[run_test]
B --> C[parse_json_file]
B --> D[lambda dummy test]
A --> E[run_perf_test]
E --> F[json.loads repetition]
G[finalize_tests] --> H[test summary output]
I[format_time] --> J[format perf test duration]
subgraph Main Execution
A --> B --> E --> G
end
initialize_testsstarts test output.run_testexecutes unit tests (parse_json_file, dummy lambda).run_perf_testperforms the JSON parsing loop.format_timeis used to format the performance test duration.finalize_testsoutputs the final test result summary.
This documentation covers the internal structure, usage, and behavior of the perf_test.py file comprehensively.
```