Skip to content

Commit

Permalink
add tests for SceneRenderProfiler
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarkilhed committed Nov 20, 2024
1 parent f888b12 commit e81de41
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
71 changes: 71 additions & 0 deletions packages/scenes/src/behaviors/SceneRenderProfiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { calculateNetworkTime, processRecordedSpans } from "./SceneRenderProfiler";

describe('calculateNetworkTime', () => {
it('should return the duration of a single request', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 0, responseEnd: 100 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(100);
});

it('should return the total time for non-overlapping requests', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 0, responseEnd: 100 } as PerformanceResourceTiming,
{ startTime: 200, responseEnd: 300 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(200);
});

it('should merge overlapping requests and return the correct total time', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 0, responseEnd: 100 } as PerformanceResourceTiming,
{ startTime: 50, responseEnd: 150 } as PerformanceResourceTiming,
{ startTime: 200, responseEnd: 300 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(250);
});

it('should handle multiple overlapping intervals', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 0, responseEnd: 200 } as PerformanceResourceTiming,
{ startTime: 100, responseEnd: 300 } as PerformanceResourceTiming,
{ startTime: 250, responseEnd: 350 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(350);
});

it('should handle multiple overlapping intervals in wrong order', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 100, responseEnd: 300 } as PerformanceResourceTiming,
{ startTime: 0, responseEnd: 200 } as PerformanceResourceTiming,
{ startTime: 250, responseEnd: 350 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(350);
});

it('should correctly calculate time with gaps between requests', () => {
const requests: PerformanceResourceTiming[] = [
{ startTime: 0, responseEnd: 100 } as PerformanceResourceTiming,
{ startTime: 150, responseEnd: 250 } as PerformanceResourceTiming,
{ startTime: 300, responseEnd: 400 } as PerformanceResourceTiming,
];
expect(calculateNetworkTime(requests)).toBe(300);
});
});

describe('processRecordedSpans', () => {
it('should return the whole array if the last element is greater than 30', () => {
const spans = [10, 10, 40];
expect(processRecordedSpans(spans)).toEqual([10, 10, 40]);
});

it('should return up to the last element greater than 30', () => {
const spans = [10, 20, 30, 40, 60, 10];
expect(processRecordedSpans(spans)).toEqual([10, 20, 30, 40, 60]);
});

it('should return only the first element if all are below or equal to 30', () => {
const spans = [10, 20, 15, 5];
expect(processRecordedSpans(spans)).toEqual([10]);
});
});
6 changes: 3 additions & 3 deletions packages/scenes/src/behaviors/SceneRenderProfiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export class SceneRenderProfiler {
}
}

function processRecordedSpans(spans: number[]) {
// identifie last span in spans that's bigger than 50
export function processRecordedSpans(spans: number[]) {
// identify last span in spans that's bigger than SPAN_THRESHOLD
for (let i = spans.length - 1; i >= 0; i--) {
if (spans[i] > SPAN_THRESHOLD) {
return spans.slice(0, i + 1);
Expand All @@ -163,7 +163,7 @@ function captureNetwork(startTs: number, endTs: number) {
}

// Will calculate total time spent on Network
function calculateNetworkTime(requests: PerformanceResourceTiming[]): number {
export function calculateNetworkTime(requests: PerformanceResourceTiming[]): number {
if (requests.length === 0) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Observable, catchError, from, map } from 'rxjs';
import { LoadingState } from '@grafana/schema';
import { sceneGraph } from '../core/sceneGraph';
import { QueryResultWithState, SceneQueryControllerEntry } from '../behaviors/types';
import { writeSceneLog } from '../utils/writeSceneLog';

/**
* Will look for a scene object with a behavior that is a SceneQueryController and register the query with it.
Expand Down

0 comments on commit e81de41

Please sign in to comment.