mariotoffia / FluentDocker

Use docker, docker-compose local and remote in tests and your .NET core/full framework apps via a FluentAPI
Apache License 2.0
1.31k stars 97 forks source link

Performance of start/stop/remove container #215

Closed forfcw closed 2 years ago

forfcw commented 2 years ago

Have you got some performance problems with start/stop/remove containers?

var hosts = new Hosts().Discover();
var host = hosts.FirstOrDefault(x => x.IsNative) ?? hosts.FirstOrDefault(x => x.Name == "default");
var _container = host.Create("nginx:alpine",
    prms: new ContainerCreateParams
    {
        Name = "test",
        Network = "host",
        PortMappings = new string[] { "9111:80", "9112:443" },
        Volumes = new string[] { "/data/log:/var/log:rw" },
        RestartPolicy = RestartPolicy.Always
    });
_container.Start();

It usually takes 4s or more to start a container, sometimes finding and deleting a container even takes 6s.

[root@root:~]# dotnet test.dll 
start: 5.230721
stop: 1.2377657
remove:1.7702362

Looking forward to your recovery!

mariotoffia commented 2 years ago

Hi @forfcw and thanks for reporting this issue. I've done a simple test to try to replicate what you see.

In my case, I have a really weak hp dragonfly laptop with an "Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, 1992 Mhz, 4 Core(s)" with probably not the fastest ram in the world ;)

I'm using WSL2.

Using the following sample (I will check it in this into Examples Solution later on)

        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        var hosts = new Hosts().Discover();
        var host = hosts.FirstOrDefault(x => x.IsNative) ?? hosts.FirstOrDefault(x => x.Name == "default");

        Console.WriteLine("Hosts discovered in {0} s", TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds);

        var _container = host.Create("nginx:alpine",
            prms: new ContainerCreateParams
            {
                Name = "test",
                Network = "host",
                PortMappings = new string[] { "9111:80", "9112:443" },
                Volumes = new string[] { "/data/log:/var/log:rw" },
                RestartPolicy = RestartPolicy.Always
            });

        Console.WriteLine("Create container: " + TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds);

        try
        {
            stopwatch.Restart();
            _container.Start();

            Console.WriteLine("Start container: " + TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds);
        } finally
        {

            stopwatch.Restart();
            _container.Dispose();
            Console.WriteLine("Dispose container: " + TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds);

            stopwatch.Stop();

        }

It will output the following:

Hosts discovered in 0.083 s
Create container: 1.51 s
Start container: 0.56 s
Dispose container: 0.353 s

When I try to do it on my shell, the creation will take around one to two seconds so I think FluentDocker, in this case, do not add any substantial additional time.

I will investigate this a bit further, but in the meantime can you please use the docker commands from the shell and clock how long an "nginx:alpine" takes to start and be deleted on your system?

💡 Note that it will call both stop and remove when disposing the container.

docker -H unix:///var/run/docker.sock start 37ec8c07e093b452b3a18346b4338dd7bdb33d203582b1bd8d0035f888f2921a
docker -H unix:///var/run/docker.sock inspect 37ec8c07e093b452b3a18346b4338dd7bdb33d203582b1bd8d0035f888f2921a

docker -H unix:///var/run/docker.sock stop 37ec8c07e093b452b3a18346b4338dd7bdb33d203582b1bd8d0035f888f2921a
docker -H unix:///var/run/docker.sock rm --force 37ec8c07e093b452b3a18346b4338dd7bdb33d203582b1bd8d0035f888f2921a

Cheers, Mario

forfcw commented 2 years ago

May be I wronged FULENTDOCKER, It takes the same time in shell!