Very Slow upload/download speed of Firebase Storage

前端 未结 2 1533
庸人自扰
庸人自扰 2021-02-14 01:51

The Story

I am using Firebase Storage in my app to upload large files into the Firebase Storage. Files are mostly videos which can be even larger than 2

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 02:53

    I've been doing some testing on my end and I'm trying to get to the bottom of why you see bad perf. I have been able to replicate bad performance when using getStream doing reads of single bytes at a time but this can be fixed by doing a read into a reasonably size byte buffer (256kb buffer). After fixing this, the performance I see is equal to downloading directly with httpsUrlConnection.

    Three possibilities pop into my mind:

    1) you are seeing timeouts during dns resolution (to our multiple ips) and connection pooling isnt working during uploads. But this would only explain why uploads (which are chunked) would be slow. Downloads are not chunked.

    2) you are in a region we have not yet optimized for. We are rolling out local support soon across asia and europe to optimized performance.

    3) Maybe you are comparing it with something other than httpsurlconnection? Can you give more details of your test?

    Here is my test:

            final long startTime = System.currentTimeMillis();
            // Test via Firebase
            mStorage.child(downloadPath).getStream(new StreamDownloadTask.StreamProcessor() {
                   @Override
                   public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot,
                          final InputStream inputStream) throws IOException {
                     readStream("FB", inputStream, startTime);
                   }
            });
    
            //Test with AsyncTask
            AsyncTask task = new AsyncTask() {
                @Override
                protected Void doInBackground(Void... voids) {
                    URL url = null;
                    try {
                        url = new URL(uri.toString());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    try {
                        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                        connection.connect();
                        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                            return null;
                        }
                        InputStream inputStream = connection.getInputStream();
                        readStream("ASYNC_TASK", inputStream, startTime);
                        inputStream.close();
    
                    }
                    catch(IOException e) {
    
                    }
    
                    return null;
                }
            };
    
            task.execute();
    
        private void readStream(String desc, InputStream inputStream, long startTime) {
          long bytesRead = 0;
          byte[] buffer = new byte[256*1024];
          int b = 1;
          try {
            while (b > 0) {
                b = inputStream.read(buffer);
                bytesRead += b;
                    Log.i("giug", "PROGRESS_" + desc + ": bytesperMS:" +
                            (bytesRead / (System.currentTimeMillis() - startTime)));
            }
            inputStream.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
       } 
    

提交回复
热议问题