Middleware for a client 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;
}
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.