123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- import {
- MockSpan,
- ProblemSpan,
- TransactionEventBuilder,
- } from 'sentry-test/performance/utils';
- import {render, screen} from 'sentry-test/reactTestingLibrary';
- import {EntryType, IssueType} from 'sentry/types';
- import {
- extractQueryParameters,
- extractSpanURLString,
- SpanEvidenceKeyValueList,
- } from './spanEvidenceKeyValueList';
- describe('SpanEvidenceKeyValueList', () => {
- describe('N+1 Database Queries', () => {
- const builder = new TransactionEventBuilder('a1', '/');
- const parentSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 0.2,
- op: 'http.server',
- problemSpan: ProblemSpan.PARENT,
- });
- parentSpan.addChild({
- startTimestamp: 0.01,
- endTimestamp: 2.1,
- op: 'db',
- description: 'SELECT * FROM books',
- problemSpan: ProblemSpan.OFFENDER,
- });
- parentSpan.addChild({
- startTimestamp: 2.1,
- endTimestamp: 4.0,
- op: 'db',
- description: 'SELECT * FROM books',
- problemSpan: ProblemSpan.OFFENDER,
- });
- builder.addSpan(parentSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Parent Span'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.parent-span')
- ).toHaveTextContent('http.server');
- expect(screen.getByRole('cell', {name: 'Repeating Spans (2)'})).toBeInTheDocument();
- expect(
- screen.getByTestId(/span-evidence-key-value-list.repeating-spans/)
- ).toHaveTextContent('db - SELECT * FROM books');
- expect(
- screen.queryByTestId('span-evidence-key-value-list.')
- ).not.toBeInTheDocument();
- expect(screen.queryByRole('cell', {name: 'Parameter'})).not.toBeInTheDocument();
- expect(
- screen.queryByTestId('span-evidence-key-value-list.problem-parameters')
- ).not.toBeInTheDocument();
- });
- });
- describe('MN+1 Database Queries', () => {
- const builder = new TransactionEventBuilder('a1', '/');
- const parentSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 0.2,
- op: 'http.server',
- problemSpan: ProblemSpan.PARENT,
- });
- parentSpan.addChild({
- startTimestamp: 0.01,
- endTimestamp: 2.1,
- op: 'db',
- description: 'SELECT * FROM books',
- problemSpan: ProblemSpan.OFFENDER,
- });
- parentSpan.addChild({
- startTimestamp: 2.1,
- endTimestamp: 4.0,
- op: 'db.sql.active_record',
- description: 'SELECT * FROM books WHERE id = %s',
- problemSpan: ProblemSpan.OFFENDER,
- });
- builder.addSpan(parentSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Parent Span'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.parent-span')
- ).toHaveTextContent('http.server');
- expect(screen.getByRole('cell', {name: 'Repeating Spans (2)'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.repeating-spans-2')
- ).toHaveTextContent('db - SELECT * FROM books');
- expect(screen.getByTestId('span-evidence-key-value-list.')).toHaveTextContent(
- 'db.sql.active_record - SELECT * FROM books WHERE id = %s'
- );
- expect(screen.queryByRole('cell', {name: 'Parameter'})).not.toBeInTheDocument();
- expect(
- screen.queryByTestId('span-evidence-key-value-list.problem-parameters')
- ).not.toBeInTheDocument();
- });
- });
- describe('Consecutive DB Queries', () => {
- const builder = new TransactionEventBuilder(
- 'a1',
- '/',
- IssueType.PERFORMANCE_CONSECUTIVE_DB_QUERIES
- );
- const parentSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 0.65,
- op: 'http.server',
- problemSpan: ProblemSpan.PARENT,
- });
- parentSpan.addChild({
- startTimestamp: 0.1,
- endTimestamp: 0.2,
- op: 'db',
- description: 'SELECT * FROM USERS LIMIT 100',
- problemSpan: ProblemSpan.CAUSE,
- });
- parentSpan.addChild({
- startTimestamp: 0.2,
- endTimestamp: 0.4,
- op: 'db',
- description: 'SELECT COUNT(*) FROM USERS',
- problemSpan: [ProblemSpan.CAUSE, ProblemSpan.OFFENDER],
- });
- parentSpan.addChild({
- startTimestamp: 0.4,
- endTimestamp: 0.6,
- op: 'db',
- description: 'SELECT COUNT(*) FROM ITEMS',
- problemSpan: [ProblemSpan.CAUSE, ProblemSpan.OFFENDER],
- });
- builder.addSpan(parentSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Starting Span'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.starting-span')
- ).toHaveTextContent('db - SELECT * FROM USERS LIMIT 100');
- expect(screen.queryAllByRole('cell', {name: 'Parallelizable Spans'}).length).toBe(
- 1
- );
- const parallelizableSpanKeyValue = screen.getByTestId(
- 'span-evidence-key-value-list.parallelizable-spans'
- );
- expect(parallelizableSpanKeyValue).toHaveTextContent(
- 'db - SELECT COUNT(*) FROM USERS'
- );
- expect(parallelizableSpanKeyValue).toHaveTextContent(
- 'db - SELECT COUNT(*) FROM ITEMS'
- );
- expect(
- screen.getByTestId('span-evidence-key-value-list.duration-impact')
- ).toHaveTextContent('46% (300ms/650ms)');
- });
- });
- describe('N+1 API Calls', () => {
- const builder = new TransactionEventBuilder(
- 'a1',
- '/',
- IssueType.PERFORMANCE_N_PLUS_ONE_API_CALLS
- );
- const parentSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 200,
- op: 'pageload',
- problemSpan: ProblemSpan.PARENT,
- });
- parentSpan.addChild({
- startTimestamp: 10,
- endTimestamp: 2100,
- op: 'http.client',
- description: 'GET /book/?book_id=7&sort=up',
- problemSpan: ProblemSpan.OFFENDER,
- });
- parentSpan.addChild({
- startTimestamp: 10,
- endTimestamp: 2100,
- op: 'http.client',
- description: 'GET /book/?book_id=8&sort=down',
- problemSpan: ProblemSpan.OFFENDER,
- });
- builder.addSpan(parentSpan);
- builder.addEntry(
- TestStubs.EventEntry({
- type: EntryType.REQUEST,
- data: {
- url: 'http://some.service.io',
- },
- })
- );
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Repeating Spans (2)'})).toBeInTheDocument();
- expect(
- screen.getByTestId(/span-evidence-key-value-list.repeating-spans/)
- ).toHaveTextContent('/book/[Parameters]');
- expect(screen.queryByRole('cell', {name: 'Parameters'})).toBeInTheDocument();
- const parametersKeyValue = screen.getByTestId(
- 'span-evidence-key-value-list.parameters'
- );
- expect(parametersKeyValue).toHaveTextContent('book_id:{7,8}');
- expect(parametersKeyValue).toHaveTextContent('sort:{up,down}');
- });
- describe('extractSpanURLString', () => {
- it('Tries to pull a URL from the span data', () => {
- expect(
- extractSpanURLString({
- span_id: 'a',
- data: {
- url: 'http://service.io',
- },
- })?.toString()
- ).toEqual('http://service.io/');
- });
- it('Pulls out a relative URL if a base is provided', () => {
- expect(
- extractSpanURLString(
- {
- span_id: 'a',
- data: {
- url: '/item',
- },
- },
- 'http://service.io'
- )?.toString()
- ).toEqual('http://service.io/item');
- });
- it('Falls back to span description if URL is faulty', () => {
- expect(
- extractSpanURLString({
- span_id: 'a',
- description: 'GET http://service.io/item',
- data: {
- url: '/item',
- },
- })?.toString()
- ).toEqual('http://service.io/item');
- });
- });
- describe('extractQueryParameters', () => {
- it('If the URLs have no parameters or are malformed, returns nothing', () => {
- const URLs = [
- new URL('http://service.io/items'),
- new URL('http://service.io/values'),
- ];
- expect(extractQueryParameters(URLs)).toEqual({});
- });
- it('If the URLs have one changing parameter, returns it and its values', () => {
- const URLs = [
- new URL('http://service.io/items?id=4'),
- new URL('http://service.io/items?id=5'),
- new URL('http://service.io/items?id=6'),
- ];
- expect(extractQueryParameters(URLs)).toEqual({
- id: ['4', '5', '6'],
- });
- });
- it('If the URLs have multiple changing parameters, returns them and their values', () => {
- const URLs = [
- new URL('http://service.io/items?id=4&sort=down&filter=none'),
- new URL('http://service.io/items?id=5&sort=up&filter=none'),
- new URL('http://service.io/items?id=6&sort=up&filter=none'),
- ];
- expect(extractQueryParameters(URLs)).toEqual({
- id: ['4', '5', '6'],
- sort: ['down', 'up'],
- filter: ['none'],
- });
- });
- });
- });
- describe('Slow DB Span', () => {
- const builder = new TransactionEventBuilder(
- 'a1',
- '/',
- IssueType.PERFORMANCE_SLOW_DB_QUERY
- );
- const parentSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 200,
- op: 'pageload',
- problemSpan: ProblemSpan.PARENT,
- });
- parentSpan.addChild({
- startTimestamp: 10,
- endTimestamp: 10100,
- op: 'db',
- description: 'SELECT pokemon FROM pokedex',
- problemSpan: ProblemSpan.OFFENDER,
- });
- builder.addSpan(parentSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Slow DB Query'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.slow-db-query')
- ).toHaveTextContent('SELECT pokemon FROM pokedex');
- expect(screen.getByRole('cell', {name: 'Duration Impact'})).toBeInTheDocument();
- });
- });
- describe('Render Blocking Asset', () => {
- const builder = new TransactionEventBuilder(
- 'a1',
- '/',
- IssueType.PERFORMANCE_RENDER_BLOCKING_ASSET,
- {
- duration: 3,
- fcp: 2500,
- }
- );
- const offenderSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 1.0,
- op: 'resource.script',
- description: 'https://example.com/resource.js',
- problemSpan: ProblemSpan.OFFENDER,
- });
- builder.addSpan(offenderSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Slow Resource Span'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.slow-resource-span')
- ).toHaveTextContent('resource.script - https://example.com/resource.js');
- expect(screen.getByRole('cell', {name: 'FCP Delay'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.fcp-delay')
- ).toHaveTextContent('1s (40% of 2.50s)');
- expect(screen.getByRole('cell', {name: 'Duration Impact'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.duration-impact')
- ).toHaveTextContent('33% (1s/3.00s');
- });
- });
- describe('Uncompressed Asset', () => {
- const builder = new TransactionEventBuilder(
- 'a1',
- '/',
- IssueType.PERFORMANCE_UNCOMPRESSED_ASSET,
- {
- duration: 0.931, // in seconds
- }
- );
- const offenderSpan = new MockSpan({
- startTimestamp: 0,
- endTimestamp: 0.487, // in seconds
- op: 'resource.script',
- description: 'https://example.com/resource.js',
- problemSpan: ProblemSpan.OFFENDER,
- data: {
- 'Encoded Body Size': 31041901,
- },
- });
- builder.addSpan(offenderSpan);
- it('Renders relevant fields', () => {
- render(<SpanEvidenceKeyValueList event={builder.getEvent()} />);
- expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.transaction')
- ).toHaveTextContent('/');
- expect(screen.getByRole('cell', {name: 'Slow Resource Span'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.slow-resource-span')
- ).toHaveTextContent('resource.script - https://example.com/resource.js');
- expect(screen.getByRole('cell', {name: 'Asset Size'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.asset-size')
- ).toHaveTextContent('29.6 MiB (31041901 B)');
- expect(screen.getByRole('cell', {name: 'Duration Impact'})).toBeInTheDocument();
- expect(
- screen.getByTestId('span-evidence-key-value-list.duration-impact')
- ).toHaveTextContent('52% (487ms/931ms)');
- });
- });
- });
|