forked from ospfranco/expo-sqlite-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
198 lines (179 loc) · 4.6 KB
/
App.js
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
import { useState } from "react";
import { StatusBar } from "expo-status-bar";
import {
StyleSheet,
Text,
View,
Button,
ActivityIndicator,
} from "react-native";
import * as SQLite from "expo-sqlite";
import Chance from "chance";
import performance from "react-native-performance";
const chance = new Chance();
const db = SQLite.openDatabase("db.testDb");
async function addData(j) {
return new Promise((resolve, reject) => {
db.transaction(
(tx) => {
for (let i = j * 50000; i < j * 50000 + 50000; i++) {
tx.executeSql(
'INSERT INTO "Test" (id, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
i,
chance.name(),
chance.name(),
chance.name(),
chance.name(),
chance.name(),
chance.integer(),
chance.integer(),
chance.integer(),
chance.integer(),
chance.integer(),
chance.floating(),
chance.floating(),
chance.floating(),
chance.floating(),
]
);
}
},
(error) => {
reject(error);
},
() => {
console.warn("db created");
resolve();
}
);
});
}
async function createDB() {
return new Promise((resolve, reject) => {
db.transaction(
(tx) => {
tx.executeSql("DROP TABLE IF EXISTS Test;");
tx.executeSql(
"CREATE TABLE Test ( id INT PRIMARY KEY, v1 TEXT, v2 TEXT, v3 TEXT, v4 TEXT, v5 TEXT, v6 INT, v7 INT, v8 INT, v9 INT, v10 INT, v11 REAL, v12 REAL, v13 REAL, v14 REAL)"
);
},
(error) => {
reject(error);
},
() => {
console.warn("db created");
resolve();
}
);
});
}
async function query() {
return new Promise((resolve, reject) => {
db.exec(
[{ sql: "SELECT * FROM Test", args: [] }],
false,
(error, resultSet) => {
if (error) {
reject(error);
} else {
resolve(resultSet);
}
}
);
});
}
async function queryDB() {
let times = { load: [], access: [] };
for (let i = 0; i < 10; i++) {
performance.mark("queryStart");
const result = await query();
let measurement = performance.measure("queryEnd", "queryStart");
console.warn(`query ${i} done`);
times.load.push(measurement.duration);
console.warn(result[0].rows.length);
performance.mark("accessStart");
for (let j = 0; j < result[0].rows.length; j++) {
const v14 = result[0].rows[j].v14;
}
let accessMeasurement = performance.measure("accessEnd", "accessStart");
times.access.push(accessMeasurement.duration);
}
return times;
}
export default function App() {
const [isLoading, setIsLoading] = useState(false);
const [times, setTimes] = useState({ load: [], access: [] });
const createDb = async () => {
try {
setIsLoading(true);
await createDB();
console.warn("created db");
for (let i = 0; i < 6; i++) {
await addData(i);
console.warn(`created ${i}`);
}
} catch (e) {
console.warn(e);
} finally {
setIsLoading(false);
}
};
const queryDb = async () => {
try {
setIsLoading(true);
const times = await queryDB();
setTimes(times);
} catch (e) {
console.warn("error querying", e);
} finally {
setIsLoading(false);
}
};
return (
<View style={styles.container}>
<Text style={styles.header}>Expo SQLite</Text>
<Button title="Create DB" onPress={createDb} />
<Button title="Query DB" onPress={queryDb} />
{isLoading && <ActivityIndicator color="white" />}
{!!times.load.length && (
<Text style={styles.big}>
Load{" "}
{(
times.load.reduce((acc, t) => (acc += t), 0) / times.load.length
).toFixed(0)}{" "}
ms average
</Text>
)}
{!!times.access.length && (
<Text style={styles.big}>
Access{" "}
{(
times.access.reduce((acc, t) => (acc += t), 0) / times.access.length
).toFixed(0)}{" "}
ms average
</Text>
)}
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#222",
alignItems: "center",
justifyContent: "center",
},
header: {
color: "white",
fontSize: 28,
},
normal: {
color: "white",
},
big: {
color: "white",
fontSize: 28,
},
});