【发布时间】:2017-02-26 21:45:27
【问题描述】:
我的问题
由于有一些代码可以支持这个问题 - 我会提前询问。在 linux/mono 上运行的 Servicestack 自托管服务(或任何 http 侦听器)是否存在任何已知的性能问题?
我的实际用例是一个 Web 服务调用多个其他(非公共)Web 服务。在 windows 下运行时,我注意到性能非常快,但在 linux/mono 下运行时 - 它似乎变慢了,请求的长度可能需要长达 15 秒(相比之下,在 windows 下运行时需要 0.2 秒)。
我的后续问题是 - 我在这里做错了什么(如果有的话)?
.
我的环境
我正在运行 Windows 10 PC - i7-6700 @ 3.4ghz 4 核 -(超线程 - 所以 8 个逻辑核心),32GB 内存,并且有一个使用 hyper V 的 Linux VM(Ubuntu 16.04)。它有 2 个核心(i7-6500 @3.4ghz - 4GB Ram 分配给它)。基本上 - 下面的代码中的任何内容都不应该对下面的服务定义底层的硬件造成过多的负担。我也尝试过在其他虚拟机上托管它,以确保它不是我的本地硬件 - 但无论我尝试什么,我似乎都能得到一致的结果。
我使用 mono:latest 映像和 xbuild 我的 C# 解决方案来创建我在 linux 机器上托管的 docker 映像。
另外 - 我对 Linux 世界还很陌生 - 不太确定如何在这个平台上进行故障排除(还没有!)
其中一项服务的示例
程序类:适用于windows / linux:
class Program
{
static void Main(string[] args)
{
var listeningOn = args.Length == 0 ? "http://*:32700/api/user/" : args[0];
var appHost = new AppHost()
.Init()
.Start(listeningOn);
Console.WriteLine("AppHost Created at {0}, listening on {1}",
DateTime.Now, listeningOn);
// check if we're running on mono
if (Type.GetType("Mono.Runtime") != null)
{
// on mono, processes will usually run as daemons - this allows you to listen
// for termination signals (ctrl+c, shutdown, etc) and finalize correctly
UnixSignal.WaitAny(new[]
{
new UnixSignal(Signum.SIGINT),
new UnixSignal(Signum.SIGTERM),
new UnixSignal(Signum.SIGQUIT),
new UnixSignal(Signum.SIGHUP)
});
}
else
{
Console.ReadLine();
}
}
}
应用主机:
public class AppHost : AppSelfHostBase
{
public AppHost() : base("Test User Service", typeof(AppHost).Assembly)
{
Plugins.Add(new PostmanFeature());
Plugins.Add(new CorsFeature());
}
public override void Configure(Container container)
{
}
}
合同:
[Api("Get User"), Route("/getUserByUserIdentifier/{Tenant}/{Identifier}", "GET")]
public class GetUserByUserIdentifierRequest : IReturn<GetUserByUserIdentifierResponse>
{
public string Tenant { get; set; }
public string Identifier { get; set; }
}
public class GetUserByUserIdentifierResponse
{
public UserDto User { get; set; }
}
public class UserDto
{
public string UserName { get; set; }
public string UserIdentifier { get; set; }
}
我用来测试应用程序的单独控制台:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");
ConcurrentBag<long> timings = new ConcurrentBag<long>();
Parallel.For(0, 100, new ParallelOptions { MaxDegreeOfParallelism = 8 }, x =>
{
Console.WriteLine("Attempt #{0}", x);
using (JsonServiceClient client = new JsonServiceClient("http://127.0.0.1:32700/api/user/")) //Specify Linux box IP here!
{
Stopwatch sw = new Stopwatch();
Console.WriteLine("Stopwatch started...");
sw.Start();
GetUserByUserIdentifierResponse response = client.Get(new GetUserByUserIdentifierRequest {Tenant = "F119A0DF-5002-4FF1-A0CE-8B60CFEE16A2", Identifier = "3216C49E-80C9-4249-9407-3E636E8C58AC"});
sw.Stop();
Console.WriteLine("Stopwatch stopped... got value [{0}] back in {1}ms", response.ToJson(), sw.ElapsedMilliseconds);
timings.Add(sw.ElapsedMilliseconds);
}
});
var allTimes = timings.ToList();
Console.WriteLine("Longest time taken = {0}ms", allTimes.Max());
Console.WriteLine("Shortest time taken = {0}ms", allTimes.Min());
Console.WriteLine("Avg time taken = {0}ms", allTimes.Average());
Console.WriteLine("Done!");
Console.ReadLine();
}
}
结果:
所以在我本地的 windows 盒子上,每个请求可能需要 0.1 秒到 0.02 秒的时间。经过多次尝试后,100 个请求的平均速度约为 0.1 秒。
如果我将测试应用程序指向 linux 框 - 指向在 docker 容器中使用 mono 编译和运行的相同代码 - 我看到大多数请求的响应时间在 0.8 秒到 0.05 秒之间,但我确实看到(几乎每次我尝试)某些请求需要 15 秒才能得到服务。这在 Mono/linux 上比在 .NET/Windows 上慢很多!
顺便说一句,如果我将并行循环从 100 增加到 500,我发现 Windows 服务可以毫不费力地处理所有请求,但是 client 应用程序(我的测试程序)将因 IO 错误而失败:
System.IO.IOException was unhandled by user code
HResult=-2146232800
Message=Unable to read data from the transport connection: The connection was closed.
Source=System
StackTrace:
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadToEnd()
at ServiceStack.Text.JsonSerializer.DeserializeFromStream[T](Stream stream)
at ServiceStack.Serialization.JsonDataContractSerializer.DeserializeFromStream[T](Stream stream)
at ServiceStack.JsonServiceClient.DeserializeFromStream[T](Stream stream)
at ServiceStack.ServiceClientBase.GetResponse[TResponse](WebResponse webResponse)
at ServiceStack.ServiceClientBase.Send[TResponse](String httpMethod, String relativeOrAbsoluteUrl, Object request)
at ServiceStack.ServiceClientBase.Get[TResponse](IReturn`1 requestDto)
at httppoke.Program.<>c__DisplayClass0_0.<Main>b__0(Int32 x) in <Redacted>\Program.cs:line 30
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
InnerException:
我觉得这个错误可能有助于表明正在发生的事情,但我真的不知道如何根据我所面临的问题对其进行交互。
同样值得注意的是,在测试程序运行时在 linux 机器上查看“top”或“docker stats”,CPU 使用率从未超过 4%。该服务并没有做任何费力的事情。
请注意 - 我正在尝试让多个服务相互通信 - 此处显示的服务是“测试用户”服务的一个非常精简的版本。我发现当服务调用其他服务(每个服务在它自己的 docker 容器中运行)时 - 服务之间进行通信所需的时间长得令人无法接受。
我在这里只向您展示所有一项服务的原因是,可以证明对服务的多次调用似乎明显减慢了速度。
我不确定这是由服务堆栈引起的问题,因为我还运行了一个自托管的 Nancyfx 服务,并且该服务也有同样的行为。 帮助!
【问题讨论】:
标签: c# mono servicestack nancy httplistener