测量Docker容器的执行时间 [英] Measure execution time of docker container

查看:194
本文介绍了测量Docker容器的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 my_image 的docker映像,它启动命令并关闭.

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

使用命令 docker run --rm my_image 在容器中运行映像时,是否可以测量容器的执行时间?

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 ?

我需要在容器执行后查看这些计时信息,因此我不能使用 time 命令.

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

即使使用了-rm ,我还是希望以某种方式找到Docker保留的一些容器执行历史记录.但是,如果它不存在,那么@tgogos的答案是合适的.

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.

推荐答案

第一种方法:时间

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.

您要查找的信息由docker存储,并且可以由 docker container inspect 访问.

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

*请注意,我没有使用-rm ,因为下一步是检查容器.之后,您将必须删除它.您可能感兴趣的时间戳是:

* 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:

  • 已创建":"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"
        }
...

持续时间计算示例(带有bash):

您可以使用以下单个命令将这些时间戳记放入bash变量中:

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)

然后,您可以将它们转换为UNIX纪元时间戳(自1970年1月1日( seconds )

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

这篇关于测量Docker容器的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆