Skip to content

Commit d243f8b

Browse files
committed
docs(getting-started): Refactor Preety_print + change to start
1 parent 8455db5 commit d243f8b

File tree

3 files changed

+82
-59
lines changed

3 files changed

+82
-59
lines changed

docs/script/getting_started/pretty_print.py

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,76 +31,81 @@
3131
import os
3232

3333

34-
def start_pretty_print():
34+
def load_data(directory='./csv'):
3535
"""
36-
Pretty print the result of the demo by parsing the csv files,
37-
then proceed to calculate the average, maximum, and minimum consumption
38-
and print them in a table format
36+
Load CSV files from the specified directory and return the data as a list of dictionaries.
3937
"""
4038
data = []
41-
result = [["Cgroup",
42-
"Average consumption",
43-
"Maximum consumption",
44-
"Minimum consumption"]]
45-
46-
print("\nThe consumptions are given in Watt, "
47-
"note that the precision depend on the value given in the "
48-
"configuration file (the base CPU frequency, the CPU TDP, ...) \n")
49-
50-
# Get all the csv power report in the csv directory
51-
for root, _, files in os.walk('./csv'):
39+
for root, _, files in os.walk(directory):
5240
for filename in files:
5341
if filename.endswith('.csv'):
5442
file_path = os.path.join(root, filename)
55-
5643
with open(file_path, mode='r', newline='', encoding='UTF-8') as f:
57-
for row in csv.DictReader(f):
58-
data.append(row)
44+
data.extend(csv.DictReader(f))
45+
return data
46+
5947

60-
cgroup_data = {}
61-
total = [0, 0, 0]
62-
# We remove rapl, but it's still available in the csv files
48+
def calculate_statistics(data, scope):
49+
"""
50+
Calculate average, maximum, and minimum consumption for the given scope (cpu or dram).
51+
"""
52+
stats = {}
6353
for row in data:
64-
target = row['target']
65-
consumption = float(row['power'])
54+
if row['scope'] == scope and row['target'] != 'rapl':
55+
target = row['target']
56+
consumption = float(row['power'])
57+
stats.setdefault(target, []).append(consumption)
6658

67-
if target not in cgroup_data and not target == 'rapl':
68-
cgroup_data[target] = []
59+
return {
60+
target: {
61+
'avg': sum(consumptions) / len(consumptions),
62+
'max': max(consumptions),
63+
'min': min(consumptions)
64+
} for target, consumptions in stats.items()
65+
}
6966

70-
if not target == 'rapl':
71-
cgroup_data[target].append(consumption)
7267

73-
for target, consumptions in cgroup_data.items():
74-
avg_consumption = sum(consumptions) / len(consumptions)
75-
max_consumption = max(consumptions)
76-
min_consumption = min(consumptions)
68+
def print_statistics(stats, title):
69+
"""
70+
Print statistics (average, max, min) for the given data in a formatted table.
71+
"""
72+
if not stats:
73+
return
74+
75+
print(f"\n{title}\n")
76+
print(f"{'Target':<20} {'Average consumption':<20} {'Maximum consumption':<20} {'Minimum consumption':<20}")
77+
print("=" * 80)
7778

78-
if not target == 'global':
79-
result.append([target,
80-
f"{avg_consumption:.2f}",
81-
f"{max_consumption:.2f}",
82-
f"{min_consumption:.2f}"])
79+
total = {'avg': 0, 'max': 0, 'min': 0}
80+
for target, values in stats.items():
81+
if target != 'global':
82+
print(f"{target:<20} {values['avg']:<20.2f} {values['max']:<20.2f} {values['min']:<20.2f}")
8383
else:
84-
total = [avg_consumption, max_consumption, min_consumption]
84+
total = values
8585

86-
print(f"{'Target':<20} "
87-
f"{'Average consumption':<20} "
88-
f"{'Maximum consumption':<20} "
89-
f"{'Minimum consumption':<20}")
90-
print("=" * 80)
86+
print("-" * 80)
87+
print(f"{'Global':<20} {total['avg']:<20.2f} {total['max']:<20.2f} {total['min']:<20.2f}")
9188

92-
for row in result[1:]:
93-
print(f"{row[0]:<20} {row[1]:<20} {row[2]:<20} {row[3]:<20}")
9489

95-
print("=" * 80)
96-
print(f"{'Global':<20} "
97-
f"{total[0]:<20.2f} "
98-
f"{total[1]:<20.2f} "
99-
f"{total[2]:<20.2f}")
100-
101-
print("\nIf you want to get a more precise evaluation, "
102-
"we encourage you to read the documentation of PowerAPI "
103-
"and adapt the sensor/formula configuration file in consequence \n")
90+
def start_pretty_print():
91+
"""
92+
Pretty print the CPU and DRAM power consumption statistics from CSV files.
93+
"""
94+
print("The consumptions are given in Watt, note that the precision depends on the configuration file\n")
95+
96+
data = load_data()
97+
98+
# Calculate and print CPU statistics
99+
cpu_stats = calculate_statistics(data, 'cpu')
100+
print_statistics(cpu_stats, "CPU Consumption Statistics :")
101+
102+
# Calculate and print DRAM statistics
103+
dram_stats = calculate_statistics(data, 'dram')
104+
print_statistics(dram_stats, "DRAM Consumption Statistics :")
105+
106+
# Could add the GPU statistics here
107+
108+
print("\nFor more precise evaluation, consult the PowerAPI documentation to adjust configurations.\n")
104109

105110

106111
if __name__ == '__main__':

docs/script/getting_started/start.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29-
29+
import os
3030
import sys
3131
from subprocess import call
3232
import subprocess
@@ -55,6 +55,7 @@ def start_demo():
5555
"\n" + list_arch[3] +
5656
"\n")
5757

58+
val = ""
5859
choice = True
5960
while choice:
6061
try:
@@ -123,13 +124,30 @@ def start_demo():
123124
json.dump(data, f, indent=4)
124125

125126
print("Starting the demo...")
127+
print("The demo will run for approximately 2 minutes\n")
126128

127129
call("./start.sh")
128130

129-
print("The demo has ended, "
130-
"you can see the result under the /csv directory"
131-
" or use 'python3 pretty_print.py' "
132-
"to get a quick summary of the result in the terminal")
131+
verification = 0
132+
133+
# Get all the csv power report in the csv directory
134+
for root, _, files in os.walk('./csv'):
135+
for filename in files:
136+
if filename.endswith('.csv'):
137+
verification += 1
138+
file_path = os.path.join(root, filename)
139+
print("The power report is available at: " + file_path)
140+
141+
if verification == 0:
142+
print("\nNo power report available, "
143+
"please check the configuration file "
144+
"and the sensor availability for your "
145+
"processor architecture\n")
146+
else:
147+
print("\nThe demo has ended, "
148+
"you can see the result under the /csv directory"
149+
" or use 'python3 pretty_print.py' "
150+
"to get a quick summary of the result in the terminal\n")
133151

134152

135153
if __name__ == '__main__':

docs/script/getting_started/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ docker compose logs sensor -f &
66

77
docker compose logs formula -f &
88

9-
sleep 60
9+
sleep 120
1010

1111
docker compose down

0 commit comments

Comments
 (0)