@s-ui/perf

1.10.0 • Public • Published

sui-perf

Scoped javaScript timer based on performance.mark() and performance.measure()

Motivation

sui-perf is based on marky, but adds key features oriented to performance debugging on server side:

  • Scoped instances of performance, so several request can be tracked separately (despite they're simultaneous).
  • Text-based timeline chart generation, so you can check performances as in dev tools.

example

Usage

Mark your measurements and print them as a timeline.

perf.mark('Example 2')
perf.measure('Example 2 - task(70)')(() => task(70))
perf.measure('Example 2 - asyncTask(200)')(() => asyncTask(200))
  .then(x => perf.stop('Example 2'))
  .then(x => perf.getEntries())
  .then(printTimelineChart())

Output:

example-2

Charts can get as complex as needed to find out the blockers of your code. Check out examples running npm run examples:

example-3

Installation

npm install @s-ui/perf --save

API Reference

Measurements

getPerf(scopeUniqueId)

Get an instance of perf which measurements are scoped to this instance.

import getPerf from '@s-ui/perf'

const perf = getPerf('my-request-unique-id')
perf.mark('Request')
/* ... */
perf.stop('Request')

perf.mark(name)

Mark the beginning of a measurement.

perf.stop(name)

Mark the ending of a measurement. This creates a new a new entry with given name.

perf.getEntries()

Get entries of given instances. The entries follow structure of browser's PerformanceEntry instances.

perf.mark('task1'); /* ... */ perf.stop('task1')
perf.mark('task2'); /* ... */ perf.stop('task2')
console.log(perf.getEntries())

/*
[ { startTime: 134.809812, name: 'task1', duration: 564.807043, entryType: 'measure' },
  { startTime: 135.348685, name: 'task2', duration: 490.119122, entryType: 'measure' } ]
 */

perf.measure(label)(func)

Measure the execution of given function and return what the function returns.

In case a instance of Promise is returned, the measurement will measure the actual promise.

Original:

fetch('api.domain.com/user/58')
  .then(response => response.json())
  .then(console.log)

Measuring fetch:

perf.measure('Fetch user')
  (() => fetch('api.domain.com/user/58') )
  .then(response => response.json())
  .then(console.log)

Charts

@s-ui/perf/lib/chart has no dependency of and can be imported separately.

getTimelineChart(timelineOptions)(entries)

Get a text-based timeline chart from given entries. Check timelineOptions.

import {getTimelineChart} from '@s-ui/perf/lib/charts'

const options = {/* your custom options */}
const timeline = getTimelineChart(options)(perf.getEntries())
console.log(timeline)
/*
timeline        start  time   %    label
███████████████ 0ms    138ms  100% Example 2
████            0ms    33ms   24%  Example 2 - task(70)
    ███████████ 33ms   105ms  76%  Example 2 - asyncTask(200)
 */

printTimelineChart(timelineOptions)(entries)

A shorthand to directly print the chart with console.log. Check timelineOptions.

import {printTimelineChart} from '@s-ui/perf/lib/charts'
const options = {/* your custom options */}
printTimelineChart(options)(perf.getEntries())
/*
timeline        start  time   %    label
███████████████ 0ms    138ms  100% Example 2
████            0ms    33ms   24%  Example 2 - task(70)
    ███████████ 33ms   105ms  76%  Example 2 - asyncTask(200)
 */

timelineOptions

width=15

Length of the chart. The number stands for number of chars width, as its a text-based char.

timeRange

Time range, in milliseconds. By default it's the longest measurement. But, if can be usefull to set a fix timeRange to compare 2 different sets of data. For instance if you want to component different response times on the same url.

minPercent=0

Filters times than are less than given percent. For instance, is minPercent is set to 5, request than are less than 5% of time range will not be displayed.

Special measurements

measureSuperagent(superagent, perf)

Measure all requests made with the given instance of superagent.

import superagent from 'superagent'
import getPerf from '@s-ui/perf'
import measureSuperagent from '@s-ui/perf/lib/measure-superagent'

const perf = getPerf('my-request-unique-id')
const clearMeasureSuperAgent =  measureSuperagent(superagent, perf)

/* ... */
console.log(perf.getEntries())
clearMeasureSuperAgent()

measureReact(perf)

Measure mounting of all React components, either on client or server.

import getPerf from '@s-ui/perf'
import measureReact from '@s-ui/perf/lib/measure-react'

const perf = getPerf('my-request-unique-id')
const clearMeasureReact =  measureReact(perf)

/* ... */
console.log(perf.getEntries())
clearMeasureReact()

measureAxios(perf)(axios)

Measure all axios requests.

import axios from 'axios'
import getPerf from '@s-ui/perf'
import measureAxios from '@s-ui/perf/lib/measure-axios'

/* ... */

const axiosInstance = axios.create()
const perf = getPerf('my-request-unique-id')
const clearMeasureAxios = measureAxios(perf)(axiosInstance)

/* ... */
console.log(perf.getEntries())
clearMeasureAxios()

Contributing

Please refer to the main repo contributing info.

Readme

Keywords

none

Package Sidebar

Install

npm i @s-ui/perf

Weekly Downloads

1

Version

1.10.0

License

MIT

Unpacked Size

53.7 kB

Total Files

12

Last publish

Collaborators

  • alejandro.ferrante
  • ivanmlaborda
  • salvador.juan
  • andresin87.adevinta
  • izeller
  • gfabregoadv
  • a.ferrer
  • ignacio_navarro
  • oscar_ramirez
  • jordi.munoz
  • joanleon-adv
  • aitor.rodriguez
  • luis-garrido
  • jenifer.lopez
  • isabelgomez87
  • pa.chruscinski.ext
  • schibstedspain
  • alfredo.arronte
  • belen.santos
  • xavi_ballestar
  • ferrangbtw
  • jamile.radloff
  • davidmartin2108
  • sergi.quintela
  • estefania_garcia
  • carlosvillu-adevinta
  • miriam-gil
  • arnau.guell
  • ferran.simon
  • victor.perez.adevinta
  • mariapaula.forero.ext
  • oscar.gomez
  • david.nieto
  • oriol.puig
  • nacho_torrella
  • xavi.murcia
  • ignacio.rodriguez
  • javier.miguel
  • francisco.ruiz.lloret
  • sziauberyte
  • alfredo.zimperz
  • andresadv
  • javiauso
  • alverd004
  • marian.lucaci
  • pablogs
  • alisa_bayanova
  • cristhianb
  • sergiocollado
  • pablo.rey-adevinta
  • beatrizip
  • alex.castells
  • david.cuadrado.ext
  • giovanny.sayas.ext
  • patricio.sartore
  • azahara
  • marc.benito
  • sergio.escano
  • cristina.rodriguez.duque
  • pol.valls
  • frandelacasa-adevinta
  • carolina.mallo.ext
  • daniel.perez.ext
  • hpintos_adevinta
  • carlos.gonzalezl
  • albert.peiro
  • oscar-raig-adevinta
  • thomas.page.ext
  • sebastian.badea.adevinta
  • victoria.pasichnyk.ext
  • sendami.luque.ext
  • luz_adv
  • alfredo.narvaez
  • ruben-martin
  • dann41
  • emiliovz
  • adria.velardos
  • arturo.vicente
  • diegomr
  • sergi.martinez.adevinta
  • guillemgc3