.net 6 console app : Pre-Compile not work well, neither Crossgen or Publish with ready to run or Publish to Signal App

Viewed 103

i want to pre-compile my .net 6 code to speed up first call of the method, something like ngen.exe do during NetFamework age.

I have a .net 6 testing console app, as simple as below:

internal class Program
{
    private static readonly Stopwatch Stopwatch = new Stopwatch();
    private static int Delay = 3000;
    static void Main(string[] args)
    {
        var end = DateTime.Now.AddMilliseconds(Delay);
        while (DateTime.Now < end)
        {
            continue;
        }
        Stopwatch.Start();
        Console.WriteLine("Test 1");
        Stopwatch.Stop();
        Console.WriteLine($"Cost[{Stopwatch.ElapsedMilliseconds}]ms");
        Stopwatch.Restart();
        Console.WriteLine("Test 2");
        Stopwatch.Stop();
        Console.WriteLine($"Cost[{Stopwatch.ElapsedMilliseconds}]ms");
        Console.ReadLine();
    }
}

i try these methods, there are not work well when i run it.

corssgen

# Publish
$ cd /home/root/src/Tester
$ dotnet publish Tester.csproj -o bin/publish -f net6.0 -c Release -r linux-x64 -p:UseAppHost=true --self-contained=false

# Crossgen2
$ cd /home/root/bin/crossgen2.linux-x64.6.0.5
$ ./crossgen2 -r . --targetarch x64 --targetos linux /home/root/src/Tester/bin/publish/Tester.dll --out /home/root/src/Tester/bin/crossgen/Tester.dll

$ cp /home/root/src/Tester/bin/publish/Tester.deps.json /home/root/src/Tester/bin/crossgen
$ dotnet /home/root/src/Tester/bin/crossgen/Tester.dll

deploying/ready-to-run

$ cd /home/root/src/Tester
$ dotnet publish  -o bin/readyToRun -f net6.0 -c Release -r linux-x64 --self-contained=false -p:PublishReadyToRun=true
$ dotnet /home/root/src/Tester/readyToRun/Tester.dll

deploying/single-file

$ cd /home/root/src/Tester
$ dotnet publish -o bin/native -f net6.0 -c Release -r linux-x64 -p:UseAppHost=true --self-contained=true -p:PublishSingleFile=true -p:PublishTrimmed=true
$ dotnet /home/root/src/Tester/native/Tester.dll

native-aot

$ cp -rf /home/root/src/Tester /home/root/src/Tester_AOT
$ vi /home/root/src/Tester_AOT/Tester.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
  </PropertyGroup>
</Project>

$ cd /home/root/src/Tester_AOT
$ dotnet publish -r linux-x64 -c Release -p:UseAppHost=true --self-contained=false
$ dotnet bin/Release/net6.0/linux-x64/publish/Tester.dll

there output similar like

Test 1
Cost[9]ms
Test 2
Cost[0]ms

if any one can tell me the right way to do this , many thanks.

0 Answers
Related