From 2884a7ccf80695e5d3235d6f881daccad4b43860 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Mar 2026 21:49:10 +0100 Subject: [PATCH] LLR history graph!!! --- OpenBench/static/style.css | 153 ++++++++++++++++++++++ OpenBench/templatetags/mytags.py | 206 ++++++++++++++++++++++++++++++ OpenBench/utils.py | 47 +++++++ Templates/OpenBench/workload.html | 84 ++++++++++++ 4 files changed, 490 insertions(+) diff --git a/OpenBench/static/style.css b/OpenBench/static/style.css index 1726d8d..f1341c1 100644 --- a/OpenBench/static/style.css +++ b/OpenBench/static/style.css @@ -309,6 +309,159 @@ body, html, main{ padding: 0.50rem 0.75rem; } +#content .llr-history-panel { + margin: 0.5rem 0 0.75rem; +} + +#content .llr-history-widget { + position: relative; + width: 372px; +} + +#content .llr-history-chart { + display: flex; + align-items: stretch; + gap: 0.55rem; +} + +#content .llr-history-yaxis { + width: 44px; + display: flex; + flex-direction: column; + justify-content: space-between; + color: #A7B1C2; + font-size: 10px; + text-align: right; + padding: 0.35rem 0 1.6rem; +} + +#content .llr-history-main { + display: flex; + flex-direction: column; + gap: 0.28rem; +} + +#content .llr-history-plot { + position: relative; +} + +#content .llr-history-label { + color: var(--color-font3); + font-size: 0.85rem; + margin-bottom: 0.2rem; +} + +#content .llr-history-graph { + display: block; + width: 320px; + height: 112px; +} + +#content .llr-history-graph .llr-bg { + fill: #242933; + stroke: #4C566A; + stroke-width: 1; +} + +#content .llr-history-graph .llr-grid-line { + stroke: rgba(236, 239, 244, 0.14); + stroke-width: 1; +} + +#content .llr-history-graph .llr-boundary { + stroke: #BF616A; + stroke-width: 1; + stroke-dasharray: 4 3; + opacity: 0.85; +} + +#content .llr-history-graph .llr-zero { + stroke: #81A1C1; + stroke-width: 1; + opacity: 0.7; +} + +#content .llr-history-graph .llr-path-positive, +#content .llr-history-graph .llr-path-negative { + fill: none; + stroke-width: 2; + stroke-linecap: round; + stroke-linejoin: round; +} + +#content .llr-history-graph .llr-path-positive { + stroke: #7BCB8B; +} + +#content .llr-history-graph .llr-path-negative { + stroke: #E07A7A; +} + +#content .llr-history-xaxis { + display: flex; + justify-content: space-between; + color: #A7B1C2; + font-size: 10px; + padding: 0 0.2rem; +} + +#content .llr-history-graph .llr-point { + fill: #ECEFF4; +} + +#content .llr-history-graph .llr-hover-line { + stroke: #ECEFF4; + stroke-width: 1; + stroke-dasharray: 2 2; + opacity: 0; +} + +#content .llr-history-graph .llr-hover-point { + fill: #ECEFF4; + stroke-width: 1.5; + opacity: 0; +} + +#content .llr-history-graph .llr-hover-line.active, +#content .llr-history-graph .llr-hover-point.active { + opacity: 0.85; +} + +#content .llr-history-graph .llr-hover-point-positive { + stroke: #7BCB8B; +} + +#content .llr-history-graph .llr-hover-point-negative { + stroke: #E07A7A; +} + +#content .llr-history-graph .llr-hitbox { + fill: transparent; + cursor: crosshair; +} + +#content .llr-history-tooltip { + position: absolute; + left: 0; + top: 0; + padding: 0.18rem 0.4rem 0.12rem; + border-radius: 4px; + background-color: rgba(36, 41, 51, 0.96); + border: 1px solid #4C566A; + color: var(--color-font1); + font-size: 0.72rem; + pointer-events: none; + opacity: 0; + transform: translateY(2px); + transition: opacity 0.12s ease, transform 0.12s ease; + white-space: nowrap; +} + +#content .llr-history-tooltip.active { + opacity: 1; + transform: translateY(0); +} + /* Test Views */ #content #config { diff --git a/OpenBench/templatetags/mytags.py b/OpenBench/templatetags/mytags.py index 8e69745..ec320ec 100644 --- a/OpenBench/templatetags/mytags.py +++ b/OpenBench/templatetags/mytags.py @@ -18,9 +18,13 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +import html +import json import re, django import OpenBench.config, OpenBench.utils, OpenBench.stats, OpenBench.models +from django.utils.safestring import mark_safe + def oneDigitPrecision(value): try: value = round(value, 1) @@ -200,6 +204,207 @@ def machine_name(machine_id): return machine.info['machine_name'] except: return 'None' +def llr_history_graph(test, width=320, height=112): + + if test.test_mode != 'SPRT': + return '' + + history = list(OpenBench.utils.get_llr_history(test)) + if not history: + history = [[0, 0.0], [max(test.games, 1), test.currentllr]] + elif history[-1][0] != test.games or history[-1][1] != test.currentllr: + history.append([test.games, test.currentllr]) + + x_max = max(max(point[0] for point in history), 1) + observed = [point[1] for point in history] + [0.0] + observed_min = min(observed) + observed_max = max(observed) + observed_span = observed_max - observed_min + + if observed_span < 0.75: + center = (observed_min + observed_max) / 2.0 + y_min = center - 0.375 + y_max = center + 0.375 + else: + padding = max(observed_span * 0.15, 0.15) + y_min = observed_min - padding + y_max = observed_max + padding + + left_pad = 8 + right_pad = 8 + top_pad = 8 + bottom_pad = 8 + inner_width = max(width - left_pad - right_pad, 1) + inner_height = max(height - top_pad - bottom_pad, 1) + + def scale_x(value): + return left_pad + inner_width * (value / x_max) + + def scale_y(value): + return top_pad + inner_height * (1.0 - ((value - y_min) / (y_max - y_min))) + + plotted_points = [ + { + 'games': x, + 'llr': round(y, 4), + 'x': round(scale_x(x), 2), + 'y': round(scale_y(y), 2), + } + for x, y in history + ] + y_mid = (y_min + y_max) / 2.0 + x_mid = x_max / 2.0 + x_quarter = x_max / 4.0 + x_three_quarter = 3.0 * x_max / 4.0 + + grid_lines = [] + for value in [y_max, y_mid, y_min]: + y_pos = scale_y(value) + grid_lines.append( + '' + % (left_pad, y_pos, width - right_pad, y_pos) + ) + + for value in [x_quarter, x_mid, x_three_quarter]: + x_pos = scale_x(value) + grid_lines.append( + '' + % (x_pos, top_pad, x_pos, height - bottom_pad) + ) + + def interpolate_zero_crossing(a, b): + if a['llr'] == b['llr']: + return None + + ratio = (0.0 - a['llr']) / (b['llr'] - a['llr']) + if ratio < 0.0 or ratio > 1.0: + return None + + x_pos = a['x'] + ratio * (b['x'] - a['x']) + return { + 'games': a['games'] + ratio * (b['games'] - a['games']), + 'llr': 0.0, + 'x': round(x_pos, 2), + 'y': round(scale_y(0.0), 2), + } + + positive_segments = [] + negative_segments = [] + current_segment = [plotted_points[0]] + current_positive = plotted_points[0]['llr'] >= 0.0 + + for index in range(1, len(plotted_points)): + prev_point = plotted_points[index - 1] + point = plotted_points[index] + point_positive = point['llr'] >= 0.0 + + if point_positive == current_positive: + current_segment.append(point) + continue + + crossing = interpolate_zero_crossing(prev_point, point) + if crossing: + current_segment.append(crossing) + + if len(current_segment) >= 2: + if current_positive: + positive_segments.append(current_segment) + else: + negative_segments.append(current_segment) + + current_segment = [crossing, point] if crossing else [point] + current_positive = point_positive + + if len(current_segment) >= 2: + if current_positive: + positive_segments.append(current_segment) + else: + negative_segments.append(current_segment) + + positive_paths = ''.join( + '' % ( + ' '.join('%.2f,%.2f' % (point['x'], point['y']) for point in segment) + ) + for segment in positive_segments + ) + negative_paths = ''.join( + '' % ( + ' '.join('%.2f,%.2f' % (point['x'], point['y']) for point in segment) + ) + for segment in negative_segments + ) + + guides = [] + for value, css_class in [ + (test.lowerllr, 'llr-boundary'), + (0.0, 'llr-zero'), + (test.upperllr, 'llr-boundary'), + ]: + if y_min <= value <= y_max: + y_pos = scale_y(value) + guides.append( + '' + % (css_class, left_pad, y_pos, width - right_pad, y_pos) + ) + + latest_x = plotted_points[-1]['x'] + latest_y = plotted_points[-1]['y'] + title = 'LLR history: current %.2f after %d games' % (test.currentllr, test.games) + history_json = html.escape(json.dumps(plotted_points, separators=(',', ':'))) + + svg = ''' +
+
+
+
{y_max_label}
+
{y_mid_label}
+
{y_min_label}
+
+
+
+ + {title} + + {grid_lines} + {guides} + {positive_paths} + {negative_paths} + + + + +
+
+
+
0
+
{x_mid_label}
+
{x_max_label}
+
+
+
+
+ '''.format( + width=width, + height=height, + title=html.escape(title), + history_json=history_json, + grid_lines=''.join(grid_lines), + guides=''.join(guides), + positive_paths=positive_paths, + negative_paths=negative_paths, + latest_x=latest_x, + latest_y=latest_y, + top_pad=top_pad, + hover_bottom=height - bottom_pad, + y_max_label=html.escape('%.2f' % (y_max)), + y_mid_label=html.escape('%.2f' % (y_mid)), + y_min_label=html.escape('%.2f' % (y_min)), + x_mid_label=html.escape('%d' % (round(x_mid))), + x_max_label=html.escape('%d g' % (x_max)), + ) + + return mark_safe(svg) + register = django.template.Library() register.filter('oneDigitPrecision', oneDigitPrecision) @@ -218,6 +423,7 @@ register.filter('cpuflagsBlock', cpuflagsBlock) register.filter('compilerBlock', compilerBlock) register.filter('removePrefix', removePrefix) register.filter('machine_name', machine_name) +register.filter('llr_history_graph', llr_history_graph) #### diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 7a9eed4..7e530bb 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -133,6 +133,22 @@ def read_git_credentials(engine): def path_join(*args): return "/".join([f.lstrip("/").rstrip("/") for f in args]).rstrip('/') +def llr_history_path(test_id): + return os.path.join(MEDIA_ROOT, 'llr_history', '%d.json' % (test_id)) + +def get_llr_history(test): + + path = llr_history_path(test.id) + if not os.path.exists(path): + return [[0, 0.0]] if test.test_mode == 'SPRT' else [] + + try: + with open(path) as fin: + history = json.load(fin) + return history if history else [[0, 0.0]] + except Exception: + return [[0, 0.0]] + def extract_option(options, option): match = re.search(r'(?<={0}=")[^"]*'.format(option), options) @@ -419,6 +435,7 @@ def update_test(request, machine): with transaction.atomic(): test = Test.objects.select_for_update().get(id=test_id) + should_record_llr = False if test.finished or test.deleted: return { 'stop' : True } @@ -452,6 +469,7 @@ def update_test(request, machine): test.passed = test.currentllr > test.upperllr test.failed = test.currentllr < test.lowerllr test.finished = test.passed or test.failed + should_record_llr = True elif test.test_mode == 'GAMES': @@ -476,6 +494,9 @@ def update_test(request, machine): test.save() + if should_record_llr: + record_llr_history(test) + # Update Result object; No risk from concurrent access Result.objects.filter(id=result_id).update( games = F('games' ) + games, @@ -504,3 +525,29 @@ def update_test(request, machine): ) return [{}, { 'stop' : True }][test.finished] + +def record_llr_history(test, max_points=120): + + history = get_llr_history(test) + point = [test.games, round(test.currentllr, 4)] + + if history and history[-1][0] == point[0]: + history[-1] = point + else: + history.append(point) + + if not history or history[0][0] != 0: + history.insert(0, [0, 0.0]) + + if len(history) > max_points: + keep = [history[0]] + interior = history[1:-1:2] + history = keep + interior + [history[-1]] + + while len(history) > max_points: + history = [history[0]] + history[1:-1:2] + [history[-1]] + + path = llr_history_path(test.id) + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, 'w') as fout: + json.dump(history, fout) diff --git a/Templates/OpenBench/workload.html b/Templates/OpenBench/workload.html index 1abbc6d..a9faa7a 100644 --- a/Templates/OpenBench/workload.html +++ b/Templates/OpenBench/workload.html @@ -5,6 +5,83 @@ {% block scripts %} + {% endblock %} {% block content %} @@ -192,6 +269,13 @@ + + {% if workload.test_mode == "SPRT" %} +
+
LLR History
+ {{workload|llr_history_graph}} +
+ {% endif %}