Aucune description

benchmark.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. Text,
  6. View,
  7. Platform,
  8. Dimensions,
  9. Image,
  10. } from 'react-native';
  11. const fs = RNFetchBlob.fs
  12. const { Assert, Comparer, Info, prop } = RNTest
  13. const describe = RNTest.config({
  14. group : '0.8.0',
  15. run : true,
  16. expand : true,
  17. timeout : 999999999,
  18. })
  19. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  20. const dirs = RNFetchBlob.fs.dirs
  21. false && describe('upload BASE64 v.s. Storage', (report, done) => {
  22. let b64data = null
  23. let storageFile = dirs.DocumentDir + '/benchmark-cache'
  24. let b64res, storageRes
  25. let iteration = 50
  26. let target = `${TEST_SERVER_URL}/public/1mb-dummy`
  27. RNFetchBlob
  28. .config({ path : storageFile })
  29. .fetch('GET', target)
  30. .then((res) => res.readFile('base64'))
  31. .then((data) => {
  32. b64data = data
  33. report(
  34. <Info key="test data should correct">
  35. <Text>size of b64data = {data.length}</Text>
  36. </Info>)
  37. b64Test()
  38. })
  39. // base64 upload benchmark
  40. function b64Test() {
  41. let p = Promise.resolve()
  42. let begin = Date.now()
  43. let count = 0
  44. for(let i=0; i< iteration; i++) {
  45. p = p.then(() => {
  46. if(++count <iteration){
  47. report(
  48. <Info key="benchmark progress" uid="report">
  49. <Text style={{textAlign:'center'}}>BASE64 {count}/{iteration}</Text>
  50. </Info>)
  51. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, b64data)
  52. }
  53. else {
  54. b64res = Date.now() - begin
  55. storageTest()
  56. }
  57. })
  58. }
  59. }
  60. // storage upload benchmark
  61. function storageTest() {
  62. let p = Promise.resolve()
  63. let begin = Date.now()
  64. let count = 0
  65. for(let i=0; i< iteration; i++) {
  66. p = p.then(() => {
  67. if(++count < iteration){
  68. report(
  69. <Info key="benchmark progress" uid="report">
  70. <Text style={{textAlign:'center'}}>Storage {count}/{iteration}</Text>
  71. </Info>)
  72. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {}, RNFetchBlob.wrap(storageFile))
  73. }
  74. else {
  75. storageRes = Date.now() - begin
  76. summary()
  77. }
  78. })
  79. }
  80. }
  81. function summary() {
  82. report(
  83. <Info key="BASE64">
  84. <Text>{`BASE64 ${b64res/iteration} ms/req`}</Text>
  85. </Info>,
  86. <Info key="Storage">
  87. <Text>{`Storage ${storageRes/iteration} ms/req`}</Text>
  88. </Info>)
  89. done()
  90. }
  91. })
  92. describe('write file BASE64 v.s. URI', (report, done) => {
  93. let iteration = 200
  94. let target = `${TEST_SERVER_URL}/public/12k-dummy`
  95. let sourceURI = dirs.DocumentDir + '/benchmark2-source'
  96. let writeTarget = dirs.DocumentDir +'/benchmark2-target'
  97. let sourceBASE64 = null
  98. let b64Res = 0
  99. let uriRes = 0
  100. RNFetchBlob.fetch('GET', target)
  101. .then((res) => {
  102. sourceBASE64 = res.base64()
  103. return fs.writeFile(sourceURI, res.base64(), 'base64')
  104. })
  105. .then(() => {
  106. let p = Promise.resolve()
  107. let begin = Date.now()
  108. let count = 0
  109. for(let i=0; i< iteration; i++) {
  110. p = p.then(() => {
  111. if(++count < iteration){
  112. report(
  113. <Info key="benchmark progress" uid="report2">
  114. <Text style={{textAlign:'center'}}>BASE64 {count}/{iteration}</Text>
  115. </Info>)
  116. return fs.readFile(sourceURI, 'base64')
  117. .then((data) => fs.writeFile(writeTarget, data))
  118. }
  119. else {
  120. b64Res = Date.now() - begin
  121. uriTest()
  122. }
  123. })
  124. }
  125. })
  126. function uriTest() {
  127. let p = Promise.resolve()
  128. let begin = Date.now()
  129. let count = 0
  130. for(let i=0; i< iteration; i++) {
  131. p = p.then(() => {
  132. if(++count < iteration){
  133. report(
  134. <Info key="benchmark progress" uid="report2">
  135. <Text style={{textAlign:'center'}}>URI {count}/{iteration}</Text>
  136. </Info>)
  137. return fs.writeFile(writeTarget, sourceURI, 'uri')
  138. }
  139. else {
  140. uriRes = Date.now() - begin
  141. summary()
  142. }
  143. })
  144. }
  145. }
  146. function summary() {
  147. report(
  148. <Info key="BASE64 - writeFile">
  149. <Text>{`BASE64 ${b64Res/iteration} ms/req`}</Text>
  150. </Info>,
  151. <Info key="URI - writeFile">
  152. <Text>{`URI ${uriRes/iteration} ms/req`}</Text>
  153. </Info>)
  154. done()
  155. }
  156. })
  157. false && describe('read file benchmark', (report, done) => {
  158. let iteration = 50
  159. let target = `${TEST_SERVER_URL}/public/1mb-dummy`
  160. let source = dirs.DocumentDir + '/benchmark3-source'
  161. let res = {}
  162. RNFetchBlob.fetch('GET', target)
  163. .then((res) => {
  164. return fs.writeFile(source, res.base64(), 'base64')
  165. })
  166. .then(() => {
  167. test('base64', () => {
  168. test('ascii', () => {
  169. test('utf8', summary)
  170. })
  171. })
  172. })
  173. function test(encode, cb) {
  174. let p = Promise.resolve()
  175. let begin = Date.now()
  176. let count = 0
  177. for(let i=0; i< iteration; i++) {
  178. p = p.then(() => {
  179. if(++count < iteration){
  180. report(
  181. <Info key="benchmark progress" uid="report3">
  182. <Text style={{textAlign:'center'}}>{encode} {count}/{iteration}</Text>
  183. </Info>)
  184. return fs.readFile(source, encode)
  185. }
  186. else {
  187. res[encode] = Date.now() - begin
  188. cb()
  189. }
  190. })
  191. }
  192. }
  193. function summary() {
  194. report(
  195. <Info key="BASE64 - readFile">
  196. <Text>{`BASE64 ${res['base64']/iteration} ms/req`}</Text>
  197. </Info>,
  198. <Info key="ASCII - readFile">
  199. <Text>{`ASCII ${res['ascii']/iteration} ms/req`}</Text>
  200. </Info>,
  201. <Info key="UTF8 - readFile">
  202. <Text>{`UTF8 ${res['utf8']/iteration} ms/req`}</Text>
  203. </Info>)
  204. done()
  205. }
  206. })