如何在 ansible playbook 性能调整中减少 {time.sleep} 时间

how to reduce {time.sleep} time in ansible playbook performance tuning

我正在使用以下剧本进行 ansible 剧本性能调整

---
# simplespeedtest.yml
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - command: echo 1
    - command: echo 2
    - command: echo 30

我已经通过tasts_profile模块测试了剧本的运行时间和剧本中的每个任务,结果如下:

# time $ANSIBLE_HOME/bin/ansible-playbook -i hosts simplespeed_localhost.yml

PLAY [localhost] ********************************************************************************
TASK [command] ********************************************************************************
Thursday 11 January 2018  15:29:45 +0800 (0:00:00.091)       0:00:00.091 ****** 
changed: [localhost]

TASK [command] ********************************************************************************
Thursday 11 January 2018  15:29:45 +0800 (0:00:00.316)       0:00:00.407 ****** 
changed: [localhost]

TASK [command] ********************************************************************************
Thursday 11 January 2018  15:29:46 +0800 (0:00:00.205)       0:00:00.613 ****** 
changed: [localhost]

PLAY RECAP ********************************************************************************
localhost                  : ok=3    changed=3    unreachable=0    failed=0   

Thursday 11 January 2018  15:29:46 +0800 (0:00:00.187)       0:00:00.800 ****** 
=============================================================================== 
command ---------------------------------- 0.32s
command ----------------------------------- 0.21s
command ----------------------------------- 0.19s
Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds

real    0m1.894s
user    0m1.652s
sys 0m0.220s

执行任务的总时间是 0.72 秒,但是执行剧本的总时间是 1.894 秒,这比任务总时间要长,我想知道如何减少这个时间。通过 运行 以下命令:

time python -m cProfile -o test1.out -s time $ANSIBLE_HOME/bin/ansible-playbook -i hosts simplespeed_localhost.yml

我得到了 python cprofile 文件

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      633    0.673    0.001    0.673    0.001 {time.sleep}
    34730    0.072    0.000    0.076    0.000 /usr/local/lib/python2.7/dist-packages/yaml/reader.py:99(forward)
     3597    0.069    0.000    0.213    0.000 /usr/local/lib/python2.7/dist-packages/yaml/scanner.py:1272(scan_plain)
    63467    0.058    0.000    0.128    0.000 /usr/local/lib/python2.7/dist-packages/yaml/scanner.py:142(need_more_tokens)
    38543    0.051    0.000    0.585    0.000 /usr/local/lib/python2.7/dist-packages/yaml/scanner.py:113(check_token)
    66812    0.046    0.000    0.055    0.000 /usr/local/lib/python2.7/dist-packages/yaml/scanner.py:276(stale_possible_simple_keys)
      153    0.042    0.000    0.093    0.001 /usr/lib/python2.7/ConfigParser.py:464(_read)
       46    0.040    0.001    0.040    0.001 {compile}
   162917    0.039    0.000    0.040    0.000 /usr/local/lib/python2.7/dist-packages/yaml/reader.py:87(peek)
    22624    0.032    0.000    0.032    0.000 /usr/local/lib/python2.7/dist-packages/yaml/error.py:6(__init__)
     4981    0.031    0.000    0.150    0.000 /usr/local/lib/python2.7/dist-packages/yaml/parser.py:273(parse_node)
  967/207    0.030    0.000    0.089    0.000 /usr/lib/python2.7/sre_parse.py:379(_parse)
     7649    0.028    0.000    0.428    0.000 /usr/local/lib/python2.7/dist-packages/yaml/scanner.py:153(fetch_more_tokens)
      101    0.026    0.000    0.026    0.000 {method 'read' of 'file' objects}
    22624    0.026    0.000    0.058    0.000 /usr/local/lib/python2.7/dist-packages/yaml/reader.py:114(get_mark)
   100506    0.025    0.000    0.029    0.000 {isinstance}

调用{time.sleep}的时间是0.673s,我想知道ansible的哪个进程会调用{time.sleep},如何减少

ps: 我从 ./lib/ansible/executor/task_executor.py 中的 ansible 源代码中获得了 3 个位置调用 {time.sleep} 并更改了 arg 但它似乎不起作用

您需要更仔细地考虑要完成的任务。一个包含琐碎任务且没有 SSH 连接的简单 playbook 不能代表现实生活中 ansible playbook 的实际性能,因此您可能得出的任何分析结果都将毫无意义。

此外,Ansible的任务是在目标机器上远程执行的(或者对于本地任务,在子进程中执行)。这意味着配置文件将无法测量正在执行的实际模块代码。所以简单地 运行 ansible-playbook 通过分析器并不能显示完整的故事。

话虽如此,让我们尝试从探查器中获取更多信息。您可以使用 gprof2dot 从配置文件输出生成调用图,详见 here。该图将根据每个功能所花费的时间进行着色。

这是当我 运行 在我的计算机上使用剧本时该图表的样子。根据您系统的性能特征,它可能看起来有所不同:

查看图表,我们可以看到 time.sleep() 是从 ansible.plugins.strategy.__init__.py module. We can see from the code that the sleep occurs while ansible is waiting for tasks to complete. You can change the polling interval in the configuration file 调用的,但默认值为 0.001 秒,这已经相当快了。改变这一点不太可能产生重大影响。

总结一下:

  • 您需要更现实的剧本来进行有效的基准测试
  • 由于远程任务执行,探查器数据有限
  • 更改睡眠间隔不会显着提高性能

以下是来自 Ansible 团队的一些与性能相关的建议,可能会有帮助: