Any difference in resource usage running lxc exec or cmd directly within container?

I am doing some Nginx HTTP/2 HTTPS comparison benchmarks between different LEMP stack installers installed in their respective LXD containers.

Is there any resource usage differences between running the benchmark command directly within the LXD container image versus running the command via lxc exec image-name -- cmd ?

Is it more accurate to run the benchmark commands within the LXD container as opposed to via lxc exec command ?

Hi!

Let’s see how much of a difference it makes.

$ cat bench.sh
#!/bin/bash
for i in `seq 1 100`;
do
        lxc exec mycontainer uptime 1&>/dev/null
done    

$ time bash bench.sh 
Elapsed time   : 0m8.382s
User mode      : 0m1.640s
System mode    : 0m1.269s
CPU percentage : 34.70

$ lxc exec mycontainer -- sudo --user ubuntu --login
ubuntu@mycontainer:~$ cat bench.sh 
#!/bin/bash
for i in `seq 1 100`;
do
        uptime 1&>/dev/null
done    

ubuntu@mycontainer:~$ time bash bench.sh

|real|0m0.242s|
|---|---|
|user|0m0.133s|
|sys|0m0.086s|

That means that in this case, it takes about 80ms to do the lxc exec.
Depending on the benchmark, if you are measuring processes that take several minutes,
then it does not matter much to lose 80ms with lxc exec.

I would consider a fair way to conduct the benchmark, is by:

$ lxc file push mybenchmark.sh mycontainer/tmp/
$ lxc exec mycontainer -- time /tmp/mybenchmark.sh
2 Likes

Cheers @simos thanks :slight_smile: