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

Android Performance: readStream takes too long #321

Open
iyawnis opened this issue Apr 10, 2017 · 8 comments
Open

Android Performance: readStream takes too long #321

iyawnis opened this issue Apr 10, 2017 · 8 comments

Comments

@iyawnis
Copy link

iyawnis commented Apr 10, 2017

Library version: 0.10.4
RN Version: 0.41.1

I have a JSON file, inside my assets directory in the android version.
The file is 8MB. I am reading the file using the following function:

const path = RNFetchBlob.fs.asset('app_data.json');
return RNFetchBlob.fs.readStream(path, 'utf8')
 .then((stream) => {
     let data = ''
     stream.open()
     stream.onData((chunk) => {
         data += chunk
     })
     stream.onEnd(() => {
       dispatch({ type: APP_DATA.SUCCESS, payload: JSON.parse(data) });
     })
 })

The above function takes about 15 seconds to execute on Android, and about 2 seconds on iOS.
Is there any obvious reason why this is happening ? Any optimisations I can make ? Should I increase the buffer, or make changes to the way the file is stored?

The file is loaded once when the application launches, there are no animations or anything interactive while this process takes place, just a loading screen with a gif image

readFile is also taking about the same amount of time.


I have tried tinkering with interval / buffer size, but the loading time does not appear to be affected.. Also the JSON.parse does not appear to add any additional overhead.


By changing the buffer size / interval, the loading time either stays the same, or can get worse, but no improvements.


iOS goes through the same loading screen in about 0.5 sec..

The delay is between promise.resolve(data) being called from Android, and the data reaching my code.

@iyawnis iyawnis changed the title Android Performance: readStream Android Performance: readStream takes too long Apr 12, 2017
@wkh237
Copy link
Owner

wkh237 commented Apr 15, 2017

@latusaki , thanks for the feedback, will look into this issue. Besides, have you tried an interval like - 1 second and 1MB buffer size ?

@iyawnis
Copy link
Author

iyawnis commented Apr 15, 2017 via email

@iyawnis
Copy link
Author

iyawnis commented Apr 15, 2017

return RNFetchBlob.fs.readStream(path, 'utf8', 1024000, 1000)
still takes 15 - 16 sec.

wkh237 added a commit that referenced this issue Apr 16, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
… Android #321
@wkh237
Copy link
Owner

wkh237 commented Apr 16, 2017

@latusaki , after some investigation I noticed that the fs.readStream is somehow broken, have done some fix and also tested with a ~ 9MB text file with the following code

RNFetchBlob.config({ path : dirs.DCIMDir + '/readStreamtest'})
  .fetch('GET', `${TEST_SERVER_URL}/public/9mb-5987598452-dummy`)
  .then((res) => {
    return fs.readStream(res.path(), 'utf8', 1024000, 250)
  })
  .then((stream) => {
    let begin = Date.now()
    let data = ''
    stream.open()
    stream.onData((chunk) => {
      console.log('read chunk', chunk.length)
      data += chunk
    })
    stream.onEnd(() => {
      console.log('size of 9mb dummy', data.length)
      console.log(Date.now() - begin, 'ms elapsed')
      done()
    })
  })

The process can be done in less than 4 seconds, the test device I'm using is Sony Experia XA (Android 6)

Not sure if it's helpful? Perhaps you can upgrade the package from branch issue-321 and let's see if it's getting better 👍

@iyawnis
Copy link
Author

iyawnis commented Apr 16, 2017

Thank you for looking into this. I tried with the issue-321 branch, and changed the buffer/interval to the above specifications, but still same performance. Although checking at the committed code on the branch, only the error handling has changed, is that correct ?

@wkh237
Copy link
Owner

wkh237 commented Apr 16, 2017

@latusaki , what's the device and OS version you're using? Have you tried using release mode ?

@iyawnis
Copy link
Author

iyawnis commented Apr 16, 2017

using Moto X 2014, android 6, release mode (./gradlew assembleRelease, installed app-release.apk) :/
As mentioned above though, I think this is related to the bridge performance. When using readFile, the file is read on Android side in about half second. It takes about 14 seconds, between promise.resolve, and .then() in my promise being called. Can't think why you are getting 4s performance on stream though.

wkh237 added a commit that referenced this issue May 5, 2017

Verified

This commit was signed with the committer’s verified signature.
jesse-c Jesse Claven
… Android #321
@d3v2a
Copy link

d3v2a commented Oct 5, 2017

i have the problem too but it not take long time to read file but for check if file exist (~ 1s)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants