|
| 1 | +# @react-native-windows/perf-testing |
| 2 | + |
| 3 | +Performance testing utilities for React Native Windows components. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This package provides infrastructure for measuring and tracking performance of React Native components. It enables: |
| 8 | + |
| 9 | +- **Automated performance regression detection** on every PR |
| 10 | +- **Component-wise performance baselines** (mount, unmount, re-render times) |
| 11 | +- **Lightweight perf snapshots** similar to Jest snapshots |
| 12 | +- **Easy extensibility** for new components via base classes |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @react-native-windows/perf-testing --save-dev |
| 18 | +# or |
| 19 | +yarn add @react-native-windows/perf-testing --dev |
| 20 | +``` |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +### 1. Create a Performance Test |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { ComponentPerfTestBase, IScenario } from '@react-native-windows/perf-testing'; |
| 28 | +import { View } from 'react-native'; |
| 29 | + |
| 30 | +class ViewPerfTest extends ComponentPerfTestBase { |
| 31 | + readonly componentName = 'View'; |
| 32 | + readonly category = 'core' as const; |
| 33 | + readonly testId = 'perf-test-view'; |
| 34 | + |
| 35 | + createComponent(props?: Record<string, unknown>) { |
| 36 | + return <View testID={this.testId} {...props} />; |
| 37 | + } |
| 38 | + |
| 39 | + getCustomScenarios(): IScenario[] { |
| 40 | + return [ |
| 41 | + { |
| 42 | + name: 'nested-views', |
| 43 | + description: 'Test nested views performance', |
| 44 | + run: () => this.measureNestedViews(50), |
| 45 | + }, |
| 46 | + ]; |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### 2. Write Tests |
| 52 | + |
| 53 | +```typescript |
| 54 | +const viewPerfTest = new ViewPerfTest(); |
| 55 | + |
| 56 | +describe('View Performance', () => { |
| 57 | + test('mount time', async () => { |
| 58 | + const perf = await viewPerfTest.measureMount(); |
| 59 | + expect(perf).toMatchPerfSnapshot(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('rerender time', async () => { |
| 63 | + const perf = await viewPerfTest.measureRerender(); |
| 64 | + expect(perf).toMatchPerfSnapshot(); |
| 65 | + }); |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. Run Tests |
| 70 | + |
| 71 | +```bash |
| 72 | +# Run perf tests |
| 73 | +yarn perf |
| 74 | + |
| 75 | +# Update baselines |
| 76 | +yarn perf:update |
| 77 | +``` |
| 78 | + |
| 79 | +## API Reference |
| 80 | + |
| 81 | +### Core Functions |
| 82 | + |
| 83 | +| Function | Description | |
| 84 | +|----------|-------------| |
| 85 | +| `measurePerf(component, options)` | Core measurement function | |
| 86 | +| `toMatchPerfSnapshot(threshold?)` | Jest matcher for perf snapshots | |
| 87 | + |
| 88 | +### Base Classes |
| 89 | + |
| 90 | +| Class | Description | |
| 91 | +|-------|-------------| |
| 92 | +| `ComponentPerfTestBase` | Abstract base class for component perf tests | |
| 93 | + |
| 94 | +### Interfaces |
| 95 | + |
| 96 | +| Interface | Description | |
| 97 | +|-----------|-------------| |
| 98 | +| `PerfMetrics` | Measurement results | |
| 99 | +| `PerfThreshold` | Pass/fail thresholds | |
| 100 | +| `IComponentPerfTest` | Contract for component tests | |
| 101 | +| `IScenario` | Custom scenario interface | |
| 102 | + |
| 103 | +### Threshold Presets |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { ThresholdPresets } from '@react-native-windows/perf-testing'; |
| 107 | + |
| 108 | +// Available presets |
| 109 | +ThresholdPresets.core // 10% regression threshold |
| 110 | +ThresholdPresets.list // 15% for list components |
| 111 | +ThresholdPresets.interactive // 20% for animated components |
| 112 | +ThresholdPresets.community // 25% relaxed threshold for external use |
| 113 | +``` |
| 114 | + |
| 115 | +## Extending for Custom Components |
| 116 | + |
| 117 | +Community developers can create perf tests for their custom native components: |
| 118 | + |
| 119 | +```typescript |
| 120 | +import { ComponentPerfTestBase } from '@react-native-windows/perf-testing'; |
| 121 | +import { MyCustomSlider } from 'my-custom-component'; |
| 122 | + |
| 123 | +class MyCustomSliderPerfTest extends ComponentPerfTestBase { |
| 124 | + readonly componentName = 'MyCustomSlider'; |
| 125 | + readonly category = 'custom' as const; |
| 126 | + readonly testId = 'perf-test-slider'; |
| 127 | + |
| 128 | + createComponent(props?: Record<string, unknown>) { |
| 129 | + return <MyCustomSlider testID={this.testId} {...props} />; |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Configuration |
| 135 | + |
| 136 | +### Jest Setup |
| 137 | + |
| 138 | +Add to your Jest setup file: |
| 139 | + |
| 140 | +```typescript |
| 141 | +import '@react-native-windows/perf-testing/matchers'; |
| 142 | +``` |
| 143 | + |
| 144 | +### Custom Thresholds |
| 145 | + |
| 146 | +```typescript |
| 147 | +expect(perf).toMatchPerfSnapshot({ |
| 148 | + maxDurationIncrease: 15, // Allow 15% regression |
| 149 | + maxRenderCount: 5, |
| 150 | +}); |
| 151 | +``` |
| 152 | + |
| 153 | +## License |
| 154 | + |
| 155 | +MIT |
0 commit comments