Slow response to file return using FileResult controller?

I am trying to create a speed test application for our customers connecting to our electronic labs. I want to check the download speed in Mbps.

The logic that I came across is; after the click event, write startTime, make an ajax call to the FileResult controller to return the 2.67 mb jpg file back to the client. After "success" write down endTime, subtract two timestamps, then call another controller to finish some logic and write the results in db, where I will then return the view to show the results.

I host on an Azure Db server in the region where I live. My results are 1 Mb / s, which seems slow compared to speedtest.net, where I get 15 Mb / s, choosing a server in the same region.

I am wondering if this approach is unsuccessful? I am still working on the basics, so try catch, etc. Not implemented.

Script on 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);
    }
+4
source share

Source: https://habr.com/ru/post/1629315/


All Articles