Giter Site home page Giter Site logo

dmngu9 / storybook-addon-performance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from atlassian-labs/storybook-addon-performance

0.0 1.0 0.0 443 KB

๐Ÿšง A storybook addon to help better understand and debug performance for React components.

Home Page: https://storybook-addon-performance.netlify.com

License: Other

JavaScript 1.96% TypeScript 98.04%

storybook-addon-performance's Introduction

storybook-addon-performance ๐Ÿš€

A storybook addon to help better understand and debug performance for React components

storybook-addon-performance demo

๐Ÿšง This addon is experimental and a work in progress. We are not on stable versions yet ๐Ÿšง

๐Ÿ“บ Project overview by Jack Herrington

Highlights ๐ŸŒŸ

  • Zero config (except for interactions): Generate performance information relating to server-side rendering and client-side mounting without any configuration
  • Pin results: You can run some tasks, pin the result, make some changes, rerun the tasks and see what changed
  • Interactions: Add your own custom user interactions to run as a parameter to your story. This lets you time how long interactions take. The API for this is super flexible and powerful!
  • Control: Run all tasks for an overview, or run individual tasks to drill down on specific problems
  • Marked: All tasks are marked with the User Timing API to allow for easy debugging of individual tasks in your browser's performance profiler

Marking tasks

Installation

  1. Install storybook-addon-performance
# yarn
yarn add storybook-addon-performance --dev

# npm
npm install storybook-addon-performance --save-dev
  1. Register the addon in .storybook/main.js
module.exports = {
  addons: ['storybook-addon-performance/register'],
};
  1. Add the decorator

You can either add the decorator globally to every story in .storybook/preview.js (recommended)

import { addDecorator } from '@storybook/react';
import { withPerformance } from 'storybook-addon-performance';

addDecorator(withPerformance);

Or you can add it to individual stories:

Using Component Story Format (CSF)

import MyComponent from './MyComponent';
import { withPerformance } from 'storybook-addon-performance';

export default {
  title: 'MyComponent',
  component: MyComponent,
  decorators: [withPerformance],
};

Using StoriesOf API

import MyComponent from './MyComponent';
import { withPerformance } from 'storybook-addon-performance';

storiesOf('MyComponent', module)
  .addDecorator(withPerformance)
  .add('MyComponent', () => <MyComponent />);

Usage: Interactions

Interaction tasks are a task type that can be defined and run on a story-by-story basis. They are useful for timing the interactive performance of your components.

To define your interaction tasks, first create an array of objects, each containing the name and description (optional) of the task, and a run function that performs whatever tasks you'd like to measure:

import { InteractionTaskArgs, PublicInteractionTask } from 'storybook-addon-performance';
import { findByText, fireEvent } from '@testing-library/dom';

// ...

const interactionTasks: PublicInteractionTask[] = [
  {
    name: 'Display dropdown',
    description: 'Open the dropdown and wait for Option 5 to load',
    run: async ({ container }: InteractionTaskArgs): Promise<void> => {
      const element: HTMLElement | null = container.querySelector('.addon__dropdown-indicator');
      invariant(element);
      fireEvent.mouseDown(element);
      await findByText(container, 'Option 5', undefined, { timeout: 20000 });
    },
  },
];

The run function in each task object takes two arguments:

  • container: an HTMLElement container that contains a rendered instance of the story component

  • controls: contains an async timing function that can be optionally called to specify when to start and finish measurements; otherwise the time taken to complete the entire run function is measured. Useful when a task involves some set-up work.

    To use, wrap the operations in question with controls.time as shown below:

    run: async ({ container }: InteractionTaskArgs): Promise<void> => {
      // setup
      await controls.time(async () => {
        // interaction task you'd like to measure
      });
    };

Note that you can use whatever libraries you'd like to perform these interaction tests โ€“ the example above uses @testing-library/dom to open the select in the example and wait for a specific item.

You can then include the array of interaction tasks inside the performance parameters of your story, with the key interactions:

select.story = {
  name: 'React select',
  parameters: {
    performance: {
      interactions: interactionTasks,
    },
  },
};

Supplied types

As seen above, the plugin exports two type definitions to assist with creating your own interaction tasks:

  • PublicInteractionTask: defines the object structure for an interaction task; pass an array of these tasks as a parameter to storybook, as shown above.
  • InteractionTaskArgs: the arguments for an interaction task's run function

Local addon development

# Start the typescript watcher and a local storybook:
yarn dev

# Start just the typescript watcher
# This is needed as storybook does not compile addons
yarn typescript:watch

# Start the local storybook
yarn storybook:dev

Thanks

Made with โค๏ธ by your friends at Atlassian


With โค๏ธ from Atlassian

storybook-addon-performance's People

Contributors

alexreardon avatar andrewocc avatar danieldelcore avatar obweger avatar czearing avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.