﻿ Python Program to Track and Analyze Software Metrics

# Track and Analyze Software Metrics with Python

Write a Python program to implement a system for tracking and analyzing software metrics.

Implementing a system for tracking and analyzing software metrics involves creating a Python application that can record various metrics such as CPU usage, memory usage, and more. The system should store these metrics, allow for adding new data, and perform analyses to calculate averages, and find minimum and maximum values. Additionally, it should generate reports summarizing the metrics for easy interpretation and decision-making. This solution would be particularly useful for monitoring software performance and identifying areas for optimization.

Sample Solution:

Python Code :

``````# Import necessary libraries
import statistics

# Define the SoftwareMetrics class to handle metric operations
class SoftwareMetrics:
def __init__(self):
self.metrics = {}

if metric_name not in self.metrics:
self.metrics[metric_name] = []
self.metrics[metric_name].append(value)

def calculate_average(self, metric_name):
if metric_name in self.metrics:
return statistics.mean(self.metrics[metric_name])
else:
return None

def find_min_max(self, metric_name):
if metric_name in self.metrics:
return min(self.metrics[metric_name]), max(self.metrics[metric_name])
else:
return None, None

def generate_report(self):
report = []
for metric_name, values in self.metrics.items():
avg = self.calculate_average(metric_name)
min_val, max_val = self.find_min_max(metric_name)
report.append({
'metric_name': metric_name,
'average': avg,
'min': min_val,
'max': max_val
})
return report

# Define the main function to interact with the user
def main():
metrics = SoftwareMetrics()

while True:
print("\nSoftware Metrics Tracking System")
print("2. Generate Report")
print("3. Exit")

choice = input("Enter your choice: ")
if choice == '1':
metric_name = input("Enter metric name: ")
try:
value = float(input("Enter metric value: "))
print(f"Added value {value} to metric {metric_name}.")
except ValueError:
print("Invalid value. Please enter a numeric value.")
elif choice == '2':
report = metrics.generate_report()
for metric in report:
print(f"\nMetric: {metric['metric_name']}")
print(f"  Average: {metric['average']}")
print(f"  Min: {metric['min']}")
print(f"  Max: {metric['max']}")
elif choice == '3':
print("Exiting the system.")
break
else:

# Entry point for the script
if __name__ == "__main__":
main()
``````

Output:

```(base) C:\Users\ME>python untitled1.py

Software Metrics Tracking System
2. Generate Report
3. Exit
Enter metric name: CPU_Usage
Enter metric value: 65
Added value 65.0 to metric CPU_Usage.

Software Metrics Tracking System
2. Generate Report
3. Exit
Enter metric name: CPU_Usage
Enter metric value: 60
Added value 60.0 to metric CPU_Usage.

Software Metrics Tracking System
2. Generate Report
3. Exit
Enter metric name: Memory_Usage
Enter metric value: 70
Added value 70.0 to metric Memory_Usage.

Software Metrics Tracking System
2. Generate Report
3. Exit
Enter metric name: Memory_Usage
Enter metric value: 73
Added value 73.0 to metric Memory_Usage.

Software Metrics Tracking System
2. Generate Report
3. Exit

Metric: CPU_Usage
Average: 62.5
Min: 60.0
Max: 65.0

Metric: Memory_Usage
Average: 71.5
Min: 70.0
Max: 73.0

Software Metrics Tracking System
2. Generate Report
3. Exit
Exiting the system.

(base) C:\Users\ME>
```

Explanation:

• SoftwareMetrics class:
• Initializes with an empty dictionary to store metrics.
• add_metric method adds a metric value to the dictionary under its name.
• calculate_average method calculates the average value for a metric.
• find_min_max method finds the minimum and maximum values for a metric.
• generate_report method generates a report with average, minimum, and maximum values for all metrics.
• main function:
• Initializes a 'SoftwareMetrics' instance.
• Displays a menu for users to choose options: Add Metric, Generate Report, or Exit.
• Adds a metric when the user selects "Add Metric", prompting for name and value.
• Generates and displays a report when the user selects "Generate Report".
• Exits the program when the user selects "Exit".

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿