Design Automation令牌使用量估算与APS仪表板数据不符问题排查
问题
我按照APS官方博客的方法计算Design Automation的令牌使用量及成本,在workitem的oncomplete回调中执行以下代码并存储到数据库:
function getJobMetrics(jobDetails) { const { stats } = jobDetails.jobStatus; const timeQueued = Date.parse(stats.timeQueued); const timeDownloadStarted = Date.parse(stats.timeDownloadStarted); const timeUploadEnded = Date.parse(stats.timeUploadEnded); const timeInstructionsStarted = Date.parse(stats.timeInstructionsStarted); const timeInstructionsEnded = Date.parse(stats.timeInstructionsEnded); const queueDelay = timeDownloadStarted - timeQueued; const downloadDelay = timeInstructionsStarted - timeDownloadStarted; const instructionsRunDuration = timeInstructionsEnded - timeInstructionsStarted; const totalDuration = timeInstructionsEnded - timeQueued; // 参考官方博客,失败时timeUploadEnded为null const adskCalculatedTimeTaken = (timeUploadEnded || timeInstructionsEnded) - timeDownloadStarted; const adskTokenUsageInCloudCredits = (adskCalculatedTimeTaken / 1000 / 60 / 60) * 6; return { adskDasQueueDelay: queueDelay, adskDasDownloadDelay: downloadDelay, adskDasInstructionsRunDuration: instructionsRunDuration, adskDasTotalDuration: totalDuration, adskDasTotalBytesDownloaded: stats.bytesDownloaded, adskCalculatedTimeTaken, adskTokenUsageInCloudCredits, }; }
但将月度所有workitem的adskTokenUsageInCloudCredits汇总后,与APS分析仪表板的数据不符,我的数值高出一个数量级。未找到可查询令牌消耗事件的API,请问问题出在哪里?
可能的问题原因及修正方案
1. 计费时长范围错误
APS Design Automation的实际计费逻辑仅针对指令执行阶段(即timeInstructionsStarted到timeInstructionsEnded的时长),而你的代码将下载、指令执行、上传的总时长(timeDownloadStarted到timeUploadEnded)纳入计算,这其中下载和上传阶段并不产生计费,直接导致统计值大幅偏高。
2. 时间戳时区解析偏差
APS返回的所有时间戳均为UTC格式,Date.parse()在部分运行环境中会自动转换为本地时区,导致时间差计算出现误差。建议统一使用UTC时间解析逻辑。
3. 重试任务重复统计
如果你的系统存在workitem失败重试机制,可能重复统计了同一任务的多次执行时长,而APS仪表板通常仅统计最终成功的任务计费,或合并重试的实际计费时长。
修正后的代码示例
function getJobMetrics(jobDetails) { const { stats } = jobDetails.jobStatus; // 统一按UTC解析时间,避免时区偏差 const parseUtcTime = (timeStr) => new Date(timeStr).getTime(); const timeInstructionsStarted = parseUtcTime(stats.timeInstructionsStarted); const timeInstructionsEnded = parseUtcTime(stats.timeInstructionsEnded); // 仅统计指令执行的计费时长 const billedDurationMs = timeInstructionsEnded - timeInstructionsStarted; // 按官方6 Cloud Credits/小时的标准计算 const adskTokenUsageInCloudCredits = (billedDurationMs / 1000 / 60 / 60) * 6; return { adskDasInstructionsRunDuration: billedDurationMs, adskTokenUsageInCloudCredits, // 保留其他非计费指标 adskDasQueueDelay: parseUtcTime(stats.timeDownloadStarted) - parseUtcTime(stats.timeQueued), adskDasDownloadDelay: timeInstructionsStarted - parseUtcTime(stats.timeDownloadStarted), adskDasTotalDuration: timeInstructionsEnded - parseUtcTime(stats.timeQueued), adskDasTotalBytesDownloaded: stats.bytesDownloaded, adskCalculatedTimeTaken: (parseUtcTime(stats.timeUploadEnded) || timeInstructionsEnded) - parseUtcTime(stats.timeDownloadStarted) }; }
内容的提问来源于stack exchange,提问作者solmans
相关产品推荐
相关产品推荐

