Measure execution time of docker container

谁说我不能喝 提交于 2021-02-07 02:56:28

问题


I have a docker image called my_image which launch a command and closes.

When running the image in a container using command docker run --rm my_image, is it possible to measure the execution time of the container ?

Edit :

I need to see those timing information after container execution, thus I can't use time command.

I somehow hoped to find some container execution history kept by docker even if --rm was used. But if it doesn't exist, then @tgogos' answer is suited.

The goal is to compare execution time of several images to draw a conclusion about the different tools used.


回答1:


1st approach: time

time docker run --rm --name=test alpine ping -c 10 8.8.8.8
...

real    0m10.261s
user    0m0.228s
sys 0m0.044s

but this will also include the time for creating and removing the container.

2nd approach: container information

The information you are looking for is stored by docker and can be reached by docker container inspect.

docker run --name=test alpine ping -c 10 8.8.8.8

* notice that I didn't use --rm because the next step is to inpect the container. You will have to remove it afterwards. The timestamps you might be interested in are:

  • "Created": "2018-08-02T10:16:48.59705963Z",
  • "StartedAt": "2018-08-02T10:16:49.187187456Z",
  • "FinishedAt": "2018-08-02T10:16:58.27795818Z"

$ docker container inspect test

[
    {
        "Id": "96e469fdb437814817ee2e9ad2fcdbf468a88694fcc998339edd424f9689f71f",
        "Created": "2018-08-02T10:16:48.59705963Z",
        "Path": "ping",
        "Args": [
            "-c",
            "10",
            "8.8.8.8"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-08-02T10:16:49.187187456Z",
            "FinishedAt": "2018-08-02T10:16:58.27795818Z"
        }
...

Duration calculation example (with bash):

You can put these timestamps in bash variables with single commands like this:

START=$(docker inspect --format='{{.State.StartedAt}}' test)
STOP=$(docker inspect --format='{{.State.FinishedAt}}' test)

Then you can convert them to UNIX epoch timestamps (seconds since Jan 01 1970. (UTC))

START_TIMESTAMP=$(date --date=$START +%s)
STOP_TIMESTAMP=$(date --date=$STOP +%s)

and if you subtract these two, you get the duration in seconds...

echo $(($STOP_TIMESTAMP-$START_TIMESTAMP)) seconds
9 seconds



回答2:


You have to consider a couple of things:

  1. How to get time execution of a process given a PID.
  2. Execute docker exec specifying PID=1 because container time running = entrypoint running.

Combining two things you get docker container time execution with:

docker exec -ti <container_id> ps -o etime= -p "1"

It gives you more accuracy than column STATUS of docker ps command.

If you're executing several processes in container and you need execution time for any of them, just replace "1" by its PID inside container.




回答3:


Another possible approach may be to override the default entrypoint with the time command.

$ docker run --rm --name=test --entrypoint=time alpine ping -c 10 8.8.8.8

PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=37 time=51.213 ms
64 bytes from 8.8.8.8: seq=1 ttl=37 time=7.844 ms
64 bytes from 8.8.8.8: seq=2 ttl=37 time=8.120 ms
64 bytes from 8.8.8.8: seq=3 ttl=37 time=10.859 ms
64 bytes from 8.8.8.8: seq=4 ttl=37 time=10.975 ms
64 bytes from 8.8.8.8: seq=5 ttl=37 time=12.520 ms
64 bytes from 8.8.8.8: seq=6 ttl=37 time=7.994 ms
64 bytes from 8.8.8.8: seq=7 ttl=37 time=8.904 ms
64 bytes from 8.8.8.8: seq=8 ttl=37 time=6.674 ms
64 bytes from 8.8.8.8: seq=9 ttl=37 time=7.132 ms

--- 8.8.8.8 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss
round-trip min/avg/max = 6.674/13.223/51.213 ms
real    0m 9.02s
user    0m 0.00s
sys 0m 0.00s

Doing this won't include the container start up time. You can even do something like: time docker run --rm --name=test --entrypoint=time alpine ping -c 10 8.8.8.8 to see how long just the container startup takes.



来源:https://stackoverflow.com/questions/51650405/measure-execution-time-of-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!