vendredi 28 juin 2019

Instrumenting Mocha tests with custom performance monitoring

I've got a set of Mocha tests that have a nasty habit of timing out on CI non-deterministically.

A lot of these tests are asynchronous - they make heavy use of async/await and promises - and it seems like certain async calls are being problematic.

However, it can be hard to diagnose issues without manually instrumenting the tests, inserting scores of lines like console.log('ln 88: ', Date.now()) in a bid to watch where tests are being held up, and get granular timing on which parts of our tests are being slow.

I had a thought that I could write some automated instrumentation that could identify async expressions and wrap them in a way that lets me log their performance.

I reasoned I could switch this on whenever the tests were causing me trouble. I could also use it in other projects to do detailed profiling of my tests in CI (which don't always have the same performance characteristics as when running locally).

I have some ideas as to how I can do the instrumentation - either via AST transforms or even crude string replacement - what I'm not sure is where / when.

Is my only option to write a Babel plugin, and pass Babel over my test source files? Or does Mocha provide any kind of hook in which I can transform test functions? Are there any other options?

Aucun commentaire:

Enregistrer un commentaire