Firebase存储的上传/下载速度非常慢 [英] Very Slow upload/download speed of Firebase Storage

查看:70
本文介绍了Firebase存储的上传/下载速度非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事

我在我的应用程序中使用Firebase存储将大文件上传到Firebase存储中.文件主要是视频,有时甚至会大于2 GB.

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 GB some times.

我做了什么

这就是我所做的.

UploadTask originalUpload = originalDestination.putFile(Uri.fromFile(originalSource));
            mCurrentUploadTask = originalUpload;
            originalUpload.addOnProgressListener(mOnProgressUpdateListener);
originalUpload.addOnSuccessListener(mOriginalSuccessListener);

请注意,我还需要将这些同步任务转换为异步,因为我需要使用

Just to inform, I am also converting these sync tasks to async as I need to process everything in the background using,

Tasks.await(originalUpload);

问题

这个问题很奇怪而且出乎意料.上传/下载可以完美运行,但是速度很慢.

The problem is weird and unexpected. The upload/download works perfectly, but it is very slow.

我的互联网连接速度为1 MBps,但是这些文件从未以这种速度传输.它大约为100-150 KBps,几乎是我网络可用速度的15%.

I am on a good internet connection of 1 MBps but these files are never transferred at this speed. It is around 100-150 KBps which is almost 15% of the available speed of my network.

在提出此声明之前,我已经在多个不同的网络上进行了多次测试.我无处发现Firebase会充分利用可用带宽,而其他非Firebase的下载/上传则可以全速运行.

I have tested this several times and on several different networks before making this claim. Nowhere, I found Firebase making full use of the available bandwidth, whereas other non-Firebase downloads/uploads works at full speed.

我的应用需要下载和上传大文件,因此无法承受如此低的传输速度. Firebase根本没有想到这一点.

My app needs to download and upload large files, and therefore cannot afford to transfer at such slow speeds. This was not at all expected from Firebase.

如果我在执行过程中做任何错误的事情还是Firebase固有的问题,请告诉我?

Please let me know if I am doing anything wrong in my implementation or if it is an inherent problem with Firebase?

更新

我也遇到了只有上传内容的问题.每当我下载某些内容并关闭Wifi时,都会由于该错误而取消下载.

I have also been experiencing an issue with only the uploads. When ever I am downloading something and I just turn off the Wifi, the download gets cancelled with this error.

com.google.firebase.storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response.

Read error: ssl=0xb7e7a510: I/O error during system call, Connection timed out
                                                                           javax.net.ssl.SSLException: Read error: ssl=0xb7e7a510: I/O error during system call, Connection timed out
                                                                               at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:699)
                                                                               at com.android.okio.Okio$2.read(Okio.java:113)
                                                                               at com.android.okio.RealBufferedSource.read(RealBufferedSource.java:48)
                                                                               at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:446)
                                                                               at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
                                                                               at java.io.InputStream.read(InputStream.java:162)
                                                                               at com.google.firebase.storage.FileDownloadTask.run(Unknown Source)
                                                                               at com.google.firebase.storage.StorageTask$5.run(Unknown Source)
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                               at java.lang.Thread.run(Thread.java:818)

但是当我对上传执行完全相同的操作时,它只是等待连接,如果我再次打开Wifi,它将重新连接并恢复.为什么问题仅在下载文件时发生? 再次遇到getFile()API问题吗?

But when I do the exact same thing for uploads, it just waits for connection and if I turn on the Wifi again, it reconnects and resumes. Why does the problem only happen with downloading files? Is it a problem with getFile() API again?

仅供参考,我尚未更改上传和下载的超时设置.它们是默认设置.

FYI, I have not changed the timeout settings for both uploads and downloads. They are at their defaults.

推荐答案

我一直在进行一些测试,并且试图弄清为什么您看到不良的性能. 使用getStream一次读取单个字节时,我已经能够复制不良性能,但是可以通过对合理大小的字节缓冲区(256kb缓冲区)进行读取来解决此问题. 修复此问题后,我看到的性能等同于直接通过httpsUrlConnection下载.

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.

我想到了三种可能性:

1)您在dns解析期间(到我们的多个ip)看到超时,并且在上载期间连接池不起作用.但这只能解释为什么上传(分段)的速度会很慢.下载没有分块.

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)您位于我们尚未优化的区域.我们即将在亚洲和欧洲范围内提供本地支持,以优化性能.

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)也许您正在将其与httpsurlconnection以外的其他内容进行比较?您可以提供更多测试详细信息吗?

3) Maybe you are comparing it with something other than httpsurlconnection? Can you give more details of your 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<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
            @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();
      }
   } 

这篇关于Firebase存储的上传/下载速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆