Bug 17351 - Really bad network performance of System.Net
Summary: Really bad network performance of System.Net
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.10.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-01-21 13:10 UTC by juanjo.a
Modified: 2017-06-27 17:20 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case for Visual Studio (16.11 KB, application/x-zip-compressed)
2014-02-05 11:01 UTC, juanjo.a
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description juanjo.a 2014-01-21 13:10:10 UTC
I have found that the network performance using Xamarin System.Net classes is really bad. It hardly can reach download speeds higher than 2.2 MB/s in emulator or real devices. I tried sync and async calls with the same poor speed.

For the same download on the same network and same device I can reach speeds higher than 6 MB/s (as much as my connection can) using Java.Net

Here is a test code to check the bad download speed of System.Net compared with Java.Net:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        byte[] m_ReceiveBuffer = new byte[4096];

        System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(m_DotNetDownload));
        //System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(m_JavaDownload));

        th.Start();

    }

    private void m_JavaDownload()
    {
        Java.Net.URL url = new Java.Net.URL("https://dl.google.com/android/adt/adt-bundle-windows-x86_64-20131030.zip");
        URLConnection connection = url.OpenConnection();
        InputStream input = new BufferedInputStream(url.OpenStream());
        int count;
        long totalReaded = 0;
        m_StartTime = DateTime.UtcNow;
        while ((count = input.Read(m_ReceiveBuffer)) != -1)
        {
            totalReaded += count;
            DateTime endTime = DateTime.UtcNow;
            TimeSpan timed = endTime - m_StartTime;
            System.Console.WriteLine("Avg speed: " + (totalReaded / 1024) / timed.TotalSeconds);
        }
    }

    private void m_DotNetDownload()
    {
        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://dl.google.com/android/adt/adt-bundle-windows-x86_64-20131030.zip");
        httpRequest.Method = "GET";
        httpRequest.KeepAlive = false;
        httpRequest.ReadWriteTimeout = 160 * 1000;

        httpRequest.Timeout = 160 * 1000;
        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        Stream m_ResponseStream = httpResponse.GetResponseStream();
        int readed = 0;
        long totalReaded = 0;
        m_StartTime = DateTime.UtcNow;
        do
        {
            readed = m_ResponseStream.Read(m_ReceiveBuffer, 0, m_ReceiveBuffer.Length);
            totalReaded += readed;
            DateTime endTime = DateTime.UtcNow;
            TimeSpan timed = endTime - m_StartTime;
            System.Console.WriteLine("Avg speed: " + (totalReaded / 1024) / timed.TotalSeconds);
        }
        while (readed > 0);
        httpResponse.Close();
        m_ResponseStream.Close();
        m_ResponseStream = null;
    }
Comment 1 Sadik Ali 2014-01-29 07:29:51 UTC
I have implemented above code at our end in Xamarin-android project. Application build successfully but when I debug application getting exception for m_DotNetDownload() function.

Error log: https://gist.github.com/saurabh360/30d5bc9f4aff48043fbd 

Refer screen shot: http://screencast.com/t/vJEDRNpybqt

Let me know what I am missing anything.

Checked With: 

Windows
XS 4.2.3(build 51)
XA 4.13.0
Comment 2 Jonathan Pryor 2014-01-29 09:41:01 UTC
@Sadik: That appears to be a Xamarin Studio bug. Please try in a different/new project, or try with Visual Studio. Comment #1 has ~nothing to do with the reported bug.

@Sadik: It would also be useful/"interesting" to "port" the provided code to a Console app (i.e. remove the Xamarin.Android dependency) and see what the reported performance is on Mono "proper".
Comment 3 Sadik Ali 2014-01-30 09:33:20 UTC
I have added above code in "Console Project" on both VS and Xamarin Studio but getting error for "Java.Net" assembly, I have added some assemblies but unable to resolve issue, added assembly are mentioned in screen cast.

Refer screen cast: http://screencast.com/t/cpWE8jIyJq

Please suggest me what assembly I need to add for "System.Net"?

For the "Java.Net" assembly should I need to create binding java library with the help of below tutorials:  http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/binding_a_java_library_%28.jar%29/
Comment 4 Jonathan Pryor 2014-01-30 11:07:38 UTC
@Sadik: You can't use Java.Net on desktop mono, so don't worry about the m_JavaDownload() method.

The System.Net namespace is in System.dll, which should be referenced by default.
Comment 5 Sadik Ali 2014-01-31 00:57:51 UTC
As per my observation so far in this bug, below are the case that I tried and end results for the same:

Android Application: Using java.net code -- It is working fine.
Android Application: Using System.net code -- It is throwing exception. I have reported a bug Bug 17528 for the same.

Console Application: System.Net code -- It is working fine.
Console Application: Java.Net code -- It gives library issue "Java.Net" assembly not found, to which after referring to Jonathan's comment #4 it seems to me that we can ignore this case here.

So finally, I am blocked to verify this issue because of bug:
Comment 6 juanjo.a 2014-02-05 11:01:21 UTC
Created attachment 5980 [details]
Test case for Visual Studio

This is a Visual Studio project you can run to reproduce the bug
Comment 7 Chris Hardy [MSFT] 2017-06-27 17:20:45 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Android. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.