|
| 1 | +import io |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import types |
| 6 | +import unittest |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | + |
| 10 | +def _install_reportlab_stubs(): |
| 11 | + """Provides minimal reportlab replacements so report.py can be imported.""" |
| 12 | + reportlab_module = types.ModuleType("reportlab") |
| 13 | + lib_module = types.ModuleType("reportlab.lib") |
| 14 | + reportlab_module.lib = lib_module |
| 15 | + |
| 16 | + colors_module = types.ModuleType("reportlab.lib.colors") |
| 17 | + colors_module.HexColor = lambda value: value |
| 18 | + lib_module.colors = colors_module |
| 19 | + |
| 20 | + pagesizes_module = types.ModuleType("reportlab.lib.pagesizes") |
| 21 | + pagesizes_module.letter = ("letter", "letter") |
| 22 | + lib_module.pagesizes = pagesizes_module |
| 23 | + |
| 24 | + units_module = types.ModuleType("reportlab.lib.units") |
| 25 | + units_module.inch = 1 |
| 26 | + lib_module.units = units_module |
| 27 | + |
| 28 | + styles_module = types.ModuleType("reportlab.lib.styles") |
| 29 | + |
| 30 | + def getSampleStyleSheet(): |
| 31 | + return {"Heading1": object(), "Heading2": object(), "Normal": object()} |
| 32 | + |
| 33 | + class ParagraphStyle: |
| 34 | + def __init__(self, *args, **kwargs): |
| 35 | + self.args = args |
| 36 | + self.kwargs = kwargs |
| 37 | + |
| 38 | + styles_module.getSampleStyleSheet = getSampleStyleSheet |
| 39 | + styles_module.ParagraphStyle = ParagraphStyle |
| 40 | + lib_module.styles = styles_module |
| 41 | + |
| 42 | + platypus_module = types.ModuleType("reportlab.platypus") |
| 43 | + |
| 44 | + class SimpleDocTemplate: |
| 45 | + def __init__(self, *args, **kwargs): |
| 46 | + self.args = args |
| 47 | + self.kwargs = kwargs |
| 48 | + |
| 49 | + def build(self, story): |
| 50 | + self.story = story |
| 51 | + |
| 52 | + class Paragraph: |
| 53 | + def __init__(self, text, style): |
| 54 | + self.text = text |
| 55 | + self.style = style |
| 56 | + |
| 57 | + class Spacer: |
| 58 | + def __init__(self, width, height): |
| 59 | + self.width = width |
| 60 | + self.height = height |
| 61 | + |
| 62 | + class Table: |
| 63 | + def __init__(self, data, colWidths=None, rowHeights=None): |
| 64 | + self.data = data |
| 65 | + self.colWidths = colWidths |
| 66 | + self.rowHeights = rowHeights |
| 67 | + |
| 68 | + def setStyle(self, style): |
| 69 | + self.style = style |
| 70 | + |
| 71 | + class TableStyle: |
| 72 | + def __init__(self, instructions): |
| 73 | + self.instructions = instructions |
| 74 | + |
| 75 | + class Image: |
| 76 | + def __init__(self, data, width=None, height=None): |
| 77 | + self.data = data |
| 78 | + self.width = width |
| 79 | + self.height = height |
| 80 | + |
| 81 | + platypus_module.SimpleDocTemplate = SimpleDocTemplate |
| 82 | + platypus_module.Paragraph = Paragraph |
| 83 | + platypus_module.Spacer = Spacer |
| 84 | + platypus_module.Table = Table |
| 85 | + platypus_module.TableStyle = TableStyle |
| 86 | + platypus_module.Image = Image |
| 87 | + reportlab_module.platypus = platypus_module |
| 88 | + |
| 89 | + module_map = { |
| 90 | + "reportlab": reportlab_module, |
| 91 | + "reportlab.lib": lib_module, |
| 92 | + "reportlab.lib.colors": colors_module, |
| 93 | + "reportlab.lib.pagesizes": pagesizes_module, |
| 94 | + "reportlab.lib.styles": styles_module, |
| 95 | + "reportlab.lib.units": units_module, |
| 96 | + "reportlab.platypus": platypus_module, |
| 97 | + } |
| 98 | + |
| 99 | + for name, module in module_map.items(): |
| 100 | + sys.modules.setdefault(name, module) |
| 101 | + |
| 102 | + |
| 103 | +try: |
| 104 | + from carbontracker import report |
| 105 | +except ModuleNotFoundError as exc: |
| 106 | + if exc.name and exc.name.startswith("reportlab"): |
| 107 | + _install_reportlab_stubs() |
| 108 | + from carbontracker import report |
| 109 | + else: |
| 110 | + raise |
| 111 | + |
| 112 | + |
| 113 | +SAMPLE_LOG = """\ |
| 114 | +2025-11-18 15:53:54 - carbontracker version 2.3.4.dev1+g61759d185.d20251107 |
| 115 | +2025-11-18 15:53:54 - Only predicted and actual consumptions are multiplied by a PUE coefficient of 1.58 (Daniel Bizo, 2023, Uptime Institute Global Data Center Survey). |
| 116 | +2025-11-18 15:53:54 - The following components were found: GPU with device(s) GPU, ANE. CPU with device(s) CPU. |
| 117 | +2025-11-18 15:53:54 - Monitoring thread started. |
| 118 | +2025-11-18 15:54:28 - Epoch 1: |
| 119 | +2025-11-18 15:54:28 - Duration: 0:00:33.57 |
| 120 | +2025-11-18 15:54:28 - Average power usage (W) for gpu: 0.019533333333333337 |
| 121 | +2025-11-18 15:54:28 - Average power usage (W) for cpu: 6.785066666666666 |
| 122 | +2025-11-18 15:54:28 - Carbon intensities (gCO2eq/kWh) fetched every 900 s at detected location: Copenhagen, Capital Region, DK: [143.3] |
| 123 | +2025-11-18 15:54:28 - Average carbon intensity during training was 143.30 gCO2eq/kWh. |
| 124 | +2025-11-18 15:54:28 - Monitoring thread ended. |
| 125 | +""" |
| 126 | + |
| 127 | + |
| 128 | +class TestReportModule(unittest.TestCase): |
| 129 | + def setUp(self): |
| 130 | + self.parser = report.LogParser(SAMPLE_LOG) |
| 131 | + |
| 132 | + def test_format_duration_produces_readable_value(self): |
| 133 | + self.assertEqual(report.format_duration(3661), "1h 1min 1s") |
| 134 | + self.assertEqual(report.format_duration(59), "59s") |
| 135 | + |
| 136 | + def test_log_parser_extracts_metadata_and_epochs(self): |
| 137 | + parser = self.parser |
| 138 | + self.assertEqual(parser.version, "2.3.4.") |
| 139 | + self.assertEqual(parser.pue, 1.58) |
| 140 | + self.assertIsNone(parser.location) |
| 141 | + self.assertEqual(parser.start_time, "2025-11-18 15:53:54") |
| 142 | + self.assertEqual(parser.end_time, "2025-11-18 15:54:28") |
| 143 | + self.assertEqual(len(parser.epochs), 1) |
| 144 | + self.assertAlmostEqual(parser.epochs[0]["duration"], 33.57) |
| 145 | + self.assertAlmostEqual(parser.epochs[0]["gpu_power"], 0.019533333333333337) |
| 146 | + self.assertAlmostEqual(parser.epochs[0]["cpu_power"], 6.785066666666666) |
| 147 | + self.assertAlmostEqual(parser.epochs[0]["total_power"], 6.8046) |
| 148 | + |
| 149 | + def test_calculate_energy_metrics_matches_expected_numbers(self): |
| 150 | + metrics = self.parser.calculate_energy_metrics() |
| 151 | + self.assertAlmostEqual(metrics["total_duration"], 33.57) |
| 152 | + self.assertAlmostEqual(metrics["avg_gpu_power"], 0.019533333333333337) |
| 153 | + self.assertAlmostEqual(metrics["avg_cpu_power"], 6.785066666666666) |
| 154 | + self.assertAlmostEqual(metrics["total_power"], 6.8046) |
| 155 | + self.assertAlmostEqual(metrics["energy_kwh"], 6.345289499999999e-05, places=12) |
| 156 | + self.assertAlmostEqual(metrics["co2_kg"], 9.092799853499999e-06, places=12) |
| 157 | + |
| 158 | + def test_generate_plots_returns_png_buffer(self): |
| 159 | + plots = self.parser.generate_plots() |
| 160 | + self.assertIn("combined_plots", plots) |
| 161 | + plot_buffer = plots["combined_plots"] |
| 162 | + self.assertGreater(len(plot_buffer.getvalue()), 0) |
| 163 | + |
| 164 | + def test_generate_report_from_log_builds_document(self): |
| 165 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 166 | + log_path = os.path.join(tmp_dir, "training.log") |
| 167 | + output_path = os.path.join(tmp_dir, "report.pdf") |
| 168 | + with open(log_path, "w", encoding="utf-8") as log_file: |
| 169 | + log_file.write(SAMPLE_LOG) |
| 170 | + |
| 171 | + with mock.patch("carbontracker.report.SimpleDocTemplate") as mock_doc_template, mock.patch( |
| 172 | + "carbontracker.report.Image" |
| 173 | + ) as mock_image: |
| 174 | + mock_image.return_value = mock.Mock(name="ImageFlowable") |
| 175 | + doc_instance = mock_doc_template.return_value |
| 176 | + report.generate_report_from_log(log_path, output_path) |
| 177 | + |
| 178 | + mock_doc_template.assert_called_once_with( |
| 179 | + output_path, pagesize=report.letter, rightMargin=72, leftMargin=72, topMargin=24, bottomMargin=72 |
| 180 | + ) |
| 181 | + doc_instance.build.assert_called_once() |
| 182 | + story_passed = doc_instance.build.call_args[0][0] |
| 183 | + self.assertGreater(len(story_passed), 0) |
| 184 | + self.assertTrue(any(isinstance(flowable, report.Paragraph) for flowable in story_passed)) |
| 185 | + |
0 commit comments