11/* eslint-disable no-undef */
22import * as React from 'react' ;
3- import { ScrollView , StyleSheet , View , Text , Button } from 'react-native' ;
3+ import {
4+ ScrollView ,
5+ StyleSheet ,
6+ View ,
7+ Text ,
8+ Button ,
9+ ActivityIndicator
10+ } from 'react-native' ;
411import { mean } from 'ramda' ;
512import { match } from 'react-states' ;
613import useBenchmark from './useBenchmark' ;
714
815function Benchmarks ( { benchmarks } ) {
916 return (
10- < View >
11- < Text >
12- Average Time to Render:{ '\n' }
13- { benchmarks . map ( ( e ) => `${ e . name } : ${ mean ( e . values ) . toFixed ( 2 ) } ms\n` ) }
17+ < View style = { styles . resultsContainer } >
18+ < Text style = { styles . resultsTitle } > Average Time to Render:</ Text >
19+ { benchmarks . map ( ( e ) => (
20+ < Text key = { e . name } style = { styles . resultItem } >
21+ { e . name } : { mean ( e . values ) . toFixed ( 2 ) } ms
22+ </ Text >
23+ ) ) }
24+ </ View >
25+ ) ;
26+ }
27+
28+ function ProgressIndicator ( {
29+ runId,
30+ runs,
31+ profileId,
32+ numOfProfiles,
33+ profileName
34+ } ) {
35+ const overallProgress = (
36+ ( ( profileId * runs + runId + 1 ) / ( numOfProfiles * runs ) ) *
37+ 100
38+ ) . toFixed ( 0 ) ;
39+
40+ return (
41+ < View style = { styles . progressContainer } >
42+ < ActivityIndicator size = "large" color = "#007AFF" />
43+ < Text style = { styles . progressTitle } > Benchmarking in Progress...</ Text >
44+ < Text style = { styles . progressText } >
45+ Profile: { profileName } ({ profileId + 1 } /{ numOfProfiles } )
46+ </ Text >
47+ < Text style = { styles . progressText } >
48+ Run: { runId + 1 } /{ runs }
1449 </ Text >
50+ < Text style = { styles . progressPercentage } > { overallProgress } % Complete</ Text >
1551 </ View >
1652 ) ;
1753}
1854
1955export default function Benchmark ( { samples, html, ignoredTags } ) {
20- const { onLayout, launch, ...state } = useBenchmark ( {
56+ const {
57+ onLayout,
58+ launch,
59+ profile : currentProfile ,
60+ ...state
61+ } = useBenchmark ( {
2162 runs : samples
2263 } ) ;
2364 const renderHtml = React . useCallback (
24- ( { runId, profile } ) => (
25- < View key = { runId } onLayout = { onLayout } >
26- < profile . component
27- ignoredTags = { ignoredTags }
28- running = { true }
29- html = { html }
30- { ...profile . props }
31- />
32- </ View >
33- ) ,
34- [ html , ignoredTags , onLayout ]
65+ ( { runId } ) => {
66+ if ( ! currentProfile ) return null ;
67+ return (
68+ < View key = { runId } onLayout = { onLayout } >
69+ < currentProfile . component
70+ ignoredTags = { ignoredTags }
71+ running = { true }
72+ html = { html }
73+ { ...currentProfile . props }
74+ />
75+ </ View >
76+ ) ;
77+ } ,
78+ [ html , ignoredTags , onLayout , currentProfile ]
3579 ) ;
80+
81+ const isRunning = state . state === 'RUNNING' || state . state === 'WAIT_RUN' ;
82+
3683 return (
37- < View >
38- < Button
39- style = { { marginTop : 40 } }
40- title = "Run Benchmark"
41- onPress = { launch }
42- disabled = { state . state !== 'WAIT_BENCH' }
43- />
44- < ScrollView contentContainerStyle = { styles . container } >
84+ < View style = { styles . wrapper } >
85+ < View style = { styles . buttonContainer } >
86+ < Button
87+ title = "Run Benchmark"
88+ onPress = { launch }
89+ disabled = { state . state !== 'WAIT_BENCH' }
90+ />
91+ </ View >
92+
93+ { isRunning && currentProfile && (
94+ < ProgressIndicator
95+ runId = { state . runId }
96+ runs = { state . runs }
97+ profileId = { state . profileId }
98+ numOfProfiles = { state . numOfProfiles }
99+ profileName = { currentProfile . name }
100+ />
101+ ) }
102+
103+ < ScrollView contentContainerStyle = { styles . scrollContainer } >
45104 { match ( state , {
46105 WAIT_BENCH : ( { benchmarks } ) =>
47106 benchmarks ? (
48107 < Benchmarks benchmarks = { benchmarks } />
49108 ) : (
50- < Text > Waiting for benchmark to launch</ Text >
109+ < Text style = { styles . waitingText } >
110+ Waiting for benchmark to launch
111+ </ Text >
51112 ) ,
52113 WAIT_RUN : renderHtml ,
53114 RUNNING : renderHtml
@@ -59,11 +120,65 @@ export default function Benchmark({ samples, html, ignoredTags }) {
59120
60121const styles = StyleSheet . create ( {
61122 wrapper : {
62- flexGrow : 1 ,
63- padding : 16
123+ flex : 1
64124 } ,
65- container : {
125+ buttonContainer : {
126+ marginBottom : 16 ,
127+ paddingHorizontal : 16
128+ } ,
129+ scrollContainer : {
66130 flexGrow : 1 ,
67- padding : 16
131+ paddingHorizontal : 16
132+ } ,
133+ progressContainer : {
134+ backgroundColor : '#E3F2FD' ,
135+ padding : 20 ,
136+ marginHorizontal : 16 ,
137+ marginBottom : 16 ,
138+ borderRadius : 12 ,
139+ alignItems : 'center' ,
140+ borderWidth : 1 ,
141+ borderColor : '#90CAF9'
142+ } ,
143+ progressTitle : {
144+ fontSize : 18 ,
145+ fontWeight : 'bold' ,
146+ color : '#1976D2' ,
147+ marginTop : 12 ,
148+ marginBottom : 8
149+ } ,
150+ progressText : {
151+ fontSize : 14 ,
152+ color : '#1565C0' ,
153+ marginTop : 4
154+ } ,
155+ progressPercentage : {
156+ fontSize : 20 ,
157+ fontWeight : 'bold' ,
158+ color : '#0D47A1' ,
159+ marginTop : 12
160+ } ,
161+ resultsContainer : {
162+ backgroundColor : '#f5f5f5' ,
163+ padding : 16 ,
164+ borderRadius : 8 ,
165+ marginBottom : 16
166+ } ,
167+ resultsTitle : {
168+ fontSize : 18 ,
169+ fontWeight : 'bold' ,
170+ marginBottom : 12 ,
171+ color : '#333'
172+ } ,
173+ resultItem : {
174+ fontSize : 16 ,
175+ marginBottom : 8 ,
176+ color : '#555'
177+ } ,
178+ waitingText : {
179+ fontSize : 16 ,
180+ color : '#888' ,
181+ textAlign : 'center' ,
182+ marginTop : 20
68183 }
69184} ) ;
0 commit comments