linux 主机PHP vs Docker PHP:慢5倍

e5nqia27  于 2023-02-07  发布在  Linux
关注(0)|答案(1)|浏览(140)

我们正在使用Docker进行PHP开发,我对在主机上运行PHP 7.4与在Docker容器中运行代码的性能差异很感兴趣。
我运行的是最新的Linux Mint版本,docker版本是Docker version 20.10.17-没有特殊的非标准配置。
为了公平地比较两个PHP环境,我仔细检查了配置,当然也禁用了xdebug。
然后我运行一些基本的PHP基准测试,我发现在github:

<?php

$startTime = time();
$runSeconds = 5;
$rounds = 5;
$count = 0;
for ($i=0; $i<$rounds; $i++) {
    while ((time()-$startTime) <= $runSeconds) {
        serialize(['bar'=>'foo']);
        $count ++;
    }
    $round = $i+1;
    $startTime += $runSeconds;
    $count = number_format($count);
    echo "Round {$round}: {$count} /per {$runSeconds} second\n";
    $count = 0;
}

在我的主机PHP 7.4上的结果:

Round 1: 93,052,589 /per 5 second
Round 2: 89,096,400 /per 5 second
Round 3: 89,190,317 /per 5 second
Round 4: 89,145,362 /per 5 second
Round 5: 88,923,066 /per 5 second

Docker PHP 7.4

https://hub.docker.com/layers/php/library/php/7.4.30-zts-alpine3.15/images/sha256-6e1a13b9e0446eedc3a1220b24ed52727b3330a165b5787395c1a3082e355481?context=explore

Round 1: 18,937,581 /per 5 second
Round 2: 16,089,889 /per 5 second
Round 3: 16,083,545 /per 5 second
Round 4: 16,087,953 /per 5 second
Round 5: 16,079,214 /per 5 second

这是一个巨大的差异。是否有一些共同的瓶颈,解释了巨大的差异?
我认为Docker没有太多的开销,至少开销应该是可以忽略不计的-但5倍慢的性能已经是很多,我认为。

6mw9ycah

6mw9ycah1#

我可以使用一个非常类似的测试来确认Alpine在我的机器上的执行速度明显更慢。
看起来它可能与不同的底层库和不同的内核选项有关(例如,RAM大页面被禁用,opcache不会从中受益)。
比较(越高越好,即25秒内的迭代计数,opcache已禁用):

OS 7.4.18 (current)             Total: 89,226,100
Custom 7.4.32 Alpine            Total: 57,472,042
Official 7.4.33 Bullseye        Total: 101,363,370
Official 8.1.15 Alpine          Total: 53,698,833
Official 8.1.15 Bullseye        Total: 103,377,353
Official 8.1.15 Bullseye+JIT    Total: 102,026,978
Official 7.4.33 Alpine          Total: 58,911,670

测试片段:

...

for ($i = 0; $i < $rounds; $i++) {
    $startTime = time();
    while ((time() - $startTime) <= $runSeconds) {
        $prefix = (string)$count . '|' . (string)$i;
        json_encode([$prefix . 'bar' => $prefix . 'foo'], JSON_THROW_ON_ERROR);
        $count++;
    }

    $round = $i + 1;
    $startTime += $runSeconds;
    $total += $count;
    $count = number_format($count);

    echo " Round {$round}: {$count} per {$runSeconds} second\n";

    $count = 0;
}
echo " Total: " . number_format($total) . "\n";

相关问题