代码之家  ›  专栏  ›  技术社区  ›  simone.tino

Docker映像中的性能差异

  •  2
  • simone.tino  · 技术社区  · 7 年前

    我有一个。NET Core 2.0控制台应用程序根据其运行的Docker base映像具有不同的性能结果。应用程序对字符串执行多个调用。中的StartsWith(string)函数。净额。这是程序。cs文件:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    
    namespace ConsoleApp {
        class Program {
    
            private static string variable = "MYTEXTSTRING";
            private static IEnumerable<string> collection = new[] { "FAF", "LEP", "GHI" };
    
            static void Main(string[] args) {
    
                int counter = args.Length > 0 ? Int32.Parse(args[0]) : 1000;
    
                var stopwatch = new Stopwatch();
                stopwatch.Start();
    
                for (int i = 0; i < counter; i++) {
                    foreach (string str in collection) {
                        variable.StartsWith(str);
                    }
                }
    
                stopwatch.Stop();
    
                Console.WriteLine($"Elapsed time: '{stopwatch.ElapsedMilliseconds}ms' - {counter * collection.Count()} calls to string.StartsWith()");
    
                Console.ReadLine();
    
            }
        }
    }
    

    然后,此代码在Linux Ubuntu VM中的Docker容器中运行。 根据我使用的基本图像,我看到了非常不同的性能结果。

    以下是使用Red Hat base图像的Docker文件:

    # Red Hat base image
    FROM registry.access.redhat.com/dotnet/dotnet-20-rhel7
    
    # set the working directory
    WORKDIR /app
    
    # copy files
    ADD . /app
    
    # run Model.Build
    CMD ["dotnet", "ConsoleApp.dll", "20000"]
    

    以下是使用Linux Debian base映像的Docker文件:

    # Docker Hub base image
    FROM microsoft/dotnet:2.0.5-runtime-jessie
    
    # set the working directory
    WORKDIR /app
    
    # copy files
    ADD . /app
    
    # run Model.Build
    CMD ["dotnet", "ConsoleApp.dll", "20000"]
    

    如您所见,除了基本图像之外,这两个DockerFile实际上是相同的。 以下是我获得的性能结果:

    • Red Hat base image:“运行时间:'540ms'-60000次调用string.StartsWith()”。
    • Docker Hub基本映像:“运行时间:'15ms'-60000次调用string.StartsWith()”。
    • 本机执行:“运行时间:'14ms'-60000次对字符串的调用。StartsWith()”

    因此,虽然使用Debian base映像的容器的性能结果与本机执行非常相似,但使用Red Hat映像的容器的性能要慢得多。

    问题: 为什么StartWith()函数的执行方式如此不同?使用Red Hat base映像时,是什么导致性能下降这么多?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   Tom Deseyn    7 年前

    StartsWith 考虑文化。容器区域性不同,因为它们不具有相同的 LANG 环境变量。

    您可以使用 开始使用 重载以更改区域性的考虑方式(例如。 variable.StartsWith(str, StringComparison.Ordinal) ).