linux/mono上的HTTP性能



我的问题
由于有一些代码支持这个问题,我会提前问它。在linux/mono上运行的Servicestack自主机服务(或者任何http侦听器(是否存在已知的性能问题?

我的实际用例是一个web服务调用多个其他(非公共(web服务。当在windows下运行时,我注意到性能快得惊人,但当在linux/mono下运行时——它似乎会变慢,请求的长度可能需要15秒(相比之下,在windows下则需要0.2秒(。

我的后续问题是——我在这里做错了什么(如果有的话(?

我的环境
我正在运行一台Windows 10 PC-i7-6700@3.4ghz 4核-(超线程-因此有8个逻辑核(,32GB ram,并有一个使用hyper V的Linux虚拟机(Ubuntu 16.04(。它有2个核(i7-6500@3.4ghz-分配了4GB内存(。基本上,下面代码中的任何内容都不应该对下面服务定义的底层硬件过度征税。我也尝试过在其他虚拟机上托管这个,以确保它不是我的本地硬件,但无论我在哪里尝试,我似乎都能得到一致的结果
我使用mono:latest映像和xbuild我的C#解决方案来创建一个docker映像,并将其托管在linux机器上
此外,我对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编译并在mono下运行的,我会看到大多数请求的响应时间在0.8秒到0.05秒之间,但我确实看到(几乎每次尝试(有些请求需要15秒才能得到服务。这在Mono/linux上比在.NET/Windows上慢得多!

顺便说一句,如果我将并行循环从100增加到500,我发现windows服务可以毫不费力地处理所有请求,但客户端应用程序(我的测试程序(将失败,并出现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服务在运行,同样的行为在该服务中也很明显。帮助

v4.5.2更新

ServiceStack在其第4.5.2版中增加了对.NET Core的支持,现在这是在Linux上运行ServiceStack的推荐和支持选项。


在linux/mono上运行的Servicestack自主机服务(或者任何http侦听器(是否存在已知的性能问题?

Mono的HTTP堆栈对于繁重的工作负载来说是缓慢且不稳定的,对于较小的工作负载也可以,但我们不建议将其用于生产工作负载。我们已经记录了使用HyperFastCI+nginx在Mono上运行的最可靠的设置,例如:

  • https://github.com/ServiceStackApps/mono-server-config
  • https://github.com/ServiceStackApps/mono-docker-config

在Linux上托管ServiceStack和.NET的未来是.NET Core,它快速、稳定且受良好支持。ServiceStack对.NET Core的支持将在本周晚些时候发布的下一个v4.5.2发行说明中公布。

最新更新