【发布时间】:2014-12-20 07:54:07
【问题描述】:
我创建了一个简单的“Hello World”服务,使用 OWIN 和 Nancy 在 Azure Worker 角色中运行。在本地运行时,我的响应时间约为 1 毫秒。一旦我部署它,大约需要 250~400 毫秒。它在单个 Standard_A1 实例上运行。我不认为这是 Nancy 的问题,因为我在使用 WebApi 而不是 Nancy 时得到了类似的响应时间。我知道这不是我访问它的网络,因为监视器也显示了类似的响应时间。
这是我的发布资料:
我的 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!";
}
}
对可能导致它的原因有什么想法,或者我什至可以如何尝试找出原因?
【问题讨论】:
-
呃。这可能与它有关吗?
await Task.Delay(1000); -
@RichTurner 这实际上使响应时间延长了 3 倍......该部分实际上是从 VS 模板生成的。
-
抱歉,我明白了,如果你去掉延迟,响应时间会延长 3 倍?如果您消除延迟,应该花费更少的时间。
-
其实我不这么认为。 RunAsync 实际上只是一个美化的 while 循环,以保持角色连续运行。没有它,我将看到的只是正在写入的 Traces 流。如果我在 Run 方法中使用 while(true) 语句,我会得到相同的效果。这就是我以前在示例中看到的方式。
-
是的 - 请仔细阅读;)您在美国西海岸的地理位置是哪里?