Skip to content

Commit

Permalink
Refactor react perf profiler for local dev, local data collection (#6186
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kyledurand committed Jun 16, 2022
1 parent a622544 commit 2ba83b7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-maps-watch.md
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Add react profiler log data option to storybook globals
@@ -1,5 +1,4 @@
import React, {PropsWithChildren} from 'react';
import isChromatic from 'chromatic/isChromatic';
import {version} from '../../package.json';

interface Data {
Expand All @@ -15,35 +14,26 @@ interface ProfileProps {
kind: string;
}

const isDevelopment = process.env.NODE_ENV === 'development';

const trackRenderPerformance = (data: Data) => {
const commitSha = process.env.STORYBOOK_GITHUB_SHA
? process.env.STORYBOOK_GITHUB_SHA
: 'localdev';

const body = JSON.stringify({...data, commitSha, version});

const target = isDevelopment
? '//localhost:3000/api/profiler'
: 'https://polaris-coverage.shopifycloud.com/api/profiler';

fetch(target, {
method: 'POST',
keepalive: true,
mode: 'no-cors',
body,
});
console.log({...data, commitSha, version});
};

const Profiler = ({id, kind, children}: PropsWithChildren<ProfileProps>) => (
export const RenderPerformanceProfiler = ({
id,
kind,
children,
}: PropsWithChildren<ProfileProps>) => (
<React.Profiler
id={id}
// https://reactjs.org/docs/profiler.html#onrender-callback
onRender={(_, phase, actualDuration, baseDuration) => {
trackRenderPerformance({
id,
kind,
kind: kind.replace('All Components/', ''),
phase,
actualDuration,
baseDuration,
Expand All @@ -53,8 +43,3 @@ const Profiler = ({id, kind, children}: PropsWithChildren<ProfileProps>) => (
{children}
</React.Profiler>
);

const Component = ({children}: PropsWithChildren<{}>) => <>{children}</>;

export const RenderPerformanceProfiler =
isDevelopment || isChromatic() ? Profiler : Component;
4 changes: 2 additions & 2 deletions polaris-react/.storybook/manager.js
Expand Up @@ -71,13 +71,13 @@ function GridPanel(props) {
name: 'Show grid overlay',
description:
'Show or hide a 4 / 12 column grid, overlaying components',
defaultValue: false,
defaultValue: 'false',
control: {type: 'boolean'},
},
gridInFrame: {
name: 'Grid in frame',
description: 'Show grid within app frame context',
defaultValue: false,
defaultValue: 'false',
control: {type: 'boolean'},
},
gridWidth: {
Expand Down
25 changes: 20 additions & 5 deletions polaris-react/.storybook/preview.js
Expand Up @@ -41,11 +41,15 @@ function GridOverlayDecorator(Story, context) {
);
}

function RenderPerformanceProfilerDecorator(Story, context) {
function ReactRenderProfiler(Story, context) {
const {profiler} = context.globals;
const Wrapper = profiler ? RenderPerformanceProfiler : React.Fragment;
const props = profiler ? {id: context.id, kind: context.kind} : {};

return (
<RenderPerformanceProfiler id={context.id} kind={context.kind}>
<Wrapper {...props}>
<Story {...context} />
</RenderPerformanceProfiler>
</Wrapper>
);
}

Expand All @@ -58,7 +62,18 @@ export const globalTypes = {
{title: 'Disabled', value: 'false'},
{title: 'Enabled', value: 'true'},
],
showName: true,
showName: 'true',
},
},
profiler: {
name: 'React.Profiler',
defaultValue: 'false',
toolbar: {
items: [
{title: 'Disabled', value: 'false'},
{title: 'Enabled', value: 'true'},
],
showName: 'true',
},
},
};
Expand All @@ -67,5 +82,5 @@ export const decorators = [
GridOverlayDecorator,
StrictModeDecorator,
AppProviderDecorator,
RenderPerformanceProfilerDecorator,
ReactRenderProfiler,
];

0 comments on commit 2ba83b7

Please sign in to comment.