akhela/website-carbon-calculator

估算您的网页碳足迹

1.0.1 2022-06-27 20:04 UTC

This package is auto-updated.

Last update: 2024-09-23 14:21:59 UTC


README

估算您的网页碳足迹

安装

$ composer require akhela/website-carbon-calculator

API

估算网页碳足迹

查看Google 文档以生成 Google Pagespeed Api Key

$websiteCarbonCalculator = new WebsiteCarbonCalculator('GooglePagespeedApiKey');
$websiteCarbonCalculator->calculateByURL('https://www.websitecarbon.com/how-does-it-work/')

输出

{
  "url": "https://www.websitecarbon.com",
  "isGreenHost": true,
  "bytesTransferred": 135289,
  "networkRequests": 16,
  "performanceScore": 0.94,
  "loadingExperience": "FAST",
  "domSize": 308,
  "speedIndex": 934,
  "firstMeaningfulPaint": 491,
  "interactive": 692, 
  "bootupTime": 94,
  "serverResponseTime": 150,
  "mainthreadWork": 762,
  "energy": 0.000171,
  "co2PerPageview": 0.0739
}

为了更好的性能,检测托管能源类型并将其存储在数据库中,以避免重复调用 The Green Web Foundation。

检测托管能源类型

$isGreenHost = WebsiteCarbonCalculator::isGreenHost('https://www.websitecarbon.com')
$websiteCarbonCalculator->calculateByURL('https://www.websitecarbon.com/how-does-it-work/', ['isGreenHost'=>$isGreenHost])

它是如何工作的

计算网站碳排放在某种程度上是一个挑战,但通过使用五个关键数据我们可以做出相当好的估计

  • 有线数据传输
  • 网络数据的能耗强度
  • 数据中心使用的能源类型
  • 电力的碳强度
  • 网站流量

底层使用Google pagespeed apiThe green web foundation api

网站碳计算器

互联网消耗了大量的电力。精确到每年416.2TWh。为了给您一些参考,这个数字超过了整个英国的电力消耗。

从数据中心到传输网络,再到我们手中的设备,都在消耗电力,从而产生碳排放。

该项目广泛受到Website Carbon Calculator算法2.0The Green Web Foundation co2.js的启发

术语表

测试

运行测试

$ pagespeedApiKey=xxxxxx php vendor/bin/phpunit