Skip to content
Snippets Groups Projects
test_likwid_parser.py 3.45 KiB
import json
import tempfile
import os
import pytest

from cbutil.likwid_parser import parse_likwid_json


def test_parse_likwid_json():
    # create temporary file with sample JSON data
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
        data = {
            "Performance group": {
                "Performance group":
                {
                    "Metric STAT": {
                        "metric1": {
                            "Sum": 10,
                            "Avg": 5,
                            "Min": 2,
                            "Max": 8
                        },
                        "metric2": {
                            "Sum": 20,
                            "Avg": 10,
                            "Min": 5,
                            "Max": 15
                        }
                    }
                }}
        }
        json.dump(data, tmp_file)
        tmp_file.flush()

        # test case 1: all keys specified
        expected_output = {
            "metric1 Sum": 10,
            "metric2 Sum": 20,
            "metric1 Avg": 5,
            "metric2 Avg": 10,
            "metric1 Min": 2,
            "metric2 Min": 5,
            "metric1 Max": 8,
            "metric2 Max": 15
        }
        assert parse_likwid_json(tmp_file.name,
                                 "Performance group",
                                 sum_keys=["metric1", "metric2"],
                                 avg_keys=["metric1", "metric2"],
                                 min_keys=["metric1", "metric2"],
                                 max_keys=["metric1", "metric2"]) == expected_output

        # test case 2: only sum keys specified
        expected_output = {
            "metric1 Sum": 10,
            "metric2 Sum": 20
        }
        assert parse_likwid_json(tmp_file.name,
                                 "Performance group",
                                 sum_keys=["metric1", "metric2"]) == expected_output

    # cleanup temporary file
    os.unlink(tmp_file.name)


def test_parse_likwid_json_fail():
    with pytest.raises(FileNotFoundError):
        parse_likwid_json("invalid_file.json", "Performance group", sum_keys=["metric1"])


def test_parse_likwid_json_real_data():
    test_data = "tests/likwid_MEM_DP_fe2ti216bddc_skylakesp2_pardiso_80_1_10_1e-8.json"
    sum_keys = ['AVX DP [MFLOP/s] STAT',
                'CPI STAT',
                'DP [MFLOP/s] STAT',
                'Energy DRAM [J] STAT',
                'Energy [J] STAT',
                'Memory bandwidth [MBytes/s] STAT',
                'Memory data volume [GBytes] STAT',
                'Memory read bandwidth [MBytes/s] STAT',
                'Memory read data volume [GBytes] STAT',
                'Memory write bandwidth [MBytes/s] STAT',
                'Memory write data volume [GBytes] STAT',
                'Operational intensity STAT',
                'Packed [MUOPS/s] STAT',
                'Power DRAM [W] STAT',
                'Power [W] STAT',
                'Scalar [MUOPS/s] STAT']
    avg_keys = ['Runtime (RDTSC) [s] STAT',
                'Clock [MHz] STAT',
                'DP [MFLOP/s] STAT',
                ]
    min_keys = ['Clock [MHz] STAT',
                'DP [MFLOP/s] STAT', ]
    max_keys = min_keys
    parse_likwid_json(test_data,
                      "MEM_DP",
                      sum_keys=sum_keys,
                      avg_keys=avg_keys,
                      min_keys=min_keys,
                      max_keys=max_keys)