使用 Saxon-JS 识别 XSLT 转换的性能瓶颈

问题描述

谁能提供一些指导以查明转换中的瓶颈?

这是 Saxon-JS 的 node.js 实现。我正在尝试提高转换某些 XML 文档的速度,以便我可以提供一个同步 API,理想情况下可以在 60 秒内响应(230 秒是应用程序网关的硬限制)。我还需要能够处理最多 50MB 大小的 XML 文件

我已经运行了节点内置的分析器 (https://nodejs.org/en/docs/guides/simple-profiling/)。但鉴于 Saxon-JS 的免费版本的源代码并不是人类可读的,因此很难理解结果。

我的代码

const path = require('path');
const Saxonjs = require('saxon-js');
const { loadCodelistsInMem } = require('../standards_cache/codelists');
const { writeFile } = require('../config/fileSystem');
const config = require('../config/config');
const { getStartTime,getelapsedtime } = require('../config/appInsights');

// Used for easy debugging the xslt stylesheet
// Runs iati.xslt transform on the supplied XML
const runTransform = async (sourceFile) => {
    try {
        const fileName = path.basename(sourceFile);

        const codelists = await loadCodelistsInMem();

        // this pulls the right array of Saxonjs resources from the resources object
        const collectionFinder = (url) => {
            if (url.includes('codelist')) {
                // get the right filepath (remove file:// and after the ?
                const versionPath = url.split('schemata/')[1].split('?')[0];
                if (codelists[versionPath]) return codelists[versionPath];
            }
            return [];
        };

        const start = getStartTime();
        const result = await Saxonjs.transform(
            {
                sourceFileName: sourceFile,stylesheetFileName: `${config.TMP_BASE_DIR}/data-quality/rules/iati.sef.json`,destination: 'serialized',collectionFinder,logLevel: 10,},'async'
        );
        console.log(`${getelapsedtime(start)} (s)`);

        await writeFile(`performance_tests/output/${fileName}`,result.principalResult);
    } catch (e) {
        console.log(e);
    }
};

runTransform('performance_tests/test_files/test8meg.xml');

控制台输出示例:

❯ node --prof utils/runTransform.js
SEF generated by Saxon-JS 2.0 at 2021-01-27T17:10:38.029Z with -target:JS -relocate:true
79.938 (s)
❯ node --prof-process isolate-0x102d7b000-19859-v8.log > v8_log.txt

文件

最大的性能违规者的 V8 日志片段:

 [Bottom up (heavy) profile]:
  Note: percentage shows a share of a particular caller in the total
  amount of its parent calls.
  Callers occupying less than 1.0% are not shown.

   ticks parent  name
  33729   52.5%  T __ZN2v88internal20Builtin_ConsoleClearEiPmPNS0_7IsolateE
   6901   20.5%    T __ZN2v88internal20Builtin_ConsoleClearEiPmPNS0_7IsolateE
   3500   50.7%      T __ZN2v88internal20Builtin_ConsoleClearEiPmPNS0_7IsolateE
   3197   91.3%        LazyCompile: *k /Users/nosvalds/Projects/validator-api/node_modules/saxon-js/Saxonjs2N.js:287:264
   3182   99.5%          LazyCompile: *<anonymous> /Users/nosvalds/Projects/validator-api/node_modules/saxon-js/Saxonjs2N.js:682:218
   2880   90.5%            LazyCompile: *d /Users/nosvalds/Projects/validator-api/node_modules/saxon-js/Saxonjs2N.js:734:184

非常感谢。没有大量的资源可以让我自己完成。我也已经试过了:

  • 将 stylesheetInternal 参数与预解析的 JSON 结合使用(没有太大区别)
  • 将文档拆分为单独的文档,这些文档在根 <iati-activity> 根元素内仅包含一个活动 <iati-activities> 子元素,分别转换每个子元素,然后将其重新组合在一起,最终需要 2 倍的时间。

最好,

尼克

解决方法

您在 https://saxonica.plan.io/boards/5/topics/8105?r=8106 提出了同样的问题,我已经在那里做出了回应。我知道 StackOverflow 不喜欢仅提供链接的答案,但我更喜欢通过我们自己的支持渠道而不是在可能的情况下通过 StackOverflow 为用户提供支持。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...