tamj0rd2/ncdc

Try to improve speed of generate command

tamj0rd2 opened this issue · 6 comments

debug: Metric: Program started
debug: Metric: build a typescript program - started | time passed: 0.084s | time since start: 0.084s
debug: Metric: build a typescript program - completed | time passed: 4.375s | time since start: 4.459s
debug: Metric: build a schema generator - started | time passed: 0.001s | time since start: 4.46s
debug: Metric: build a schema generator - completed | time passed: 5.618s | time since start: 10.078s
debug: Metric: load schema for MySchema1 - started | time passed: 0.001s | time since start: 10.079s
debug: Metric: load schema for MySchema1 - completed | time passed: 0.068s | time since start: 10.147s
debug: Metric: load schema for MySchema2 - started | time passed: 0.001s | time since start: 10.148s
debug: Metric: load schema for MySchema2 - completed | time passed: 0.017s | time since start: 10.165s
info: JSON schemas have been written to disk
debug: Metric: Happy ending | time passed: 0.001s | time since start: 10.166s

The above output was from within docker. Here's the actual output when run locally:

debug: Metric: Program started
debug: Metric: build a typescript program - started | time passed: 0.151s | time since start: 0.151s
debug: Metric: build a typescript program - completed | time passed: 4.272s | time since start: 4.423s
debug: Metric: build a schema generator - started | time passed: 0.002s | time since start: 4.425s
debug: Metric: build a schema generator - completed | time passed: 2.731s | time since start: 7.156s
debug: Metric: load schema for Schema1 - started | time passed: 0.001s | time since start: 7.157s
debug: Metric: load schema for Schema1 - completed | time passed: 0.035s | time since start: 7.192s
debug: Metric: load schema for Schema2 - started | time passed: 0.001s | time since start: 7.193s
debug: Metric: load schema for Schema2 - completed | time passed: 0.009s | time since start: 7.202s
info: JSON schemas have been written to disk
debug: Metric: Happy ending | time passed: 0.006s | time since start: 7.208s

So the generation only takes ~2.7 seconds which seems decent. I wonder if switching to the other schema lib would help speed things up though.

And it doesn't help that this stuff in generate.ts is blocking. This can be made async, although there won't be a big difference. The loading step already takes nano seconds.

  for (const type of types) {
    const schema = await schemaRetriever.load(type)
    const formatted = JSON.stringify(schema, null, 2)
    writeFileSync(resolve(outDir, `${type}.json`), formatted, 'utf8')
  }

Here's the output after switching to ts-json-schema-generator :O

debug: Metric: Program started
debug: Metric: build a typescript program - started | time passed: 0.067s | time since start: 0.067s
debug: Metric: build a typescript program - completed | time passed: 4.014s | time since start: 4.081s
debug: Metric: build a schema generator - started | time passed: 0s | time since start: 4.081s
debug: Metric: build a schema generator - completed | time passed: 0.009s | time since start: 4.09s
debug: Metric: load schema for Schema1 - started | time passed: 0s | time since start: 4.09s
debug: Metric: load schema for Schema1 - completed | time passed: 0.025s | time since start: 4.115s
debug: Metric: load schema for Schema2 - started | time passed: 0s | time since start: 4.115s
debug: Metric: load schema for Schema2 - completed | time passed: 0.011s | time since start: 4.126s
info: JSON schemas have been written to disk
debug: Metric: Happy ending | time passed: 0.009s | time since start: 4.135s

I wonder if the unknown properties thing is what the makes the difference, or if it's something else.

Related to #35

FYI, this is dependant on #35

I wanted to test what impact the initial typecheck has

  • Without the -f flag: avg 3.03s
  • With the -f flag: avg 2.312s

It speeds things up, but I still don't want this to be the default option. That's because if people aren't running the typescript compiler themselves to check if their stuff compiles (yes, people actually do that), they'll probably get some weird behaviour when using ncdc. I'll update the readme mentioning that this flag can help speed things up

Time run the command in docker:

Before changes: 10-11seconds
After: 5 seconds (with the force flag)