import { LevelDB } from "react-native-leveldb" ; import { Text } from "react-native" ; import * as React from "react" ; import AsyncStorage from "@react-native-async-storage/async-storage" ; import { compareReadWrite , getRandomString , getTestSetArrayBuffer , getTestSetString } from "./test-util" ; export interface BenchmarkResults { writeMany : { numKeys : number ; durationMs : number ; } ; readMany : { numKeys : number ; durationMs : number ; } ; } export function benchmarkLeveldb ( ) { let name = getRandomString ( 32 ) + '.db' ; console . info ( 'Opening DB' , name ) ; const db = new LevelDB ( name , true , true ) ; let res = { } ; const writeKvs = getTestSetArrayBuffer ( 10000 ) ; let started = new Date ( ) . getTime ( ) ; for ( const [ k , v ] of writeKvs ) { db . put ( k , v ) ; } res . writeMany = { numKeys : writeKvs . length , durationMs : new Date ( ) . getTime ( ) - started } ; const readKvs = [ ] ; started = new Date ( ) . getTime ( ) ; let it ; for ( it = db . newIterator ( ) . seekToFirst ( ) ; it . valid ( ) ; it . next ( ) ) { readKvs . push ( [ it . keyBuf ( ) , it . valueBuf ( ) ] ) ; } it . close ( ) ; res . readMany = { numKeys : readKvs . length , durationMs : new Date ( ) . getTime ( ) - started } ; db . close ( ) ; compareReadWrite ( writeKvs , readKvs ) ; return res as BenchmarkResults ; } export async function benchmarkAsyncStorage ( ) { console . info ( 'Clearing AsyncStorage' ) ; try { await AsyncStorage . clear ( ) ; } catch ( e ) { if ( ! e ?. message ?. includes ( 'Failed to delete storage directory' ) ) { throw e ; } } let res = { } ; const writeKvs = getTestSetString ( 10000 ) ; let started = new Date ( ) . getTime ( ) ; await AsyncStorage . multiSet ( writeKvs ) ; res . writeMany = { numKeys : writeKvs . length , durationMs : new Date ( ) . getTime ( ) - started } ; started = new Date ( ) . getTime ( ) ; const readKvs = await AsyncStorage . multiGet ( await AsyncStorage . getAllKeys ( ) ) as [ string , string ] [ ] ; res . readMany = { numKeys : readKvs . length , durationMs : new Date ( ) . getTime ( ) - started } ; compareReadWrite ( writeKvs , readKvs ) ; return res as BenchmarkResults ; } export const BenchmarkResultsView = ( x ) => { const { writeMany , readMany , title } = x ; const writeManyRes = writeMany && `wrote ${ writeMany . numKeys } items in ${ writeMany . durationMs }ms; ` + `(${ ( writeMany . numKeys / writeMany . durationMs ) . toFixed ( 1 ) }items/ms)` ; const readManyRes = readMany && `read ${ readMany . numKeys } items in ${ readMany . durationMs }ms; ` + `(${ ( readMany . numKeys / readMany . durationMs ) . toFixed ( 1 ) }items/ms)` ; return ( < > < Text > == { title } < /Text> < Text > Benchmark write many : { writeManyRes } < /Text> < Text > Benchmark read many : { readManyRes } < /Text> < />); } ;