Altinn / app-frontend-react

Altinn application React frontend
BSD 3-Clause "New" or "Revised" License
18 stars 31 forks source link

Tiny benchmarking harness #2699

Open martinothamar opened 2 weeks ago

martinothamar commented 2 weeks ago

Description

A benchmarking harness using tinybench as a suggestion to start tracking perf numbers. Using benchmarking provides us a paper trail - when we discover slow code that affect real usecases, we can provide benchmarks to show improvements in isolation. Upon future changes we can verify change in perf characteristics (for a variety of inputs) such as

Memory allocations is missing, I couldn't find a way to correctly correlate heap changes to benchmark runs, as there might be GCs occurring in the middle of runs.

The included benchmark is a microbenchmark comparing some implementations of splitDashedKey that did pop up in some profiling (although this is not an issue anymore, so it serves mostly as an example).

image

Related Issue(s)

Verification/QA

sonarcloud[bot] commented 2 weeks ago

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
7.9% Coverage on New Code (required ≥ 45%)
0.0% Condition Coverage on New Code (required ≥ 45%)

See analysis details on SonarCloud

adamhaeger commented 1 week ago

Hmm I dont really see how tinybench is useful for real world performance testing of a webapp. Seems more focused on low level testing of functions.

Id say some of the metrics we should focus on are:

  1. Component Render Times
    • Initial Render Time
    • Re-render Times
    • Wasted Renders
  2. React Reconciliation (Diffing Algorithm) Efficiency
  3. State and Prop Update Times
  4. Time to First Paint (TTFP) and First Contentful Paint (FCP)
  5. Memory Usage
  6. Time to Interactive (TTI)
  7. Largest Contentful Paint (LCP)
  8. Layout Shift (Cumulative Layout Shift - CLS)
  9. Network Request Time for Data Fetching
  10. JavaScript Bundle Size and Code Splitting

Tools to Measure These Metrics

We could for example set up lighthouse CI which provides many of these metrics: https://github.com/GoogleChrome/lighthouse-ci

Could fail the build if its too slow.

martinothamar commented 1 week ago

Seems more focused on low level testing of functions.

Agreed, and I think we need both. We need high level testing that uses metrics focused on UX, but during the optimization process of our code we also need benchmarking that verifies that we actually optimize.

The tools you mention are good for the explorative/analysis phase where you are running one-offs and high-level before/after. Benchmarks are good for iterating on optimizations when you've chosed your place of optimization. Proper measurement of optimization requires a lot of runs/executions so that the distribution of latency can be inspected. Higher level tests where you measure metrics to capture UX quality like the ones you mention can't be run a ton of times while also having a fast feedback loop