[Mono-list] Why is MONO network streaming much slower than .NET?

Miguel de Icaza miguel at novell.com
Mon Jun 18 12:30:40 EDT 2007


Hello,

    Do not allocate an array of 20 megabytes, it will slow down your
performance as the GC does not cope well with it.  

> Hi, 
> is MONO network streaming slow?
>  
> First I built a very simple client/server program 
> to get a data stream over network.
> In both programs I used a TcpClient,
> a NetworkStream and a byte array (8kB chunks)
> to stream 20 MB of data.
>  
> Then I tested the transfer rate of the client/server
> program between different Linux/MONO (1.2.3.1) 
> and different Windows/.NET (2.0) hosts
> in a 100 Mbit/s ethernet.
>  
> The strange result: If MONO was involved 
> the speed was dramatically reduced.
> Only a .NET/.NET transfer showed a realistic
> rate.  
>  
> .NET --> MONO : 31...42 Mbit/s   LOW
> MONO --> MONO : 43...45 Mbit/s   LOW
> MONO --> .NET : 51...76 Mbit/s   LOW
> .NET --> .NET : 94 Mbit/s        O.K.
>  
> What's wrong here?
> Any suggestions or hints?
> Thanks in advance.
>  
> Cheers
> Andreas
> 
> =======================================================
>  
> 
> Sourcecodes:
>  
> // ******
> // Client
> // ******
>  
> using System;
> using System.IO;
> using System.Net.Sockets;
>  
> namespace SimpleStreamClient
> {
>   class SimpleStreamClient
>   {
>     public static void Main(string[] args) {
>       TcpClient client;
>       NetworkStream netStream;
>       byte[] buf = new byte[8192];
>       int bytesToReceive = buf.Length;
>       int receivedBytes = 0;
>       int bytesReceived = 0;
>  
>       for(int index = 0; index < 10; index++) {
>         client = new TcpClient(args[0], 14866);
>         netStream = client.GetStream();
>         DateTime startTime = DateTime.Now;
>         bytesReceived = 0;
>         
>         while(true) {
>           bytesToReceive = buf.Length;
>           
>           while(bytesToReceive > 0) {
>             receivedBytes = netStream.Read(buf, 0, bytesToReceive);
>             if(receivedBytes == 0) {
>               break;
>             }
>             bytesReceived += receivedBytes;
>             bytesToReceive -= receivedBytes;
>           }
>           if(receivedBytes == 0) {
>             break;
>           }
>         }
>         double rate = bytesReceived / (((TimeSpan)(DateTime.Now -
> startTime)).TotalMilliseconds * 1000) * 8;
>         Console.WriteLine(bytesReceived + " Bytes received (" + rate +
> "MBit/s).");
>       }
>     Console.ReadKey();
>     }
>   }
> }
>  
> 
> // ******
> // Server
> // ******
>  
> using System;
> using System.IO;
> using System.Net;
> using System.Net.Sockets;
>  
> namespace SimpleStreamServer
> {
>   class SimpleStreamServer
>   {
>     public static void Main(string[] args) {
>       TcpClient client;
>       NetworkStream netStream;
>       byte[] bigBuf = new byte[20000000];
>       MemoryStream ms = new MemoryStream(bigBuf);
>       byte[] buf = new byte[8192];
>       string host =
> Dns.GetHostEntry(args[0]).AddressList[0].ToString();
>       IPAddress ipAddr = IPAddress.Parse(host);
>       TcpListener listener = new TcpListener(ipAddr, 14866);
>       listener.Start();
>       
>       while(true) {
>         Console.WriteLine("Waiting for client ...");
>         client = listener.AcceptTcpClient();        
>         Console.WriteLine("New client!");        
>         netStream = client.GetStream();
>         ms.Seek(0, SeekOrigin.Begin);
>         int readBytes = 0;
>         while((readBytes = ms.Read(buf, 0, buf.Length)) != 0) {
>           netStream.Write(buf, 0, readBytes);
>         }
>         netStream.Flush();
>         client.Close();
>       }
>     }
>   }
> }
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list


More information about the Mono-list mailing list