cisco-en-programmability / dnacentersdk

Cisco DNA Center Python SDK
https://dnacentersdk.readthedocs.io/en/latest/
MIT License
70 stars 31 forks source link

Lower import time + optional schema validation #129

Open steffenschumacher opened 9 months ago

steffenschumacher commented 9 months ago

Importing dnacentersdk is ~1 sec - half of this is importing the schema classes: https://ctrl.vi/i/hdKtAuyj6 For dev/test good validation desirable, but for production it is important to lower load time, memory, IO and CPU utilisation and assuming proper tests were done, it should be legitimate to run this without the validations. Even in dev/test, it is important to increase efficiency, thus using less power, save the planet and all that šŸ˜‰ and avoid wasting memory, CPU and pipeline time for loading irrelevant Schema classes for unused DNAC versions.

I propose to:

  1. lazily import schema classes in SchemaValidator.load_validators() - avoid using memory, IO, CPU on irrelevant DNAC Versions
  2. Add a NoSchemaValidator class, where json_schema_validate() returns itself, and also has a validate() which just does pass - can be used in production after validation to save even more memory, IO & CPU.

I considered just doing my own NoSchemaValidator to use for efficient production, but I would still import all the schema classes, no matter what I do - I would need to do something really shady, like manipulating the installed sdk.

To measure import times and display locally in browser:

python3.11 -m venv venv-dnac
pip install dnacentersdk tuna importTime
python -X importtime -c "from dnacentersdk import DNACenterAPI; print('done')" 2> dnac-import.log
tuna dnac-import.log