Asp.net web api .NET内核在LINUX上速度慢,在MAC/WINDOWS上速度快

Asp.net web api .NET内核在LINUX上速度慢,在MAC/WINDOWS上速度快,asp.net-web-api,.net-core,Asp.net Web Api,.net Core,我使用.net core 2.0 preview 2创建了一个简单的asp.net core webapi应用程序。 它几乎是由dotnetwepapi命令生成的样例项目。这个简单的应用程序所做的就是返回一个包含1.000.000个实例的列表。这100万个实例是修复的,只生成一次 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Micros

我使用.net core 2.0 preview 2创建了一个简单的asp.net core webapi应用程序。 它几乎是由dotnetwepapi命令生成的样例项目。这个简单的应用程序所做的就是返回一个包含1.000.000个实例的列表。这100万个实例是修复的,只生成一次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace print.Controllers
{
    [Route("api/[controller]")]
    public class PrintController : Controller
    {
        private PrintOutList list = null;

        // GET api/values
        [HttpGet]
        public IActionResult Get()
        {
            if (list == null)
            {
                list = new PrintOutList();
                list.Build(1000000);
            }
            return new OkObjectResult(list);
        }            
    }

    public enum Weekday
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3
    }

    public class PrintOut
    {
        public string Id { get; set; }
        public int ActorObjectTypeId { get;set;}
        public string ActorId { get; set;}
        public int PrintObjectTypeId { get ; set ;}
        public string PrintObjectId { get; set;}
        public string Text { get; set;}
        public Weekday Weekday { get; set; }
    }

    public class PrintOutList : List<PrintOut>
    {
        public void Build(int  count)
        {
            for(int index=0;index<count;index++)
            {
                PrintOut element = new PrintOut();

                if (index == 0) element.Id = "4711";
                else element.Id = Guid.NewGuid().ToString();
                element.ActorObjectTypeId = 100;
                element.ActorId = Guid.NewGuid().ToString();
                element.PrintObjectTypeId = 200;
                element.PrintObjectId = Guid.NewGuid().ToString();
                element.Text = "This is just a sample. " + index.ToString();
                element.Weekday = Weekday.Monday;
                Add(element);
            }

        }
    }
}
RHEL 7.2:24,78秒(虚拟机VMWare工作站)-不正确: 使用“2.0.0-preview2-006497”在RHEL 7.2上的结果 此计算机在VMWARE工作站内运行,使用2 GB RAM和2个内核 启动服务器:[root@node0打印]#/opt/dotnet/dotnet./bin/release/netcoreapp2.0/publish/print.dll

$ curl -w "@curl-format.txt" -s http://10.40.10.23:5000/api/print -o /dev/null
          remote_ip:  10.40.10.23
           local_ip:  10.40.10.23
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0,000000 Seconds
       time_connect:  0,000000 Seconds
    time_appconnect:  0,000000 Seconds
   time_pretransfer:  0,000000 Seconds
      time_redirect:  0,000000 Seconds
 time_starttransfer:  1,232000 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  77479994,000 Bytes/s
----------------------------------------------
         time_total:  3,806000
[root@node0 ~]# curl -w "@curl-format.txt" -s http://10.40.10.59:5000/api/print -o /dev/null
          *remote_ip:  10.40.10.59
           local_ip:  10.40.10.59
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0.000 Seconds
       time_connect:  0.001 Seconds
    time_appconnect:  0.000 Seconds
   time_pretransfer:  0.001 Seconds
      time_redirect:  0.000 Seconds
 time_starttransfer:  21.286 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  11937938.000 Bytes/s
----------------------------------------------
         time_total:  24.702*
[root@node1 ~]# curl -w "@curl-format.txt" -s http://10.40.40.40:5000/api/print -o /dev/null
          remote_ip:  10.40.40.40
           local_ip:  10.40.40.40
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0.000 Seconds
       time_connect:  0.000 Seconds
    time_appconnect:  0.000 Seconds
   time_pretransfer:  0.000 Seconds
      time_redirect:  0.000 Seconds
 time_starttransfer:  18.043 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  13641913.000 Bytes/s
----------------------------------------------
         time_total:  21.616
RHEL 7.4:21,6秒(虚拟机ESX Server)-错误: 使用“2.0.0-preview2-006497”在RHEL 7.4上的结果 此计算机在VMWARE工作站内运行,使用4 GB RAM和2个内核 启动服务器:[root@node0打印]#/opt/dotnet/dotnet./bin/release/netcoreapp2.0/publish/print.dll

$ curl -w "@curl-format.txt" -s http://10.40.10.23:5000/api/print -o /dev/null
          remote_ip:  10.40.10.23
           local_ip:  10.40.10.23
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0,000000 Seconds
       time_connect:  0,000000 Seconds
    time_appconnect:  0,000000 Seconds
   time_pretransfer:  0,000000 Seconds
      time_redirect:  0,000000 Seconds
 time_starttransfer:  1,232000 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  77479994,000 Bytes/s
----------------------------------------------
         time_total:  3,806000
[root@node0 ~]# curl -w "@curl-format.txt" -s http://10.40.10.59:5000/api/print -o /dev/null
          *remote_ip:  10.40.10.59
           local_ip:  10.40.10.59
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0.000 Seconds
       time_connect:  0.001 Seconds
    time_appconnect:  0.000 Seconds
   time_pretransfer:  0.001 Seconds
      time_redirect:  0.000 Seconds
 time_starttransfer:  21.286 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  11937938.000 Bytes/s
----------------------------------------------
         time_total:  24.702*
[root@node1 ~]# curl -w "@curl-format.txt" -s http://10.40.40.40:5000/api/print -o /dev/null
          remote_ip:  10.40.40.40
           local_ip:  10.40.40.40
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0.000 Seconds
       time_connect:  0.000 Seconds
    time_appconnect:  0.000 Seconds
   time_pretransfer:  0.000 Seconds
      time_redirect:  0.000 Seconds
 time_starttransfer:  18.043 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  89 Bytes
     speed_download:  13641913.000 Bytes/s
----------------------------------------------
         time_total:  21.616
OSX:4,0秒(本机)-良好: OSX上的结果(当前版本) 这台机器使用16 GB RAM,4芯 我只是有一个结果的截图。 总时间约为4秒

在Fedora25上,我也得到了25秒的坏结果


我的问题是关于在windows和linux上使用.net内核的性能差异。linux上的.net内核通常比windows上的慢吗?如果不是,那么有什么可能解释如此大的差异呢?

我使用托管在VMWare Workstation 12 Pro中的Windows Server 2012 R2标准、4 GB RAM、2个内核又做了一次测试。在这个测试中,我使用的是官方版本2.0.0。还不错

$ curl -w "@curl-format.txt" -s http://192.168.178.117:5000/api/print -o /dev/null
          remote_ip:  192.168.178.117
           local_ip:  192.168.178.120
        num_connect:  1
      num_redirects:  0
    time_namelookup:  0,000000 Seconds
       time_connect:  0,000000 Seconds
    time_appconnect:  0,000000 Seconds
   time_pretransfer:  0,000000 Seconds
      time_redirect:  0,000000 Seconds
 time_starttransfer:  1,716000 Seconds
      size_download:  294888859 Bytes
        size_header:  148 Bytes
       size_request:  93 Bytes
     speed_download:  60789292,000 Bytes/s
----------------------------------------------
         time_total:  4,851000
这是我期望的值。我怀疑Linux中的JSON序列化中存在一些内部“东西”。我的测试类有7个属性。总共有700万个“json字符串”需要创建。但是,也许我错了,这只是一个在linux机器上激活的设置


希望只有这种棘手的设置或简单的bug可以在未来的版本中修复。对我来说,跨平台c#的可用性是一种梦想,10岁了:)

你应该向GitHub的相关.NET核心回购报告。一般来说,.NET内核的开发在2.0之前更多地集中在API表面,将来还会有更多的性能调整。目前还不能保证它在所有平台上的性能都是相同的(我认为Java也不能保证)。@Dieter,你是在比较.NET内核在本机和虚拟机上的速度吗?您是否尝试在本机Linux机器上而不是虚拟机器上运行.NET core?比较苹果和橙子并不总是最好的方法。你真的认为虚拟机的运行速度会和本机的裸骨操作系统一样快吗?@Lex Li:刚刚在github上制造了这个问题。我不希望在不同的平台上有相同的性能。@Darin Dimitrov:不,我不希望有相同的性能,但今天的世界是虚拟的。即使是在我们公司,大多数客户都在使用任何类型的虚拟化。但是你是对的,我仍然错过了在虚拟windows系统上的相同测试。但我只希望在几秒钟(1到5秒)的时间上有一个加号。如果今天的世界是虚拟的,那么请确保您运行的是虚拟Windows和MAC,以便能够与虚拟Linux进行比较。否则你就是在浪费时间。无论您如何努力,本机裸骨操作系统总是比虚拟操作系统快。这基本上使您的问题变得毫无意义,我投票赞成将其关闭。在VMware Workstation内运行的Windows Server 2016 Core(2 GB RAM,4个Core)上,我得到4.88秒。可以在github上发表以下评论。linux上的函数Guid.NewGuid似乎有问题。