Slow response for returning a file using a FileResult controller?

786 Views Asked by At

I'm attempting to build a speed test app for our customers connecting to our e-labs. I want to test their download speed in Mbps.

The logic I came up with is; upon click event, record the startTime, make an ajax call to a FileResult controller to return a 2.67 mb jpg file back to the client. Upon 'success', record the endTime, subtract the two time stamps, then call a different controller to finish off some logic and record the results to the db where I then return the view to show the results.

I'm hosting on an Azure Db server in region where I live. My results are 1 Mbps, which seems slow compared to speedtest.net where I receive 15 mbps selecting a server in the same region.

I'm wondering if this approach is botched? I'm still working through the basics so the try catch's etc aren't implemented.

Script in my Page:

<script>
        $(document).ready(function () {

        $("#downloadFile").click(function () {
            var start = Date.now();
            var end = null;
            var totalSeconds = 0.00;
            $.ajax({

                url: "/Home/DownloadTest",

                success: function (data) {

                    end = Date.now();
                    //alert(start + " " + end);
                    totalSeconds = (end - start) / 1000;
                    window.location.href = "/Home/DownloadResults?totalSeconds="+totalSeconds;
                }
            });

        });

    });

</script>

FileResult Controller

//Download File
    public FileResult DownloadTest()
    {
        string directoryPath = Server.MapPath("~/TestFile/2point67mb.jpg");
        string fileName = "DownloadTest.jpg";

        return File(directoryPath, "image/jpeg", fileName);
    }

View Controller

 //Download Results
    public ActionResult DownloadResults(string totalSeconds)
    {
        double totalSecs = Convert.ToDouble(totalSeconds);

        SpeedTest Test = new SpeedTest();

        Services.IPAddress ip = new Services.IPAddress();
        var clientIP = ip.GetIPAddress();
        string[] IPAddresses = clientIP.Split(':');
        Test.Address = IPAddresses[0];

        double fileSize = 2.67; //Size of File in MB.
        double speed = 0.00;

        speed = Math.Round(fileSize / totalSecs);
        Test.ResponseTime = string.Format("{0} Mbps", speed);
        Test.Status = "Success";
        Test.UserId = User.Identity.GetUserId();
        Test.TestDate = DateTime.Now;
        db.SpeedTest.Add(Test);
        db.SaveChanges();

        return View(Test);
    }
0

There are 0 best solutions below