Browse Source

Add benchmark test

Ben Hsieh 8 years ago
parent
commit
e77ed45de6
1 changed files with 89 additions and 0 deletions
  1. 89
    0
      test/benchmark.js

+ 89
- 0
test/benchmark.js View File

@@ -0,0 +1,89 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  Text,
7
+  View,
8
+  Platform,
9
+  Dimensions,
10
+  Image,
11
+} from 'react-native';
12
+
13
+const fs = RNFetchBlob.fs
14
+const { Assert, Comparer, Info, prop } = RNTest
15
+const describe = RNTest.config({
16
+  group : '0.8.0',
17
+  run : true,
18
+  expand : true,
19
+  timeout : 10000,
20
+})
21
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
22
+const  dirs = RNFetchBlob.fs.dirs
23
+
24
+describe('upload BASE64 v.s. Storage', (report, done) => {
25
+
26
+  let b64data = null
27
+  let storageFile = dirs.DocumentDir + '/benchmark-1mb'
28
+  let b64res, storageRes
29
+  let iteration = 50
30
+
31
+  RNFetchBlob
32
+    .config({ path : storageFile })
33
+    .fetch('get', `${TEST_SERVER_URL}/public/1mb-dummy`)
34
+    .then((res) => res.readFile('base64'))
35
+    .then((data) => {
36
+      b64data = data
37
+      report(
38
+        <Info key="test data should correct">
39
+          <Text>size of b64data = {data.length}</Text>
40
+        </Info>)
41
+      b64Test()
42
+    })
43
+
44
+    // base64 upload benchmark
45
+    function b64Test() {
46
+      let p = Promise.resolve()
47
+      let begin = Date.now()
48
+      let count = 0
49
+      for(let i=0; i< iteration; i++) {
50
+        p = p.then(() => {
51
+          if(++count <iteration)
52
+            return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, b64data)
53
+          else {
54
+            b64res = Date.now() - begin
55
+            storageTest()
56
+          }
57
+        })
58
+      }
59
+    }
60
+
61
+    // storage upload benchmark
62
+    function storageTest() {
63
+      let p = Promise.resolve()
64
+      let begin = Date.now()
65
+      let count = 0
66
+      for(let i=0; i< iteration; i++) {
67
+        p = p.then(() => {
68
+          if(++count < iteration)
69
+            return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, RNFetchBlob.wrap(storageFile))
70
+          else {
71
+            storageRes = Date.now() - begin
72
+            summary()
73
+          }
74
+        })
75
+      }
76
+    }
77
+
78
+    function summary() {
79
+      report(
80
+        <Info key="BASE64">
81
+          <Text>{`BASE64 ${b64res/iteration} ms/req`}</Text>
82
+        </Info>,
83
+        <Info key="Storage">
84
+          <Text>{`Storage ${storageRes/iteration} ms/req`}</Text>
85
+        </Info>)
86
+      done()
87
+    }
88
+
89
+})