Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 7d42b66

Browse files
committed
Add benchmark
1 parent 8cb1d71 commit 7d42b66

File tree

1 file changed

+141
-6
lines changed

1 file changed

+141
-6
lines changed

test/benchmark.js

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ const describe = RNTest.config({
1616
group : '0.8.0',
1717
run : true,
1818
expand : true,
19-
timeout : 10000,
19+
timeout : 999999999,
2020
})
2121
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
2222
const dirs = RNFetchBlob.fs.dirs
2323

24-
describe('upload BASE64 v.s. Storage', (report, done) => {
24+
false && describe('upload BASE64 v.s. Storage', (report, done) => {
2525

2626
let b64data = null
27-
let storageFile = dirs.DocumentDir + '/benchmark-1mb'
27+
let storageFile = dirs.DocumentDir + '/benchmark-cache'
2828
let b64res, storageRes
2929
let iteration = 50
30+
let target = `${TEST_SERVER_URL}/public/1mb-dummy`
3031

3132
RNFetchBlob
3233
.config({ path : storageFile })
33-
.fetch('get', `${TEST_SERVER_URL}/public/1mb-dummy`)
34+
.fetch('get', target)
3435
.then((res) => res.readFile('base64'))
3536
.then((data) => {
3637
b64data = data
@@ -48,8 +49,13 @@ describe('upload BASE64 v.s. Storage', (report, done) => {
4849
let count = 0
4950
for(let i=0; i< iteration; i++) {
5051
p = p.then(() => {
51-
if(++count <iteration)
52+
if(++count <iteration){
53+
report(
54+
<Info key="benchmark progress" uid="report">
55+
<Text style={{textAlign:'center'}}>BASE64 {count}/{iteration}</Text>
56+
</Info>)
5257
return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, b64data)
58+
}
5359
else {
5460
b64res = Date.now() - begin
5561
storageTest()
@@ -65,8 +71,13 @@ describe('upload BASE64 v.s. Storage', (report, done) => {
6571
let count = 0
6672
for(let i=0; i< iteration; i++) {
6773
p = p.then(() => {
68-
if(++count < iteration)
74+
if(++count < iteration){
75+
report(
76+
<Info key="benchmark progress" uid="report">
77+
<Text style={{textAlign:'center'}}>Storage {count}/{iteration}</Text>
78+
</Info>)
6979
return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, RNFetchBlob.wrap(storageFile))
80+
}
7081
else {
7182
storageRes = Date.now() - begin
7283
summary()
@@ -87,3 +98,127 @@ describe('upload BASE64 v.s. Storage', (report, done) => {
8798
}
8899

89100
})
101+
102+
103+
false && describe('write file BASE64 v.s. URI', (report, done) => {
104+
let iteration = 200
105+
let target = `${TEST_SERVER_URL}/public/6mb-dummy`
106+
let sourceURI = dirs.DocumentDir + '/benchmark2-source'
107+
let sourceBASE64 = null
108+
let b64Res = 0
109+
let uriRes = 0
110+
RNFetchBlob.fetch('GET', target)
111+
.then((res) => {
112+
sourceBASE64 = res.base64()
113+
return fs.writeFile(sourceURI, res.base64(), 'base64')
114+
})
115+
.then(() => {
116+
let p = Promise.resolve()
117+
let begin = Date.now()
118+
let count = 0
119+
for(let i=0; i< iteration; i++) {
120+
p = p.then(() => {
121+
if(++count < iteration){
122+
report(
123+
<Info key="benchmark progress" uid="report2">
124+
<Text style={{textAlign:'center'}}>BASE64 {count}/{iteration}</Text>
125+
</Info>)
126+
return fs.writeFile(dirs.DocumentDir +'/benchmark2-target', sourceBASE64, 'base64')
127+
}
128+
else {
129+
b64Res = Date.now() - begin
130+
uriTest()
131+
}
132+
})
133+
}
134+
})
135+
136+
function uriTest() {
137+
let p = Promise.resolve()
138+
let begin = Date.now()
139+
let count = 0
140+
for(let i=0; i< iteration; i++) {
141+
p = p.then(() => {
142+
if(++count < iteration){
143+
report(
144+
<Info key="benchmark progress" uid="report2">
145+
<Text style={{textAlign:'center'}}>URI {count}/{iteration}</Text>
146+
</Info>)
147+
return fs.writeFile(dirs.DocumentDir +'/benchmark2-target', sourceURI, 'uri')
148+
}
149+
else {
150+
uriRes = Date.now() - begin
151+
summary()
152+
}
153+
})
154+
}
155+
}
156+
157+
function summary() {
158+
report(
159+
<Info key="BASE64 - writeFile">
160+
<Text>{`BASE64 ${b64Res/iteration} ms/req`}</Text>
161+
</Info>,
162+
<Info key="URI - writeFile">
163+
<Text>{`URI ${uriRes/iteration} ms/req`}</Text>
164+
</Info>)
165+
done()
166+
}
167+
168+
})
169+
170+
171+
describe('read file benchmark', (report, done) => {
172+
173+
let iteration = 200
174+
let target = `${TEST_SERVER_URL}/public/6mb-dummy`
175+
let source = dirs.DocumentDir + '/benchmark3-source'
176+
let res = {}
177+
RNFetchBlob.fetch('GET', target)
178+
.then((res) => {
179+
return fs.writeFile(source, res.base64(), 'base64')
180+
})
181+
.then(() => {
182+
test('base64', () => {
183+
test('ascii', () => {
184+
test('utf8', summary)
185+
})
186+
})
187+
})
188+
189+
function test(encode, cb) {
190+
let p = Promise.resolve()
191+
let begin = Date.now()
192+
let count = 0
193+
for(let i=0; i< iteration; i++) {
194+
p = p.then(() => {
195+
if(++count < iteration){
196+
report(
197+
<Info key="benchmark progress" uid="report3">
198+
<Text style={{textAlign:'center'}}>{encode} {count}/{iteration}</Text>
199+
</Info>)
200+
return fs.readFile(source, encode)
201+
}
202+
else {
203+
res[encode] = Date.now() - begin
204+
cb()
205+
}
206+
})
207+
}
208+
}
209+
210+
function summary() {
211+
report(
212+
<Info key="BASE64 - readFile">
213+
<Text>{`BASE64 ${res['base64']/iteration} ms/req`}</Text>
214+
</Info>,
215+
<Info key="ASCII - readFile">
216+
<Text>{`ASCII ${res['ascii']/iteration} ms/req`}</Text>
217+
</Info>,
218+
<Info key="UTF8 - readFile">
219+
<Text>{`UTF8 ${res['utf8']/iteration} ms/req`}</Text>
220+
</Info>)
221+
done()
222+
}
223+
224+
})

0 commit comments

Comments
 (0)