Skip to content

Improve performance of writing and serving JSON IIIF objects

Giacomo Marchioro edited this page Oct 11, 2021 · 2 revisions

If you are saving the JSON files in your server there is usually no need to improve the performance. However, if you are planning to create the JSON IIIF objects (manifests, collection...) "on-the-fly" each time the user requests them or caching the results temporarily you might want to improve the writing speed of the manifest.

tests/performance/performance_test.py

offers a way to test the performance of your server in serving a manifest with 2000 canvas and 2000 annotations, and one of 4000 canvas and 40000 annotations. Feel free to do a pull request with the results of the test which are appended to the csv files in the same folder.

If the speed is not enough for your needs, you can try one of the following actions or both:

  1. Install orjson: pip install orjson and use myIIIFobject.orjson_save() or .orjson_dumps() instead of myIIIFobject.json_save() or .json_dumps()
  2. ⚠️Check the IIIF JSON object once and then run optimized Python code using the -O flag e.g. python -O 0001-mvm-image.py⚠️

orjson is a much faster parser compared to the standard json module.

While the -O flag basically ⚠️removes all the assertions and most of the helper classes⚠️. Hence you should use it with caution. One strategy could be to check if the IIIF Object is valid when you insert it in your digital library or you modify it without the flag and then use the optimized version to serve it, without the need of using other tools.

An Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz using pyIIIFpres can produce 4000 canvas and 40000 annotations in 2.26 seconds, using orjson in 1.19 seconds and with the optimization in 0.48 seconds.