3
\$\begingroup\$

I'm developing a middleware for my client and it's found that there are some delay when executing the process. We are currently investigating what's the exact causes of the issue, from architecture design to coding.

Here is part of the script we are calling, which is working, and I'm just wondering if it's optimized and properly written.

public string getResponse(System.Type type, Object input, string taskName, string method)
{
    string response = string.Empty;

    try
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
        MemoryStream mem = new MemoryStream();
        ser.WriteObject(mem, input);
        string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

        using (WebClient webClient = new WebClient())
        {
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;

            logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");

            response = webClient.UploadString(string.Format(Configuration.getTargetURI(), taskName), method, data);

            logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
            logger.Debug("Response: " + response);
        }
    }
    catch (Exception ex)
    {
        logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
        logger.Error(ex.Message + ex.StackTrace);

        throw ex;
    }

    return response;
}

public string getResponseNoInput(string taskName, string method)
{
    string response = string.Empty;

    try
    {
        WebRequest req = WebRequest.Create(String.Format(Configuration.getTargetURI(), taskName));
        req.Method = method;
        req.ContentType = "application/json; charset=utf-8";

        logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");

        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            response = sr.ReadToEnd();
        }

        logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
        logger.Debug("Response: " + response);
    }
    catch(Exception ex)
    {
        logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
        logger.Error(ex.Message + ex.StackTrace);

        throw ex;
    }

    return response;
}
\$\endgroup\$
3
  • \$\begingroup\$ Wrap your MemoryStream usage in a using to deterministically dispose of its resources when they are no longer needed. \$\endgroup\$ Jun 2, 2016 at 15:53
  • \$\begingroup\$ When looking for a performance problem, one should always start with running a profiler. \$\endgroup\$
    – RubberDuck
    Jun 2, 2016 at 16:18
  • \$\begingroup\$ hi @RubberDuck , how do I run a profiler with REST? tks \$\endgroup\$
    – Trowa
    Jun 3, 2016 at 3:07

2 Answers 2

1
\$\begingroup\$

Looking at your code I don't see any glaring performance issues. What you really need to do is measure the performance of any code that accesses an outside resource, such as a file or web service. That said, I would benchmark the execution time for the following items:

  • All calls to logger since I assume these hit the file system or database. Also be wary of logging to a shared drive, as this is really a network service call disguised as a direct call to the hard disk.

  • The call to the WebClient.UploadString method. If that service gets bogged down, your middleware will run slow as well.

  • The last item that might be slow is deserializing the input from JSON.

The more likely culprits are calls utilizing external resources, such as web services or the file system.

\$\endgroup\$
1
  • \$\begingroup\$ hi Greg, yup we did put the steps into a logger so that we can trace if any part of the scripts is causing error. It seems to us that a task need at least 3 seconds for completion, depends on what method we are calling, but my client seems a bit concern on this slowness issue. \$\endgroup\$
    – Trowa
    Jun 3, 2016 at 3:06
1
\$\begingroup\$

You have a monolith architecture, with synchronous method body. The client is put on hold while you use your web service. One way to solve this and problems you will start to face when scaling up would be to change your ROA to SOA.

What you can do, is create a job queue where you add this task asynchronously. When the task completes in the queue do a SignalR push to the client side (or html5 callback push).

\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.