• 0

[C#/WCF] WCF is producing slower results then legacy asmx service?


Question

I switched to WCF b/c I've heard it's much faster then asmx. Also the WCF is configured to use MTOM while the asmx service is not. My WFC service call takes about 1 min 20 seconds to complete on a file download (streaming it in chunks) and my old asmx service takes 1 min 7 seconds to just return one massive response of embedded byte data. I'm downloading a 1.5 MB file 5 times with both calls.


static void Main(string[] args)
        {
            ServiceReference3.TestCloudWebSvcClient client = new ServiceReference3.TestCloudWebSvcClient();
            client.Open();

            Console.WriteLine("Getting file blocks...");


            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            for (int i = 0; i < 5; i++)
            {
                byte[] fileChunkData = null;
                int offset = 0;
                int totalBytes = 0;
                bool moreDataAvail = true;
                string filePath = string.Format(@"C:\Users\\Downloads\icuin44({0}).dll", i.ToString());
                if (File.Exists(filePath))
                    File.Delete(filePath);
                using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                {
                    while (moreDataAvail == true)
                    {
                        Console.WriteLine("More data avail is true for iteration " + i.ToString() + ", executing command...");
                        string errorMsg = client.DownloadLargeFile(128000, "", "icuin44.dll", 5, offset, "", "", out moreDataAvail, out fileChunkData);
                        Console.WriteLine("Command executed. Error Msg: {0} | Block length: {1}", errorMsg, (fileChunkData != null) ? fileChunkData.Length.ToString() : "null");
                        // Now that we have the new byte data (assuming the call worked), lets write it to the stream.
                        if (fileChunkData != null)
                        {
                            fs.Write(fileChunkData, 0, fileChunkData.Length);
                            offset += 128000;
                            totalBytes += fileChunkData.Length;
                        }
                        Console.WriteLine("Wrote {0} bytes for a total of {1} bytes to file stream...", fileChunkData.Length, totalBytes);
                    }
                    fs.Close();
                    fs.Dispose();
                }
            }
            stopWatch.Stop();
            Console.WriteLine("Time elapsed while running functions: {0}", stopWatch.Elapsed.ToString());
            stopWatch.Reset();

            client.Close();

            Console.WriteLine("Closed client successfully");

            Console.WriteLine("Attempting to download file via legacy service...");
            stopWatch.Start();
            ServiceReference4.TestWebSvcSoapClient client2 = new ServiceReference4.TestCloudWebSvcSoapClient();
            ServiceReference4.DownloadFilesResponse res = client2.GetDirectSvrFiles("", "", new ServiceReference4.FileSvcDef[] { new ServiceReference4.FileSvcDef() { DirPath = "", FileLastSyncDate = new DateTime(2012, 5, 9), FileName = "icuin45.dll" },
            new ServiceReference4.FileSvcDef() { DirPath = "", FileLastSyncDate = new DateTime(2012, 5, 9), FileName = "icuin45.dll" }, new ServiceReference4.FileSvcDef() { DirPath = "", FileLastSyncDate = new DateTime(2012, 5, 9), FileName = "icuin45.dll" },
            new ServiceReference4.FileSvcDef() { DirPath = "", FileLastSyncDate = new DateTime(2012, 5, 9), FileName = "icuin45.dll" }, new ServiceReference4.FileSvcDef() { DirPath = "", FileLastSyncDate = new DateTime(2012, 5, 9), FileName = "icuin45.dll" }});
            if(res.FileBinaryList != null)
            {
                Console.WriteLine("Downloaded file successfully, writing to hard drive...");

                for (int i2 = 0; i2 < 5; i2++)
                {
                    string filePath2 = string.Format(@"C:\Users\xn\Downloads\icuin44_legacy({0}).dll", i2.ToString());
                    if (File.Exists(filePath2))
                        File.Delete(filePath2);
                    using (FileStream fs = new FileStream(filePath2, FileMode.Append, FileAccess.Write))
                    {
                        fs.Write(res.FileBinaryList[i2].FileData, 0, res.FileBinaryList[i2].FileData.Length);
                        fs.Close();
                        fs.Dispose();
                    }
                    Console.WriteLine("Wrote file successfully to the hard drive.");
                }
            }
            stopWatch.Stop();
            Console.WriteLine("time elapsed running legacy functions: {0}", stopWatch.Elapsed.ToString());
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}

1 answer to this question

Recommended Posts

  • 0

The reason is that chunking requires multiple server calls to download the full contents of the file e.g. in this example you're using 128k chunks which means for a 5 meg file would require about 40 web requests each one with the overhead of establishing the connection, seeking to the correct position of the file etc. Whereas with the "legacy" service you're making a single call which is open for the duration of the download, meaning your connection setup, seeking etc only happens once.

The speed difference has nothing to do with using WCF, it's the conversion to using MTOM which is causing the performance to be slightly worse.

MOTM naturally is slower than a simple download (for the reasons explained above) but obviously you've got the advantage of being able to resume should a chunk fail to download correctly. If you increase your chunk size you'll see the performance increase also as there are fewer calls to the web service to retrieve and hence less overhead. You need to find a good balance for chunk size since obviously bigger chunks mean more to re-download if a chunk fails to be downloaded.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.