11import "./App.css" ;
22import { IDBCache } from "@instructure/idb-cache" ;
3- import { useCallback , useEffect , useState } from "react" ;
3+ import { useCallback , useState } from "react" ;
44import { uuid , deterministicHash , generateTextOfSize } from "./utils" ;
55import { Button } from "@instructure/ui-buttons" ;
6- import { MetricGroup , Metric } from "@instructure/ui-metric" ;
6+ import { Metric } from "@instructure/ui-metric" ;
77import { View } from "@instructure/ui-view" ;
88import { Flex } from "@instructure/ui-flex" ;
99import { Heading } from "@instructure/ui-heading" ;
@@ -38,38 +38,45 @@ const initialItemSize =
3838 localStorage . getItem ( "itemSize" ) || String ( DEFAULT_ITEM_SIZE ) ,
3939 ) || DEFAULT_ITEM_SIZE ;
4040
41+ const BlankStat = ( ) => (
42+ < span
43+ style = { {
44+ color : "#ddd" ,
45+ } }
46+ >
47+ ------
48+ </ span >
49+ ) ;
50+
4151const App = ( ) => {
4252 const [ hash1 , setHash1 ] = useState < string | null > ( null ) ;
4353 const [ hash2 , setHash2 ] = useState < string | null > ( null ) ;
54+ const [ timeToGenerate , setTimeToGenerate ] = useState < number | null > ( null ) ;
4455 const [ setTime , setSetTime ] = useState < number | null > ( null ) ;
4556 const [ getTime , setGetTime ] = useState < number | null > ( null ) ;
4657 const [ itemSize , setItemSize ] = useState < number > ( initialItemSize ) ;
47- const [ isEncrypting , setIsEncrypting ] = useState < boolean > ( false ) ;
48- const [ isDecrypting , setIsDecrypting ] = useState < boolean > ( false ) ;
4958
5059 const encryptAndStore = useCallback ( async ( ) => {
51- console . time ( "generating content" ) ;
52- setIsEncrypting ( true ) ;
60+ const start1 = performance . now ( ) ;
5361 const paragraphs = Array . from ( { length : DEFAULT_NUM_ITEMS } , ( _ , index ) =>
5462 generateTextOfSize ( itemSize , `${ cacheBuster } -${ index } ` ) ,
5563 ) ;
56- console . timeEnd ( "generating content" ) ;
64+ const end1 = performance . now ( ) ;
65+ setTimeToGenerate ( end1 - start1 ) ;
5766
58- const start = performance . now ( ) ;
67+ const start2 = performance . now ( ) ;
5968
6069 for ( let i = 0 ; i < DEFAULT_NUM_ITEMS ; i ++ ) {
6170 await cache . setItem ( `item-${ i } ` , paragraphs [ i ] ) ;
6271 }
6372
64- const end = performance . now ( ) ;
65- setSetTime ( end - start ) ;
73+ const end2 = performance . now ( ) ;
74+ setSetTime ( end2 - start2 ) ;
6675
6776 setHash1 ( deterministicHash ( paragraphs . join ( "" ) ) ) ;
68- setIsEncrypting ( false ) ;
6977 } , [ itemSize ] ) ;
7078
7179 const retrieveAndDecrypt = useCallback ( async ( ) => {
72- setIsDecrypting ( true ) ;
7380 const results : Array < string | null > = [ ] ;
7481 const start = performance . now ( ) ;
7582
@@ -81,18 +88,6 @@ const App = () => {
8188 const end = performance . now ( ) ;
8289 setGetTime ( end - start ) ;
8390 setHash2 ( results . length > 0 ? deterministicHash ( results . join ( "" ) ) : null ) ;
84- setIsDecrypting ( false ) ;
85- } , [ ] ) ;
86-
87- // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
88- useEffect ( ( ) => {
89- requestAnimationFrame ( ( ) => {
90- encryptAndStore ( ) . then ( ( ) => {
91- requestAnimationFrame ( ( ) => {
92- retrieveAndDecrypt ( ) ;
93- } ) ;
94- } ) ;
95- } ) ;
9691 } , [ ] ) ;
9792
9893 return (
@@ -144,7 +139,7 @@ const App = () => {
144139 < legend className = "text-lg font-semibold text-gray-700" >
145140 Test Configuration
146141 </ legend >
147- < div className = "flex flex-col gap-4 ">
142+ < Flex direction = "column" gap = "small ">
148143 < div className = "flex items-center justify-between" >
149144 < span >
150145 Cache key: < code className = "text-sm" > { cacheKey } </ code >
@@ -171,14 +166,7 @@ const App = () => {
171166 Reset
172167 </ Button >
173168 </ div >
174- </ div >
175- </ fieldset >
176169
177- < fieldset className = "border border-gray-300 rounded-lg p-4 mb-6" >
178- < legend className = "text-lg font-semibold text-gray-700" >
179- Performance Test
180- </ legend >
181- < div className = "flex flex-col gap-4" >
182170 < Flex gap = "medium" >
183171 < Flex . Item shouldGrow >
184172 < NumberInput
@@ -219,7 +207,14 @@ const App = () => {
219207 />
220208 </ Flex . Item >
221209 </ Flex >
210+ </ Flex >
211+ </ fieldset >
222212
213+ < fieldset className = "border border-gray-300 rounded-lg p-4" >
214+ < legend className = "text-lg font-semibold text-gray-700" >
215+ Performance Tests
216+ </ legend >
217+ < div className = "flex flex-col gap-4" >
223218 < View
224219 as = "span"
225220 display = "inline-block"
@@ -230,23 +225,41 @@ const App = () => {
230225 >
231226 < Flex direction = "column" >
232227 < Button color = "primary" onClick = { encryptAndStore } >
233- Encrypt and store
228+ setItem
234229 </ Button >
235230 < View padding = "medium 0 0 0" >
236- < MetricGroup >
237- < Metric
238- renderLabel = "to encrypt and store"
239- renderValue = {
240- setTime !== null
241- ? `${ Math . round ( setTime ) } ms`
242- : "N/A"
243- }
244- />
245- < Metric
246- renderLabel = "hash of data"
247- renderValue = { hash1 }
248- />
249- </ MetricGroup >
231+ < Flex >
232+ < Flex . Item size = "33.3%" >
233+ < Metric
234+ renderLabel = "generate test data"
235+ renderValue = {
236+ setTime !== null ? (
237+ `${ Math . round ( timeToGenerate || 0 ) } ms`
238+ ) : (
239+ < BlankStat />
240+ )
241+ }
242+ />
243+ </ Flex . Item >
244+ < Flex . Item shouldGrow >
245+ < Metric
246+ renderLabel = "setItem"
247+ renderValue = {
248+ setTime !== null ? (
249+ `${ Math . round ( setTime ) } ms`
250+ ) : (
251+ < BlankStat />
252+ )
253+ }
254+ />
255+ </ Flex . Item >
256+ < Flex . Item size = "33.3%" >
257+ < Metric
258+ renderLabel = "hash"
259+ renderValue = { hash1 || < BlankStat /> }
260+ />
261+ </ Flex . Item >
262+ </ Flex >
250263 </ View >
251264 </ Flex >
252265 </ View >
@@ -261,24 +274,31 @@ const App = () => {
261274 >
262275 < Flex direction = "column" >
263276 < Button color = "primary" onClick = { retrieveAndDecrypt } >
264- Retrieve and decrypt
277+ getItem
265278 </ Button >
266279
267280 < View padding = "medium 0 0 0" >
268- < MetricGroup >
269- < Metric
270- renderLabel = "to retrieve and decrypt"
271- renderValue = {
272- getTime !== null
273- ? `${ Math . round ( getTime ) } ms`
274- : "error"
275- }
276- />
277- < Metric
278- renderLabel = "hash of data"
279- renderValue = { hash2 }
280- />
281- </ MetricGroup >
281+ < Flex >
282+ < Flex . Item size = "33.3%" > </ Flex . Item >
283+ < Flex . Item shouldGrow >
284+ < Metric
285+ renderLabel = "getItem"
286+ renderValue = {
287+ getTime !== null ? (
288+ `${ Math . round ( getTime ) } ms`
289+ ) : (
290+ < BlankStat />
291+ )
292+ }
293+ />
294+ </ Flex . Item >
295+ < Flex . Item size = "33.3%" >
296+ < Metric
297+ renderLabel = "hash"
298+ renderValue = { hash2 || < BlankStat /> }
299+ />
300+ </ Flex . Item >
301+ </ Flex >
282302 </ View >
283303 </ Flex >
284304 </ View >
0 commit comments