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 ?


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

$ cat
for i in `seq 1 100`;
        lxc exec mycontainer uptime 1&>/dev/null

$ time bash 
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 
for i in `seq 1 100`;
        uptime 1&>/dev/null

ubuntu@mycontainer:~$ time bash


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 mycontainer/tmp/
$ lxc exec mycontainer -- time /tmp/

Cheers @simos thanks :slight_smile: