#Interactive Session by Hina Sharma and Mamatha Venkatesh, "Secret Sauce for Sustainable Test Automation Framework"at #ATAGTR2023.
#ATAGTR2023 was the 8th Edition of Global Testing Retreat.
To know more about #ATAGTR2023, please visit: https://gtr.agiletestingalliance.org/
The Codex of Business Writing Software for Real-World Solutions 2.pptx
#Interactive Session by Hina Sharma and Mamatha Venkatesh, "Secret Sauce for Sustainable Test Automation Framework" at #ATAGTR2023.
1. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
S E C R E T S A U C E F O R S U S T A I N A B L E T E S T
A U T O M A T I O N F R A M E W O R K
Hina Sharma
Mamatha Venkatesh
Global Testing Retreat
#ATAGTR2023
2. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
A G E N D A
• What is Sustainability?
• How do we measure sustainability?
• Importance for green automation
framework
• Challenges with unsustainable
automation framework
• Analysis of test automation
framework
• Calculating carbon emissions
• Efficient coding practices
3. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
W H AT I S S U S TA I N A B I L I T Y ?
• Introduce Sustainability
• why Sustainability now?
• Which areas get impacted
due to unsustainable
environment
6. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
7. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Why
Now
?
Environmental
challenges
Create business risks
across the enterprise
Regulatory
Rising pressure to meet
evolving ESG goals and
solve social problems
Waste & emissions
20% of every dollar spent in manufacturing is wasted.
80% emissions coming from supply chain
7
Business performance
Strong performance on ESG issues
can improve top-line growth
Economic impact
$8T 2030. The cost of
climate policy for the
market to absorb
8. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
S T E P S T O W A R D S D E C A R B O N I Z AT I O N ! !
9. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
I T C A N R E D U C E E N E R G Y A N D
E N V I R O N M E N T A L I M P A C T B Y
O P T I M I Z I N G D A T A C E N T E R
A N D P U B L I C C L O U D
R E S O U R C E S .
9
20%
On-prem data centers
typically operate at
20-40% utilization.2
50%
Cloud users are
typically
overprovisioned by
30—50%.2
70%
Electricity accounts for as much as 70% of total data center
operating costs. 1
With rising energy costs, organizations are focused on
reducing energy usage.
Sustainability in an IT
operations context
10. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
IT Transformation: Taking steps to reduce the environmental
Carbon Emission on the platform
f(# of servers, total energy required to power
server, carbon intensity of energy sources)
fewer servers lesser power
Compute Storage
Design Principles , Green
Coding , Testing
Carbon
Emission of
the
workloads
Network
Sustainability
of
the
platform
Sustainability
of
the
workloads
Apply Green
Coding/testing principles
to Applications
11. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
G lobal Testing Retreat
#ATAGTR2023
GREEN CODING
Green coding is a coding
practice that aims to solve a
problem, while taking into
account the goal of reducing
energy consumption.
It’s a way of writing the code
that considers the energy
consumption and attempts to
reduce the same through
optimizing the resource
demand on physical servers &
systems.
Compute Network Storage
• Choice of programming language
• Language best practices
• Choice of algorithm
• Architecture Principles
• Design Patterns for Saving Energy
• Tools
• Green software engineering
principles
• Best practices
12. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Automation
Framework
13. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
A U T O M AT I O N
E X E C U T I O N
Framework
Layer
Automation Test Suite
Get
Environmen
t
Connec
t to test
suite
Common
libraries
Reporting Lab
Default
Data
json
AUT
Base
Class
Test
data
json
Constant
s and
urls
Automati
on Test
Scripts
Initializ
e Class
Get
executio
n data
Us
er
User
14. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
C H A L L E N G E S W I T H U N - S U S T A I N A B L E F R A M E W O R K
Unsustainable Code
Unsustainable code refers to software that is inefficient, resource-intensive, or
poorly optimized, leading to negative environmental impacts.
Automation infrastructure or set of tools or code that is inefficient, difficult to
maintain, or fails to adapt to changing requirements.
Unsustainable Automation Framework
15. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
G R E E N A U T O M AT I O N F R A M E W O R K
A "green automation framework" refers to an environmentally sustainable and
efficient automation framework used in software development and testing.
A green automation framework helps
- write energy efficient code
- reduces resource usage
- write maintainable code
16. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
A N A LY S I S O F A T E S T F R A M E W O R K
We checked an automation framework for :
• Code sustainability
• Energy consumption
• Where we stand w.r.t the carbon emissions
17. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
C A R B O N E M I S S I O N S – A L L A L O N G
T H E W AY …
AUT DataCenter Database
Automation
Code
Storage
Compute
Nodes
Peripherals
CO2 CO2 CO2
CO2
CO2
CO2
CO2
Local Environment Product/Data Center Environment
CO2
18. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
• To calculate the energy consumption by Automation code, we have used
Python packages
CodeCarbon
• Installation:
pip install codecarbon
CO₂=Power_consumption(kilowatt-hours)*Carbon_Intensity(kg of CO₂/kilowatt-
hour)
C A L C U L AT I N G E N E R G Y
E M I S S I O N S ?
19. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
C A L C U L AT I N G C A R B O N E M I S S I O N S
• Carbon Footprint
• Energy Consumption / Power Consumption (kilowatt-hours)
• Carbon Intensity(kg of CO₂/kilowatt-hour)
20. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N O U T P U T
The below out is generated on running the code using
codecarbon
21. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Add CodeCarbon statements to the existing code:
22. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
C O D E E X P E R I M E N T
23. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
import requests
import time
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
def fetch_user_data(user_id):
# Simulate making an API call to JSONPlaceholder (replace with your actual API endpoint)
api_endpoint = f"https://jsonplaceholder.typicode.com/users/{user_id}"
response = requests.get(api_endpoint)
time.sleep(1) # Simulate some processing time
return response.json()
# List of user IDs to fetch
user_ids = [1, 2, 3]
# Repeat the API calls in a loop for 10,000 times
for _ in range(10000):
for user_id in user_ids:
user_data = fetch_user_data(user_id)
print(f"User data for user ID {user_id}: {user_data}")
# Make API calls using a loop
for user_id in user_ids:
user_data = fetch_user_data(user_id)
print(f"User data for user ID {user_id}: {user_data}")
tracker.stop()
L O O P S I N T H E C O D E :
Print user data for 3 ids using API calls
24. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
25. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
import aiohttp
import asyncio
import time
async def fetch_user_data(session, user_id):
# Simulate making an API call to JSONPlaceholder (replace with your actual API endpoint)
api_endpoint = f"https://jsonplaceholder.typicode.com/users/{user_id}"
async with session.get(api_endpoint) as response:
return await response.json()
async def main():
user_ids = [1, 2, 3]
api_calls = 10000
async with aiohttp.ClientSession() as session:
tasks = [fetch_user_data(session, user_id) for _ in range(api_calls) for user_id in user_ids]
user_data_list = await asyncio.gather(*tasks)
# Print or process the collected user data as needed
for user_data in user_data_list:
print(f"User data: {user_data}")
start_time = time.time()
asyncio.run(main())
end_time = time.time()
print(f"Total execution time: {end_time - start_time} seconds")
R E D U N D A N T A P I C A L L S I N C O D E :
26. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
27. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
O P T I M I Z E D A P I C A L L S
import requests
import time
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
def fetch_user_data(user_id):
api_endpoint = f"https://jsonplaceholder.typicode.com/users/{user_id}"
response = requests.get(api_endpoint)
time.sleep(1) # Simulate some processing time
return response.json()
# Make unnecessary API calls in a loop for different user IDs
for user_id in range(1, 11):
user_data = fetch_user_data(user_id)
print(f"User data for user ID {user_id}: {user_data}")
tracker.stop()
28. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
29. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
import requests
import time
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
def fetch_user_data(user_id):
api_endpoint =
f"https://jsonplaceholder.typicode.com/users/{user_id}"
response = requests.get(api_endpoint)
time.sleep(1) # Simulate some processing time
return response.json()
# Keep track of fetched user IDs to avoid redundant API calls
fetched_user_ids = set()
# Make API calls only for unique user IDs
for user_id in range(1, 11):
if user_id not in fetched_user_ids:
user_data = fetch_user_data(user_id)
print(f"User data for user ID {user_id}: {user_data}")
fetched_user_ids.add(user_id)
tracker.stop()
U N U S E D O B J E C T S I N T H E C O D E
30. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
31. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
U N U S E D
O B J E C T
S
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
import random
class ResourceIntensiveObject:
def __init__(self, identifier):
self.identifier = identifier
# Simulate resource-intensive initialization
self.data = [random.random() for _ in range(10**6)] # A list of 1 million random
numbers
def perform_action(self):
# Simulate some action on the object
return sum(self.data)
# Create a list of 5000 ResourceIntensiveObject instances
objects_list = [ResourceIntensiveObject(i) for i in range(5000)]
# Simulate some operations that only use a small subset of the created objects
used_objects = [obj for obj in objects_list if obj.identifier % 100 == 0]
# Perform actions on the used objects
for obj in used_objects:
result = obj.perform_action()
print(f"Action performed on object with identifier {obj.identifier}. Result: {result}")
tracker.stop()
32. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
33. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
import random
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
class ResourceIntensiveObject:
def __init__(self, identifier):
self.identifier = identifier
# Simulate resource-intensive initialization
self.data = [random.random() for _ in range(10**6)] # A list of 1 million random numbers
def perform_action(self):
# Simulate some action on the object
return sum(self.data)
# Create a list of 5000 ResourceIntensiveObject instances
objects_list = [ResourceIntensiveObject(i) for i in range(5000)]
# Simulate some operations that only use a small subset of the created objects
used_objects = [obj for obj in objects_list if obj.identifier % 100 == 0]
# Perform actions on the used objects
for obj in used_objects:
result = obj.perform_action()
print(f"Action performed on object with identifier {obj.identifier}. Result: {result}")
tracker.stop()
O P T I M I Z E D
C O D E W I T H
R E L E VA N T
O B J E C T S
34. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
E N E R G Y C O N S U M P T I O N D ATA
35. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
O B S E R VAT I O N S
Code Inefficient code –
Energy
consumption
Optimized code -
Energy
consumption
% decrease
after
optimization
Loops in the
code
0.002838 kwh 0.000735 kwh - 74.18%
API Calls 0.000082 kwh 0.000077 kwh - 6.09 %
Unused
objects
0.001098 kwh 0.00946 kwh - 13.8 %
- Energy consumption decreased by significant amount after
optimizing the code.
36. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
• Using inefficient algorithms and loops
• Creating unnecessary objects
• Not using caching
• Not using compression
• Not optimizing the use of hardware resources
C O D I N G M A L P R A C T I C E S
37. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
A C C E P TA B L E C A R B O N E M I S S I O N S
0 emissions is ideal!
- There are no standards for software applications. The carbon emissions are calculated
based on overall end to end execution.
- Optimize your code to get the best possible efficiency
What you choose to measure will impact what you will optimise, and because of the
assumptions that need to be made when summarising, multiple metrics are better than one.
38. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
O U R A N A LY S I S
• Multiple hits to the server
• Multiple connections to the database to fetch the queries
• Lower polling times to fetch the status of deployments
• Deprecated methods still being used
Below were few reasons we have analyzed to as the reasons for high carbon
emissions
39. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Design phase
Review phase
Execution phase
• - Defining requirements
• - Designing architecture
• - Planning for framework
• - Test suite/case planning
• - Review from energy consumption
perspective
• - Review from execution time
perspective
• - Review from resource perspective
• - Execution of code with energy
consumption in mind
• - Improve code for optimization
W H E R E D O E S I T S TA R T … .
40. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
R E D U C T I O N O P P O R T U N I T I E S
In addition to software maintenance, few other things that you can do to make your
automation framework more energy sustainable and green:
• The right coding language : If possible, choose for the greenest
language for coding your framework.
• Optimum use of resources: Ensure you are reserving/using what you need.
includes your virtual machines, compute nodes etc.
• Code optimization : Review your code in regular intervals. Find the culprits
loops, multiple hits to server, unused objects and keep cleaning it.
• Shift Left : Bugs cost heavily. The more you shift left, the less rounds of execution
thereby contribution to a greener environment.
• Cloud computing: Cloud computing can be a good way to reduce the energy
consumption of your automation framework. This is because cloud computing
providers typically use more energy-efficient hardware and software than on-
premises data centers.
41. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Source: https://thenewstack.io
Normalized Global Results for Energy,
Time and Memory
E N E R G Y E F F I C I E N C Y O F P R O G R A M I N G
L A N G U A G E S
42. G lobal Testing Retreat
#ATAGTR2023
2, 3 & 8, 9 December 2023
Q&A