Skip to content

ref(node): parallelize disk io when reading source files for context lines - #7374

Merged
AbhiPrasad merged 1 commit into
developfrom
ref/perf/contextlines
Mar 8, 2023
Merged

ref(node): parallelize disk io when reading source files for context lines#7374
AbhiPrasad merged 1 commit into
developfrom
ref/perf/contextlines

Conversation

@JonasBa

@JonasBaJonasBa commented Mar 7, 2023

Copy link
Copy Markdown
Contributor

I noticed a while back from a customer generated nodejs profile, that the sentry context lines integration was causing a significant slowdown when collecting an error event (iirc in the ballpark of ~200ms). In a long lived process, the cost of this is amortized as we start to hit the file cache more often, however in serverless environments like lambda functions where the cache is short lived, we may still be costing our users some precious execution time.

When I reported this to the SDK team internally and @AbhiPrasad noticed that we actually sequentially read from disk so I wanted to improve this. The changes I made in this PR parallelize i/o read calls and await adding context frames until the source files necessary to do the enrichment have been read.

I ran a quick micro benchmark with a stack trace of 5 frames and assuming the worst case of each file in stack trace being different.

Before/after
-> parallel io x 457,766 ops/sec ±5.07% (83 runs sampled)
-> sequential io x 327,708 ops/sec ±4.96% (81 runs sampled)

Benchmark

// executed with ts-nodeimport*asBenchmarkfrom'benchmark';import{ContextLines,resetFileContentCache}from'../src/integrations/contextlines';constlines=newContextLines({});constsource={exception: {values: [{stacktrace: {frames: [{colno: 1,filename: '/Users/jonasbadalic/code/sentry-javascript/packages/node/test/context-lines.benchmark.ts',lineno: 1,function: 'fxn1',},{colno: 1,filename: '/Users/jonasbadalic/code/sentry-javascript/packages/node/test/domain.test.ts',lineno: 1,function: 'fxn1',},{colno: 1,filename: '/Users/jonasbadalic/code/sentry-javascript/packages/node/test/sdk.test.ts',lineno: 1,function: 'fxn1',},{colno: 1,filename: '/Users/jonasbadalic/code/sentry-javascript/packages/node/test/stacktrace.test.ts',lineno: 1,function: 'fxn1',},{colno: 1,filename: '/Users/jonasbadalic/code/sentry-javascript/packages/node/test/utils.test.ts',lineno: 1,function: 'fxn1',},],},},],},};constsuite=newBenchmark.Suite({setup: resetFileContentCache,teardown: resetFileContentCache});suite.add('parallel io',asyncfunction(){awaitlines.addSourceContext(source);}).on('cycle',function(event: any){console.log(String(event.target));}).run({async: true});

@JonasBa
JonasBa requested review from AbhiPrasad and lforstMarch 7, 2023 16:52
@AbhiPrasad
AbhiPrasad requested a review from timfishMarch 7, 2023 16:54
@JonasBa
JonasBaforce-pushed the ref/perf/contextlines branch from 53e0afb to 2579956CompareMarch 7, 2023 17:08
@AbhiPrasad
AbhiPrasad merged commit a1dab3b into developMar 8, 2023
@AbhiPrasad
AbhiPrasad deleted the ref/perf/contextlines branch March 8, 2023 10:02
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@JonasBa@AbhiPrasad