Greening Digital Workshop:
�Set up your django app with green coding metrics
GREENWEB.ORG
Chris
Adams
2024.06.07
DjangoconEU 2024
1
GREEN WEB FOUNDATION
Hello, I’m Chris!
My background:
Loco2 - Low CO2 Travel in Europe by train
A.M.E.E (Avoid Mass Extinction Engine) - CO2 calculation as an API
Icebreaker One - data infrastructure for a net zero future
Spend Network - direct public spending for net zero
Green Software Foundation - Policy chair
Branch Magazine - editor
Green Web Foundation - make the web green
Environment Variables podcast - co-host
2
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Housekeeping
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
About the workshop
Aim: Provide an overview of digital carbon estimation approaches and tooling, and help you take your first steps making calculations. Learn to use this to optimise Django / Python projects for carbon.
Duration: 50 mins
You will need:
4
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning objectives
5
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Agenda
6
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Accessing this deck
7
This deck is online:
https://www.thegreenwebfoundation.org/events/djangocon-eu-2024/
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Before we start properly
8
If there was one thing you would like to learn today about sustainable web development
what would it be (5 mins)��
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
About the Green Web Foundation
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
The Green Web Foundation is working towards a fossil-free internet by 2030.
10
“
GREEN WEB FOUNDATION
Build open source tools, APIs, and libraries
11
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Create prototypes.
Contribute to open source projects.
Publish open data.
12
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Talks.
Workshops.
Training.
Consulting.
13
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How digital causes emissions, and why measure it
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How do digital services cause emissions?
GREEN WEB FOUNDATION
15
Source: https://developers.thegreenwebfoundation.org/co2js/overview/
Datacentre usage
End user device usage
Network transfer
Production and disposal
Powering and cooling servers
Powering core networks, mobile networks,
on-premise wifi and fixed routers
Powering end user devices - laptops, workstations, smartphones and tablets
Energy usage from making all of the above - processing raw silicon and other raw materials into integrated circuits, batteries, metal casings, etc
GREEN WEB FOUNDATION
How people estimate carbon emissions for digital
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
All models are wrong, some models are useful.
GEORGE BOX
Empirical Model-Building and Response Surfaces, 1987
17
“
GREEN WEB FOUNDATION
Usage based
18
Billing based
Estimate emissions based on observed usage of one or more parts of a system.
Estimate emissions based on observed spend. Turn billing into emissions data.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Usage based
19
Component view
System view
Global view
Billing based
“Native”
3rd party
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Usage based
20
Component view
System view
Global view
Instrument a single part of the system.��Self report own impact.
See impact of focussed changes.
Composable into bigger systems.
Instrument components in a system.
�Report at a system or service level.
Figure out where to focus efforts for things in production.
See impact of different configurations or usage scenarios
Use data from a single component to model your share of the global impact of digital
Very easy to start out with, but assumptions in models may not match your context.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Usage based
21
Component view
System view
Global view
Firefox profiler:
Javascript, browser reports own energy / carbon use. �
Code Carbon:
Python, often used for AI, can wrap existing python code
Scaphandre:
Rust, gives server-side per process figures
Green Metrics Tool:
Python, simulates common usage scenarios
Greenframe
Proprietary, javascript. Simulates common usage scenarios
KEPLER
Kubernetes focussed, uses production telemetry data, growing corporate support
Website Carbon
Javascript, proprietary
Ecograder
Javascript, open source
Sitespeed.io
Javascript, used by Wikipedia, has own grafana dashboards
WebpageTest Carbon Control
php + js, available as hosted service
An incomplete list:
(All use sustainable web model under the hood))
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Billing based
22
Native
Third party
Consumes billing data about service, region, quantity.
Direct access to hardware.
Applies proprietary conversions to turn purchased capacity into carbon dioxide equivalents.
Each provider has a different, incompatible proprietary methodology.
Largely consumes same billing data as native.��Less access to hardware.
Uses external model to enrich billing data with environmental figures.
More consistent across cloud providers (if supported)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Billing based
23
Native
Third party
Microsoft Emissions Impact Dashboard
Scopes 1, 2 and 3
AWS Customer Carbon Footprint Tool
Scopes 1, 2 (no scope 3)
Google Cloud Carbon Footprint
Scopes 1, 2 and 3
OVH
Scopes 1, 2 and 3, also water usage 👍
Cloud Carbon Footprint
Scopes 1,2 3 - first “significant” open source project
OpenCost
Recently merged PR now adds carbon costs to dashboards.
Green Pixie (proprietary)
Originally based on CCF, available as hosted service. Now offers richer data than just carbon.
(And more, each day)
An incomplete list:
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Usage based tools
24
Component view
System view
Global view
Firefox profiler
Javascript, reports measured browser energy
Code Carbon
Python, server side, often used for AI
Scaphandre
Rust, gives server-side per process figures
Green Metrics Tool
Python, simulates common usage scenarios
Greenframe
Proprietary, javascript simulates common usage scenarios
KEPLER
Kubernetes, uses production telemetry data for “real time” figures
Website Carbon
Javascript, proprietary
Ecograder
Javascript, open source
Sitespeed.io
Javascript, open source
Webpage Test Carbon Control
php + js, available as hosted service
(All use sustainable web design model under the hood))
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What these tools look like
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Who has used a website carbon calculator before?
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Website Carbon by Wholegrain Digital
Uses:
Sustainable Web Design with Green Web Dataset
27
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
WebPageTest by Catchpoint
Uses:
Sustainable Web Design through CO2.js and Green Web Dataset
28
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
WebPageTest Carbon Control
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Try it yourself:
GREEN WEB FOUNDATION
Running a test
Simple Configuration
31
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Running a test
Advanced Configuration
32
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Running a test
Advanced Configuration
4. Check the Run Carbon Control option.
5. Start Test!
33
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Find the
Carbon Control results section.
34
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Checking which domains used are hosted
green.
35
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Checking which domains used are hosted
green.
36
Click to expand!
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Estimated Carbon Footprint *
37
* For one page view. Assumes no caching.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Best practices.
You can test these changes using WebPageTest’s Pro Opportunities & Experiments feature.
38
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading test results
Breakdown of resource types.
39
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
For the nerds 🤓
Carbon Control results can be accessed via JSON.
40
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
For the nerds 🤓
Carbon Control results can be accessed via JSON.
41
Search for the carbon-footprint key.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
WebPageTest Resources
42
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Here’s one I made earlier…
Don’t to want to run the test? Here’s the link to results
https://bit.ly/wpt-djangocon-eu-2024
This should point to:
https://www.webpagetest.org/result/240606_AiDcY4_F1V/1/carboncontrol/
43
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
WebPageTest by Catchpoint
Uses:
Sustainable Web Design through CO2.js and Green Web Dataset
44
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Understanding how these work
Website carbon estimation tools are usage based, global view
45
Component view
System view
Global view
Billing based
“Native”
3rd party
Usage based
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Is just measuring transfer enough?
When deciding how we model impact we need be aware of the tradeoffs we are making when trying to come up with numbers we can use.
⚠️ This gets harder, very quickly, especially for a 50 minute workshop session. ⚠️
See this recent talk by my colleague Fershad Irani - The Nuance of Quantifying Digital Carbon Emissions
46
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Emerging Standards: Software Carbon Intensity
Think of “carbon emitted per activity”.
CO2e per page request
CO2e per CI run
CO2e per LLM query
Created by the Green Software Foundation - now a recognised ISO standard, with a freely available spec. Developed in the open on GitHub.
47
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
48
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Green Coding AI from Green Coding Solutions GmbH
You measure more than just web pages. Does model choice make a difference with AI?
Can we use greener AI models instead?
49
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What questions green coding can help us answer
Asking a well known LLM about something we care about
50
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Optimising for carbon
Trying with a smaller model.
7x lower resource usage!
Was the other answer really 7x better?
51
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Green Metrics Tool Full stack measurements
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Green Metrics Tool from Green Coding Solutions GmbH
Imaginatively titled open source green metrics tooling. Python
53
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Green Metrics Tool (GMT) - what is it?
GMT is a usage based, system view tool, that simulates common interactions in a test system
54
Component view
System view
Global view
Billing based
“Native”
3rd party
Usage based
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How GMT works at very high level
Uses docker to set up a system, and record metrics from each container for various tasks
55
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What can you do with GMT?
Try different usage scenarios for your service - static vs dynamic, different page designs, etc.
56
https://www.green-coding.io/case-studies/wordpress-vs-hugo-cloudflare/
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Applying this to django - what you need
If you have a dockerised app, you’re halfway there to trying it out.
If you have any integration tests that drive a browser, you have a head start on usage scenarios too
57
Django app
Postgres
Browser
docker-compose.yaml
Redis
usage_scenario.yaml
+
scripts to drive the browser
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning from a real example - Wagtail and the bakery
Last year on the Wagtail GSoC project: used GMT to test common scenarios on the bakery demo site
58
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning from a real example - entire lifecycle
We can then measure the impact of running these common scenarios in the GMT’s dashboard.
SCI here is Software Carbon Intensity - CO2 per page request
59
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning from a real example - putting the SCI in CI
When run on a schedule, we can see how this changes over time, and aim to improve it.
60
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Trying Green Metric Tool ourselves
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What we’ll need to do
62
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Trying it yourself - fork the pre-prepared repo
Organisation:�
thegreenwebfoundation��Project:�
bakery-gold-benchmark
Then follow the readme steps.
Docker required.
63
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Set up your copy the Wagtail bakery demo, using docker compose.
64
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
See the end to end tests
65
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Run the puppeteer scenarios
You should see see them driving a full browser to interact with the rest of the system spun up with docker compose
66
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Notify the hosted GMT service to run the usage scenarios in your repo on specialised hardware.
67
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What we send to get notified
Your job is queued up - so add an email to send a notification to when results are ready
68
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What the notification looks like
Takes a few minutes.
69
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How to read the results
The steps we defined and ran in end-to-end scenarios should be visible.
70
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Seeing a software carbon intensity score
Our CO2 per page request score comes from an average of all the pages visited in all our scenarios.
71
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Seeing a software carbon intensity score over time
Wagtail’s bakery demo, taken every day over the last few weeks.
72
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning objectives (recap)
73
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Thanks! I’ll be at the sprints if you want more
Please take 5 mins to provide the feedback form over lunch.
www.linkedin.com/in/mrchrisadams
mastodon.social/@mrchrisadams�
climateAction.tech - a supportive climate-tech community
podcast.greensoftware.foundation - interviews of experts in this field�branch.climateAction.tech - online magazine about climate/tech�
74
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
If you want to run GMT yourself
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
You CAN self host Green Metrics Tool.
⚠️ You need hardware access, and time, and expertise for an accurate setup. ⚠️
BUT
You can run a version of GMT in the cloud if you just want to check file syntax, see containers are running as expected, and metrics are being collected.
It won’t have hardware access, so some figures will be modelled or look different.
76
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Trying it yourself in GitHub codespace
Visit green metrics tool repo on GitHub
You’ll set up a github codespace to run GMT.
You’ll then import your project for a measurement run.
77
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How to spin up a codespace
78
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Choosing the correct codespace for this workshop
79
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Follow steps into fetch a public repo
80
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Seeing metrics in your codespace
81
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What It should look like
You will have a running instance of GMT, and your project should be imported to peruse.
⚠️ Don’t forget to turn off the codespace at the end!
82
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Thanks! I’ll be at the sprints if you want more
Please take 5 mins to provide the feedback form over lunch.
www.linkedin.com/in/mrchrisadams
mastodon.social/@mrchrisadams�
climateAction.tech - a supportive climate-tech community
podcast.greensoftware.foundation - interviews of experts in this field�branch.climateAction.tech - online magazine about climate/tech�
83
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Extra bonus bits
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Measuring in Firefox Profiler
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
The best way to avoid ambiguity in carbon estimates is to measure the actual energy used.
GREEN WEB FOUNDATION
Capturing a profile
The following video shows how to capture a profile using the Firefox Browser.
Afterwards, we’ll walk through the steps of capturing and reading a profile.
87
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Capturing a profile
88
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Capturing a profile
Download the latest Firefox Browser
89
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Capturing a profile
90
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Capturing a profile
Change the setting to Power.
91
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Power profiles will look different depending on the CPU you are running.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Differing Power Profiles
93
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
For the sake of time, we will use a pre-recorded profile.
https://share.firefox.dev/3QYQnCg
Profile captured on Apple M2 Mac.
94
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
Parent process
Core Firefox functionality (handling UI, navigation, profiles etc.)
95
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
Child process
Handles the content of the tab (loading content of the tab, including JavaScript)
96
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
View power & CO2e
Hover over the Power Process track.
97
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
View power & CO2e
Hover over the Power Process track.
98
Power consumption at that time(Watts)
Energy (milliwatt-hours) for the entire section
CO2e (milligrams)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
CO2e Calculation
Energy used (mWh) x
GLOBAL Average Grid Intensity (g/kWh)
99
CO2e (milligrams)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
Change the grid intensity
100
window.profile.meta.gramsOfCO2ePerKWh = 1000
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Reading a profile
Change the grid intensity
Now all CO2e calculations will use 1000g kWh
101
CO2e (milligrams)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Firefox Profiler Resources
102
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
An intro to CO2.js
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
An open-source JavaScript library that enables developers to estimate the emissions related to use of their apps, websites, and software.
“
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Who’s using CO2.js today?
105
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What can you do with CO2.js?
106
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Data in CO2.js
Country-level grid intensity
Green Web Dataset
107
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Try it yourself:
https://observablehq.com/@greenweb/co2-js-playground
GREEN WEB FOUNDATION
How do they use it?
109
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
How do they use it?
110
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
CO2.js Resources
111
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Want more? Drop us a line.
112
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION