缓慢的Azure工作者角色响应时间

时间:2014-12-19 21:29:24

标签: c# azure

我使用在Azure辅助角色中运行的OWIN和Nancy创建了一个简单的“Hello World”服务。在本地运行时,我的响应时间约为1毫秒。一旦我部署它,它需要大约250〜400毫秒。它在单个Standard_A1实例上运行。我不认为这是一个Nancy问题,因为我在使用WebApi而不是Nancy时得到类似的响应时间。我知道它不是我访问它的网络,因为监视器也显示类似的响应时间。

enter image description here

这是我的发布资料:

enter image description here

我的WorkerRole.cs类如下:

public class WorkerRole : RoleEntryPoint
{
    private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
    private IDisposable _app = null;

    public override void Run()
    {
        Trace.TraceInformation("StackOverflow.Example is running");

        try
        {
            this.RunAsync(this.cancellationTokenSource.Token).Wait();
        }
        finally
        {
            this.runCompleteEvent.Set();
        }
    }

    public override bool OnStart()
    {
        ServicePointManager.DefaultConnectionLimit = 64;

        var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["DefaultEndpoint"];
        string baseUri = String.Format("{0}://{1}", endpoint.Protocol, endpoint.IPEndpoint);

        Trace.TraceInformation(String.Format("Starting OWIN at {0}", baseUri), "Information");

        _app = WebApp.Start<Startup>(new StartOptions(url: baseUri));

        bool result = base.OnStart();

        Trace.TraceInformation("StackOverflow.Example has been started");

        return result;
    }

    public override void OnStop()
    {
        Trace.TraceInformation("StackOverflow.Example is stopping");

        this.cancellationTokenSource.Cancel();
        this.runCompleteEvent.WaitOne();

        if (_app != null)
        {
            _app.Dispose();
        }

        base.OnStop();

        Trace.TraceInformation("StackOverflow.Example has stopped");
    }

    private async Task RunAsync(CancellationToken cancellationToken)
    {
        // TODO: Replace the following with your own logic.
        while (!cancellationToken.IsCancellationRequested)
        {
            Trace.TraceInformation("Working");
            await Task.Delay(1000);
        }
    }
}

我的app.config是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="owin:HandleAllRequests" value="true"/>
  </appSettings>
</configuration>

这是控制器:

public class MonitorController : NancyModule
{
    public MonitorController()
        : base("/api")
    {
        Get["/monitor"] = x => "Hello world!";
    }
}

关于可能导致它的原因或我如何试图找出原因的任何想法?

1 个答案:

答案 0 :(得分:1)

通过VIP与* cloudapp.net网址访问端点修复此问题。