-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathBenchmarkScreen.tsx
More file actions
199 lines (174 loc) · 4.9 KB
/
BenchmarkScreen.tsx
File metadata and controls
199 lines (174 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-disable react-native/no-inline-styles */
import React, { useCallback, useMemo, useState } from 'react'
import Chance from 'chance'
import {
ActivityIndicator,
ScrollView,
Text,
TouchableOpacity,
View,
} from 'react-native'
import { StatusBar } from 'expo-status-bar'
import { ScreenStyles } from '../styles'
import { resetLargeDb, largeDb, testDb, resetTestDb } from '../tests/db'
const chance = new Chance()
const ids = Array(100000)
.fill(0)
.map(() => chance.integer())
const stringValue = chance.name()
const integerValue = chance.integer()
const doubleValue = chance.floating()
interface Benchmark {
description: string
numberOfRuns: number
prepare?: () => void
run: (i: number) => void
}
const NUMBER_OF_INSERTS = 10000
const benchmarks: Benchmark[] = [
{
description: `Insert ${NUMBER_OF_INSERTS} rows`,
numberOfRuns: NUMBER_OF_INSERTS,
prepare: () => {
resetTestDb()
testDb?.execute('DROP TABLE IF EXISTS User;')
testDb?.execute(
'CREATE TABLE User ( id REAL PRIMARY KEY, name TEXT NOT NULL, age REAL, networth REAL) STRICT;',
)
},
run: (i) => {
testDb?.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[ids[i]!, stringValue, integerValue, doubleValue],
)
},
},
{
description: `SQLite: 1000 INSERTs`,
numberOfRuns: 1000,
prepare: () => {
resetTestDb()
testDb?.execute('CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));')
},
run: (i) => {
testDb?.execute('INSERT INTO t1 (a, b, c) VALUES(?, ?, ?)', [
ids[i]!,
integerValue,
stringValue,
])
},
},
{
description: 'Load 300k record DB',
numberOfRuns: 1,
prepare: () => {
resetLargeDb()
},
run: () => {
largeDb?.execute('SELECT * FROM Test;')
},
},
]
const wait = (ms: number) =>
new Promise<void>((resolve) => setTimeout(resolve, ms))
function runBenchmark(benchmark: Benchmark) {
return wait(1000).then(() => {
console.log(benchmark.description)
benchmark.prepare?.()
const start = performance.now()
for (let i = 0; i < benchmark.numberOfRuns; i++) benchmark.run(i)
const end = performance.now()
const time = (end - start).toFixed(2)
console.log(`Took ${time}ms to run!`)
return time
})
}
export function BenchmarkScreen() {
const [results, setResults] = useState<Record<string, string | null>>({})
const [isLoading, setIsLoading] = useState(false)
const startBenchmarks = useCallback(async () => {
console.log('START BENCHMARKS')
setResults({})
setIsLoading(true)
console.log('--------- BEGINNING NitroSQLite BENCHMARKS ---------')
async function start(i = 0): Promise<void> {
const benchmark = benchmarks[i]!
setResults((prev) => ({ ...prev, [benchmark.description]: null }))
const time = await runBenchmark(benchmark)
setResults((prev) => ({ ...prev, [benchmark.description]: time }))
if (benchmarks[i + 1] != null) return start(i + 1)
}
await start()
console.log('--------- FINISHED NitroSQLite BENCHMARKS! ---------')
setIsLoading(false)
}, [])
const Results = useMemo(
() =>
benchmarks.map(({ description }, index) => {
const time = results[description]
return (
<View
style={{ paddingBottom: 10 }}
key={index}
>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginRight: -20,
}}
>
<Text
style={{
fontWeight: 'bold',
textAlign: 'center',
paddingRight: 5,
}}
>
{description}
</Text>
{time === null ? (
<ActivityIndicator />
) : (
<View style={{ width: 20, height: 20 }} />
)}
</View>
{time != null && (
<Text style={{ textAlign: 'center' }}>
Took <Text style={{ fontWeight: 'bold' }}>{time}ms</Text>
</Text>
)}
</View>
)
}),
[results],
)
return (
<ScrollView contentContainerStyle={ScreenStyles.container}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginRight: -20,
marginBottom: 20,
}}
>
<TouchableOpacity
onPressIn={() => {
startBenchmarks()
}}
style={{ paddingRight: 10 }}
>
<Text style={ScreenStyles.buttonText}>Run benchmarks</Text>
</TouchableOpacity>
{isLoading ? (
<ActivityIndicator />
) : (
<View style={{ width: 20, height: 20 }} />
)}
</View>
{Results}
<StatusBar style="auto" />
</ScrollView>
)
}