-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
224 lines (202 loc) · 5.26 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
Button,
AsyncStorage,
StyleSheet,
Text,
View,
} from 'react-native';
const RNFS = require('react-native-fs');
import { setItem, getItem } from './AsyncStorageArrayWrapper';
const path = RNFS.DocumentDirectoryPath + '/report.json';
const N = 35;
let running = false;
export default class App extends Component<{}> {
state = {
path: '',
progress: '',
};
render() {
return (
<View style={styles.container}>
<Button
title="Run single test"
onPress={() =>
runSingleTest(this.setState.bind(this)).then(path =>
this.setState({ path })
)
}
/>
<View style={{ height: 100 }} />
<Button
title="Run tests"
onPress={() =>
runTests(this.setState.bind(this)).then(path =>
this.setState({ path })
)
}
/>
<Text>{this.state.progress}</Text>
<Text>{this.state.path}</Text>
</View>
);
}
}
function delay(duration) {
return new Promise(resolve => {
setTimeout(() => resolve(), duration);
});
}
async function runSingleTest(setState) {
try {
const result = await measureRead(3100);
setState({ progress: JSON.stringify(result) });
} catch (e) {
console.error(e);
}
}
async function runTests(setState) {
if (running) {
return;
}
running = true;
setState({ progress: 'started' });
try {
const amounts = Array.apply(null, Array(N)).map((_, i) => i * 1000 + 100);
// run them sequentally
const writes = [];
for (const [index, amount] of amounts.entries()) {
const result = await measureWrites(amount);
writes.push(result);
console.log('write', result);
setState({
progress: `writes: ${Math.round(index * 100 / amounts.length)}%`,
});
}
const reads = [];
for (const [index, amount] of amounts.entries()) {
const result = await measureRead(amount);
console.log('read', result);
reads.push(result);
setState({
progress: `reads: ${Math.round(index * 100 / amounts.length)}%`,
});
}
const queries = [];
for (const [index, amount] of amounts.entries()) {
const result = await measureQueries(amount);
queries.push(result);
setState({
progress: `queries: ${Math.round(index * 100 / amounts.length)}%`,
});
}
const report = {
writes,
reads,
queries,
};
await RNFS.writeFile(path, JSON.stringify(report), 'utf8');
running = false;
return path;
} catch (e) {
console.error(e);
running = false;
return 'Error';
}
}
async function measureWrites(amount: number) {
const records = Array.apply(null, Array(amount)).map(createARecord);
const startTime = new Date();
const serialized = JSON.stringify(records);
await setItem('key', records);
const elapsed = new Date() - startTime;
const size = serialized.length / 1024 / 1024;
return {
amount,
size,
elapsed,
speed: size / elapsed,
};
}
async function measureRead(amount: number) {
const records = Array.apply(null, Array(amount)).map(createARecord);
const serialized = JSON.stringify(records);
const size = serialized.length / 1024 / 1024;
await setItem('key', records);
console.log('size=', size);
const startTime = new Date();
try {
const loadedRecords = await getItem('key');
const elapsed = new Date() - startTime;
return {
amount,
size,
elapsed,
speed: size / elapsed,
};
} catch (e) {
console.log(e);
return { amount, size, elapsed: 0, speed: 0 };
}
}
async function measureQueries(amount: number) {
await delay(100);
const records = Array.apply(null, Array(amount)).map(createARecord);
const serialized = JSON.stringify(records);
const startTime = new Date();
const queryResult = records
.sort((a, b) => b.randomDateField.getTime() - a.randomDateField.getTime())
.filter(
a =>
a.randomBooleanFlag1 &&
!a.randomBooleanFlag2 &&
a.randomTextField1[0] === '6'
);
console.log(queryResult.length);
const elapsed = new Date() - startTime;
const size = serialized.length / 1024 / 1024;
return {
amount,
size,
elapsed,
speed: size / elapsed,
};
}
function makeString(length: number) {
var text = '';
var possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function createARecord() {
return {
randomNumberKey: new Date().getTime(),
randomTextField1: makeString(100),
randomTextField2: makeString(200),
randomBooleanFlag1: Math.random() > 0.5,
randomBooleanFlag2: Math.random() > 0.5,
randomObjectField: {
randomKey: new Date().getTime(),
randomTextField1: makeString(100),
randomTextField2: makeString(200),
randomDateField: new Date(),
},
randomDateField: new Date(),
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});