akhela / website-carbon-calculator
估算您的网页碳足迹
1.0.1
2022-06-27 20:04 UTC
Requires
- ext-json: *
- guzzlehttp/guzzle: ~6.0
- sabre/uri: ^2
Requires (Dev)
- phpunit/phpunit: ^9.5
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 api和The green web foundation api
网站碳计算器
互联网消耗了大量的电力。精确到每年416.2TWh。为了给您一些参考,这个数字超过了整个英国的电力消耗。
从数据中心到传输网络,再到我们手中的设备,都在消耗电力,从而产生碳排放。
该项目广泛受到Website Carbon Calculator算法2.0和The Green Web Foundation co2.js的启发
术语表
- isGreenHost : 服务器使用可持续能源
- networkRequests : https://webdev.ac.cn/performance-scoring/
- performanceScore : https://webdev.ac.cn/resource-summary/
- domSize : https://webdev.ac.cn/dom-size/
- speedIndex : https://webdev.ac.cn/speed-index/
- firstMeaningfulPaint : https://webdev.ac.cn/first-meaningful-paint/
- interactive : https://webdev.ac.cn/interactive/
- bootupTime : https://webdev.ac.cn/bootup-time/
- serverResponseTime : https://webdev.ac.cn/time-to-first-byte/
- mainthreadWork : https://webdev.ac.cn/mainthread-work-breakdown/
- co2PerPageview : 每次有人访问网页时产生的CO2估计克数
测试
运行测试
$ pagespeedApiKey=xxxxxx php vendor/bin/phpunit