You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Xdebug 3.1无法统计ZipArchive函数耗时?

背景信息

我正在对一个遗留应用中用于压缩文件的脚本做性能分析。当前在本地虚拟机上,把55MB(约450个文件)添加到Zip归档需要30-35秒,我想通过性能分析找到优化点。应用运行在PHP 7.3(无法升级到8.x版本),Xdebug用的是支持该PHP版本的最新版3.1.6;应用里有个pclzip版本用来创建归档,我打算把它和PHP原生Zip处理做性能对比,看看有没有显著差异。

我用Selenium写了功能测试来大致测量归档时间,两种方法耗时相近,均为30-35秒左右。

kcachegrind输出结果

Xdebug为pclzip生成的耗时分解和调用图符合预期:
pcl耗时统计
pcl调用图

但Xdebug并未为原生Zip处理(ZipArchive)展示类似的耗时分解:
ZipArchive耗时统计
ZipArchive调用图

有意思的是,生成的cachegrind文件大小存在显著差异:

$ ls -lh cachegrind.out.??.gz
-rw-r--r-- 1 bertieb bertieb  32M Jan  5 12:06 cachegrind.out.23.gz
-rw-r--r-- 1 bertieb bertieb 231K Jan  5 12:09 cachegrind.out.26.gz

(cachegrind.out.23.gz为pclzip性能分析文件,cachegrind.out.26.gz为ZipArchive性能分析文件)

问题

造成这种差异的原因是什么?我原本期望两种方法能展示相似的耗时分解和调用图。我刚接触Xdebug,搜索后也未找到该差异的原因。


测试代码

现有pclzip的压缩创建/更新代码:

// in a loop
    $archive = new PclZip ($zipfilename);
    $archive->add ($filename, PCLZIP_OPT_REMOVE_PATH, $removedir);
// loop continues

ZipArchive的创建/更新代码:

// also in loop
    $archive = new ZipArchive();
    $zipres = $archive->open($documentName, ZipArchive::CREATE);
    if ($zipres) {
        $storename = substr($filename, strpos($filename, "upload/"));
        $archive->addFile($filename, $storename); 
        $archive->setCompressionName($storename, ZipArchive::CM_STORE); // contents not very compressible
        // $archive->close(); // ← not performed here as slows down script by ⅙ to ⅓, deferred until after loop instead
    }
// loop continues
// in cleanup
if ($archive) $archive->close();

内容的提问来源于stack exchange,提问作者bertieb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:50:57