【问题标题】:dotnet core 2 long build time because of long restore timedotnet core 2 构建时间长,因为恢复时间长
【发布时间】:2018-01-27 13:25:58
【问题描述】:

我注意到在 dotnet core 2 中构建似乎要慢得多。
但构建后的时间总是显示“仅”15 秒。
我不敢相信,所以我用time 计时。

> time dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  hrm -> /Users/r/dev/hrm/bin/Debug/netcoreapp2.0/hrm.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:15.45

real    0m52.366s
user    0m36.851s
sys     0m15.458s

这似乎更正确。差不多一分钟。
然后我尝试不恢复,它快了很多:

> time dotnet build --no-restore
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  hrm -> /Users/r/dev/hrm/bin/Debug/netcoreapp2.0/hrm.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:15.39

real    0m15.795s
user    0m11.397s
sys     0m4.238s

但 dotnet 也显示 15 秒。
会不会只计算建筑物的时间?
不知道为什么当一切都已经恢复时,恢复总是很慢。

还有其他方法可以加快构建过程吗?禁用遥测? (我用的是osx,我的环境设置为开发)

我更喜欢使用dotnet watch run,但这似乎更慢。 运行dotnet watch查看参数需要12秒。

> time dotnet watch
Microsoft DotNet File Watcher 2.0.0-rtm-26452

Usage: dotnet watch [options] [[--] <arg>...]

Options:
  ....


real    0m12.631s
user    0m8.880s
sys     0m3.816s

这仅在我的系统上吗?

更新:

这是 dotnet restore /clp:PerformanceSummary 的结果

> dotnet restore /clp:PerformanceSummary
  Restore completed in 43.95 ms for /Users/roeland/dev/hrm/hrm.csproj.
  Restore completed in 52.73 ms for /Users/roeland/dev/hrm/hrm.csproj.
  Restore completed in 38.48 ms for /Users/roeland/dev/hrm/hrm.csproj.

Project Evaluation Performance Summary:
    36252 ms  /Users/roeland/dev/hrm/hrm.csproj          3 calls

Project Performance Summary:
    36424 ms  /Users/roeland/dev/hrm/hrm.csproj          9 calls
              24359 ms  Restore                                    1 calls
                  1 ms  _IsProjectRestoreSupported                 2 calls
              12011 ms  _GenerateRestoreProjectPathWalk            1 calls
                  1 ms  _GenerateRestoreProjectPathItemsPerFramework   1 calls
                 43 ms  _GenerateRestoreGraphProjectEntry          1 calls
                  0 ms  _GetRestoreSettingsPerFramework            1 calls
                  6 ms  _GenerateProjectRestoreGraph               1 calls
                  3 ms  _GenerateProjectRestoreGraphPerFramework   1 calls

Target Performance Summary:
        0 ms  _GenerateRestoreGraphProjectEntry          1 calls
        0 ms  _GenerateProjectRestoreGraph               1 calls
        0 ms  _GetRestoreTargetFrameworksAsItems         1 calls
        0 ms  _GetRestoreProjectStyle                    2 calls
        0 ms  CheckForImplicitPackageReferenceOverridesBeforeRestore   2 calls
        0 ms  _CheckForUnsupportedNETCoreVersion         1 calls
        0 ms  _IsProjectRestoreSupported                 1 calls
        0 ms  _GetRestoreSettingsPerFramework            1 calls
        0 ms  _GetProjectJsonPath                        2 calls
        0 ms  _GetRestoreSettingsOverrides               1 calls
        1 ms  _GenerateRestoreProjectPathWalk            1 calls
        1 ms  _GenerateRestoreProjectPathItemsPerFramework   1 calls
        1 ms  _GenerateRestoreSpecs                      1 calls
        1 ms  _GenerateRestoreProjectSpec                1 calls
        2 ms  _GenerateProjectRestoreGraphPerFramework   1 calls
        2 ms  _GetRestoreTargetFrameworksOutput          1 calls
        5 ms  _GenerateRestoreDependencies               1 calls
       10 ms  _LoadRestoreGraphEntryPoints               1 calls
       20 ms  _GenerateDotnetCliToolReferenceSpecs       1 calls
       21 ms  _GetRestoreSettings                        1 calls
       54 ms  _GenerateRestoreGraph                      1 calls
      216 ms  Restore                                    1 calls
    12007 ms  _GenerateRestoreProjectPathItems           1 calls
    12014 ms  _GetAllRestoreProjectPathItems             1 calls
    12058 ms  _FilterRestoreGraphProjectInputItems       1 calls

Task Performance Summary:
        1 ms  Message                                    3 calls
        1 ms  ConvertToAbsolutePath                      2 calls
        1 ms  GetRestorePackageReferencesTask            1 calls
        1 ms  GetRestoreProjectReferencesTask            1 calls
        2 ms  GetRestoreProjectFrameworks                1 calls
        3 ms  RemoveDuplicates                           5 calls
        4 ms  WarnForInvalidProjectsTask                 1 calls
       18 ms  GetRestoreSettingsTask                     1 calls
       20 ms  GetRestoreDotnetCliToolsTask               1 calls
      216 ms  RestoreTask                                1 calls
    36121 ms  MsBuild                                    9 calls

【问题讨论】:

  • 使用--no-restore选项有区别吗?
  • 我试过dotnet watch run --no-restore,但似乎没有帮助。我这里没有时间。
  • 不确定是否必须是 dotnet watch run -- --no-restore,但我得到了格式异常。
  • 如果恢复时间考虑到该延迟会很有趣。 dotnet restore /clp:PerformanceSummary 应该提供一些见解
  • 所以你的项目中包含node_modules?这可能会导致..

标签: performance msbuild .net-core csproj


【解决方案1】:

对我来说,排除 .git 文件夹有助于使构建速度提高 10 倍左右。

  <PropertyGroup>
    <DefaultItemExcludes>.git\**;$(DefaultItemExcludes)</DefaultItemExcludes>
  </PropertyGroup>

【讨论】:

  • 排除git文件夹的目的是什么?它已经不包括在内
【解决方案2】:

长话短说:MSBuild 根据使用的 SDK 定义的 glob 模式扫描整个文件夹结构。这是针对每个项目评估完成的,并且 NuGet 还原似乎触发了至少三个完整的评估。

由于扫描大型目录的速度很慢,SDK 定义了通配模式,用于排除一些通常不希望作为项目一部分的已知大型目录(node_modulesbower_components 等)。

众所周知,特殊情况可能会绕过这些优化,甚至会触发包含/排除 glob 模式扩展/匹配中的性能错误。

作为预防措施,将所有已知排除在外的文件夹添加到DefaultItemExcludes 属性(在&lt;PropertyGroup&gt; 元素内):

<DefaultItemExcludes>custom\node_modules\**;$(DefaultItemExcludes)</DefaultItemExcludes>

【讨论】:

  • 如果我使用它,它会排除我项目中的所有文件,并且我使用要明确包含的文件。不是更好吗? **;$(DefaultItemExcludes)
  • 您也可以这样做,但您也可以通过 &lt;EnableDefaultItems&gt;false&lt;/EnableDefaultItems&gt; 禁用默认项目,然后添加您需要的所有 &lt;Compile Include=".."/&gt;/&lt;Content Include="..." /&gt; 项目。
  • 我在 Docker 中进行简单的dotnet new mvc 测试时遇到了这个问题。通过创建一个子文件夹作为工作目录解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 2021-10-26
相关资源
最近更新 更多