#!/usr/bin/env python3
#
# jitterdebugger - real time response messaurement tool
#
# Copyright (c) Siemens AG, 2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys
import json
import matplotlib.pyplot as plt

with open(sys.argv[1]) as file:
    rawdata = json.load(file)

cpu_id = 0
fig, ax = plt.subplots()
while True:
    if str(cpu_id) not in rawdata['cpu']:
        break

    cid = str(cpu_id)
    data = rawdata['cpu'][cid]
    d = {int(k): int(v) for k, v in data['histogram'].items()}
    l = 'cpu{} max{:>3} avg{:>5} min{:>3}'.format(
        cid,
        rawdata['cpu'][cid]['max'],
        rawdata['cpu'][cid]['avg'],
        rawdata['cpu'][cid]['min'])
    ax.bar(list(d.keys()), list(d.values()), log=True, alpha=0.5, label=l)

    cpu_id = cpu_id + 1

L = ax.legend()
plt.setp(L.texts, family='monospace')
plt.show()
