1

我想将 Google 移动友好和桌面友好 api 与 PageSpeed Insights API V5 集成。但我无法区分审计部分。我也尝试过不同的场景,但我做不到。

如何区分PageSpeed Insights API V5 中通过的审核诊断机会?

4

2 回答 2

1

下面是 GoogleChrome 灯塔用来区分机会、诊断和通过审核的代码,您可以在下面的 github 链接中找到这些代码。

// Opportunities
const opportunityAudits = category.auditRefs
        .filter(audit => audit.group === 'load-opportunities' && !Util.showAsPassed(audit.result))
        .sort((auditA, auditB) => this._getWastedMs(auditB) - this._getWastedMs(auditA));

// Diagnostics
const diagnosticAudits = category.auditRefs
        .filter(audit => audit.group === 'diagnostics' && !Util.showAsPassed(audit.result))
        .sort((a, b) => {
          const scoreA = a.result.scoreDisplayMode === 'informative' ? 100 : Number(a.result.score);
          const scoreB = b.result.scoreDisplayMode === 'informative' ? 100 : Number(b.result.score);
          return scoreA - scoreB;
        });

// Passed audits
const passedAudits = category.auditRefs
        .filter(audit => (audit.group === 'load-opportunities' || audit.group === 'diagnostics') &&
            Util.showAsPassed(audit.result));

参考:https ://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/performance-category-renderer.js

在上面的代码中, Util.showAsPassed() 方法被指定如下。

const PASS_THRESHOLD = 0.9;
const RATINGS = {
  PASS: {label: 'pass', minScore: PASS_THRESHOLD},
  AVERAGE: {label: 'average', minScore: 0.5},
  FAIL: {label: 'fail'},
  ERROR: {label: 'error'},
};

static showAsPassed(audit) {
    switch (audit.scoreDisplayMode) {
      case 'manual':
      case 'notApplicable':
        return true;
      case 'error':
      case 'informative':
        return false;
      case 'numeric':
      case 'binary':
      default:
        return Number(audit.score) >= RATINGS.PASS.minScore;
    }
  }

参考: https ://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/util.js

谢谢

于 2019-01-28T09:20:15.477 回答
0

** 机会:** 响应中没有可用的已编译机会集,但我们可以循环通过 lighthouseResult 和对于 lighthouseReslt 中的每个 json,取出 type=opportunity 的结果,它还应该有详细信息。

response = requests.get('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url='+<url>+'&strategy='+<desktop or mobile>+'&key='+<api_key>)
js = response.json()

print ("Score: ",js['lighthouseResult']['categories']['performance']['score']*100)

k = []

for m in js['lighthouseResult'] :

    try :
        for i in js['lighthouseResult'][m] :
            k.append([js['lighthouseResult'][m][i]['details'],js['lighthouseResult'][m][i]['title']])
    except :
        pass

final_opportunities = []

print (len(k))
for i in k :
    if 'overallSavingsMs' in list(i[0].keys()) :

        print (i[1],i[0]['overallSavingsMs'])
        final_opportunities.append([i[1] , i[0]['overallSavingsMs']])

审核结果可以在:lighthouseResult.audits 中找到

综合性能得分:response.lighthouseResult.categories.performance.score

对于 python 实现,您可以参考以下 github repo: https ://github.com/amartya-dev/PageSpeedAPI

于 2019-06-13T06:56:26.940 回答