go-openapi / spec

openapi specification object model
Apache License 2.0
394 stars 100 forks source link

Heavy init() calls dramatically slow down tests #138

Closed howardjohn closed 3 years ago

howardjohn commented 3 years ago

Just starting a test binary, compiled with -race, which depends on this library:

$ hyperfine './xds.test -test.run ^$' -w 1 -r 25
Benchmark #1: ./xds.test -test.run ^$
  Time (mean ± σ):     248.4 ms ±  15.1 ms    [User: 224.9 ms, System: 42.7 ms]
  Range (min … max):   216.8 ms … 276.5 ms    25 runs

Modifying the library so that MustLoadJSONSchemaDraft04 and MustLoadSwagger20Schema do nothing:

$ hyperfine './xds.test -test.run ^$' -w 1 -r 25
Benchmark #1: ./xds.test -test.run ^$
  Time (mean ± σ):     144.2 ms ±  14.0 ms    [User: 112.3 ms, System: 41.2 ms]
  Range (min … max):   123.9 ms … 174.4 ms    25 runs

You can see its almost 2x faster

Currently the library is embedding a json string from go:generate. Maybe it would be more efficient to generate the go struct directly?

howardjohn commented 3 years ago

By the way, the time to start the binary does matter, as we run the binary 1000s of times to detect test flakes. So when the init time accounts for ~250ms, it really slows down the tests

fredbi commented 3 years ago

@howardjohn on recent versions, I've removed the need to load these assets in init() and this is now initialized lazily when first calling expansion. If you are not using Expand, no heavy init() should be performed, and no memory allocated for the static assets.

howardjohn commented 3 years ago

Fantastic! I don't know how I missed that originally, sorry for the noise