Repository navigation
perf: append measurements to JSON and XML instead of rewriting - #86
Merged
Merged
Conversation
JSON and XML files were still read and written again as a whole on every measurement (after #85 changed CSV and SQLite). - Insert the new record before the closing `]` (JSON) or `</data>` (XML), written with the same pandas functions as before, so the file stays valid and is byte for byte the same as if it had been written again; about 2 ms (JSON) and 3 ms (XML) per measurement with 1,000,000 stored rows - Only the first record (for the column names) and the end of the file are read; the XML file is parsed incrementally up to its first row - Fall back to writing the whole file once when the file is new or empty, the configured columns changed, or the file does not end as expected; compact JSON written by other tools is also supported - Keep the file's line endings (e.g. \r\n written on Windows) - Document it and add tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second part after #85: JSON and XML were still read and written again as a whole on every
POST /measure.How it works
Both files are a single document, so a row cannot simply be added at the end. Instead:
[+ records +]. The new record is created with the same call as before (DataFrame.to_json(orient='records', indent=4)) and inserted before the closing], separated by,.<data>+<row>elements +</data>. The new<row>is created withDataFrame.to_xml(index=False)and inserted before the closing</data>.The result is byte for byte the same file as writing everything again (tested, including values with
],</data>,<&>, quotes, non-ASCII characters and very long values), so nothing changes for anyone reading these files.Only small parts of the file are read:
raw_decodeof the first object, up to 1 MB; XML:iterparsestopped after the first<row>);]/</data>.Falls back to writing the whole file once (as before) when
[];<data>document, no}/</row>before the closing token).Compact JSON (
[{...},{...}], e.g. written by another tool) is appended to as well, and stays valid. Line endings follow the file (\r\nif it was written on Windows).Speed (per measurement)
Before: 45 ms (JSON) and 246 ms (XML) with only 10,000 rows. (A first XML run at 1M rows showed 84 ms; that was the benchmark itself keeping a 1M-row DataFrame alive, which made Python's garbage collector slow. Measured per request without it: 3–5 ms.)
Tests
tests/test_storage.py: shared tests for both formats (same file as a full write, stored data not read, files from older versions, new and removed columns, trailing whitespace, Windows line endings, empty file) plus JSON-specific (compact JSON, nested values with}in the first record, empty array) and XML-specific (file stays valid XML) tests. 9 of them fail on the previous code; the "same file" test fails there because the full rewrite re-formatted earlier values.ruff checkpasses.Generated by Claude Code