Skip to content

Conversation

@JonasBa
Copy link
Member

@JonasBa JonasBa commented Mar 7, 2023

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-node
import * as Benchmark from 'benchmark';
import { ContextLines, resetFileContentCache } from '../src/integrations/contextlines';

const lines = new ContextLines({});

const source = {
  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',
            },
          ],
        },
      },
    ],
  },
};

const suite = new Benchmark.Suite({ setup: resetFileContentCache, teardown: resetFileContentCache });

suite
  .add('parallel io', async function () {
    await lines.addSourceContext(source);
  })
  .on('cycle', function (event: any) {
    console.log(String(event.target));
  })
  .run({ async: true });

@JonasBa JonasBa requested review from AbhiPrasad and lforst March 7, 2023 16:52
@AbhiPrasad AbhiPrasad requested a review from timfish March 7, 2023 16:54
@JonasBa JonasBa force-pushed the ref/perf/contextlines branch from 53e0afb to 2579956 Compare March 7, 2023 17:08
@AbhiPrasad AbhiPrasad merged commit a1dab3b into develop Mar 8, 2023
@AbhiPrasad AbhiPrasad deleted the ref/perf/contextlines branch March 8, 2023 10:02
Sign up for free to 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