SPB Git

Repository Public License

bench.sh 5.3 KB 96 lines shellscript

1#!/usr/bin/env bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : ORIX Monitor8# File             : Scripts/bench.sh9# Module           : Scripts10# Repository       : https://git.spboucher.ai/orix-monitor11# Created          : 2026-11-2112# Last Modified    : 2026-11-2113#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: support@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin   : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License          : Proprietary, all rights reserved (see license file)32# License File     : LICENSE33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# ORIX Monitor42# File: Scripts/bench.sh43# Description: Measures ORIX Monitor on itself: runs an isolated instance of the Release app, then reads its own CPU, memory and disk figures.44#45# Author: Simon-Pierre Boucher46# Contact: contact@spboucher.ai47#   Scripts/bench.sh [seconds=120] [section=overview] [interval=2]48# Output: build/bench/<section>-<interval>s.json. Figures are measurements of this run on this Mac, nothing else.49set -euo pipefail50export LC_ALL=C   # decimal points whatever the system locale51cd "$(dirname "$0")/.."52SECONDS_TO_RUN="${1:-120}"; SECTION="${2:-overview}"; INTERVAL="${3:-2}"53APP="${ORIX_APP:-dist/ORIX Monitor.app}"54[[ -d "$APP" ]] || { echo "ERROR: $APP not found. Run Scripts/build.sh release first." >&2; exit 1; }55DATA="$(mktemp -d /tmp/orix-bench.XXXXXX)"; mkdir -p build/bench56"$APP/Contents/MacOS/ORIX Monitor" -ORIXUITest YES -ORIXDataDir "$DATA" -ORIXSection "$SECTION" -ORIXInterval "$INTERVAL" -ApplePersistenceIgnoreState YES >/dev/null 2>&1 &57PID=$!58trap 'kill $PID 2>/dev/null || true' EXIT59sleep 10                                   # let launch work settle before measuring60cpu_seconds() { ps -o time= -p "$PID" | awk -F'[:.]' '{ n=NF; s=$(n-1)+$n/100; if (n>=3) s+=$(n-2)*60; if (n>=4) s+=$(n-3)*3600; print s }'; }61C0="$(cpu_seconds)"; T0="$(date +%s)"62sleep "$SECONDS_TO_RUN"63C1="$(cpu_seconds)"; T1="$(date +%s)"64RSS_KB="$(ps -o rss= -p "$PID" | tr -d ' ')"65FOOTPRINT="$(footprint "$PID" 2>/dev/null | awk '/phys_footprint:/{print $2, $3; exit}')"66# Ended by PID, never by bundle identifier: a copy the owner is using must not be touched. Samples of the last67# flush period (30 s) may be missing from the database; the averages below rest on what was flushed.68kill "$PID" 2>/dev/null || true69sleep 170DB="$DATA/telemetry.sqlite"71python3 - "$DB" "$C0" "$C1" "$T0" "$T1" "$RSS_KB" "$FOOTPRINT" "$SECTION" "$INTERVAL" "$APP" <<'PY' | tee "build/bench/$SECTION-${INTERVAL}s.json"72import json, os, sqlite3, subprocess, sys73db, c0, c1, t0, t1, rss, footprint, section, interval, app = sys.argv[1:11]74wall = float(t1) - float(t0)75out = {"section": section, "sampling_interval_s": float(interval), "measured_seconds": wall,76       "external": {"cpu_percent_of_one_core": round((float(c1) - float(c0)) / wall * 100, 2), "rss_mb": round(int(rss) / 1024, 1), "phys_footprint": footprint,77                    "method": "ps cumulative CPU time before/after; ps rss; footprint(1)"}}78if os.path.exists(db):79    con = sqlite3.connect(db)80    def stats(metric):81        row = con.execute("SELECT COUNT(*), AVG(v), MAX(v) FROM raw WHERE metric=? AND ts > (SELECT MIN(ts) FROM raw) + 10000", (metric,)).fetchone()82        return {"samples": row[0], "average": row[1], "maximum": row[2]}83    cpu, mem = stats(90), stats(91)84    out["self_reported"] = {"cpu_percent_of_one_core": {k: (round(v, 2) if isinstance(v, float) else v) for k, v in cpu.items()},85                            "memory_footprint_mb": {"average": round((mem["average"] or 0) / 1048576, 1), "maximum": round((mem["maximum"] or 0) / 1048576, 1)},86                            "method": "proc_pid_rusage on itself, stored as metrics self.cpu / self.memory"}87    rows = con.execute("SELECT COUNT(*) FROM raw").fetchone()[0]88    size = sum(os.path.getsize(db + s) for s in ("", "-wal") if os.path.exists(db + s))89    out["storage"] = {"raw_rows": rows, "database_bytes": size, "bytes_per_hour_at_this_rate": round(size / max(wall + 10, 1) * 3600)}90info = subprocess.run(["sysctl", "-n", "hw.model", "machdep.cpu.brand_string"], capture_output=True, text=True).stdout.split("\n")91out["machine"] = f"{info[0]} · {info[1]} · macOS {subprocess.run(['sw_vers', '-productVersion'], capture_output=True, text=True).stdout.strip()}"92out["power_source"] = subprocess.run(["pmset", "-g", "batt"], capture_output=True, text=True).stdout.split("'")[1]93print(json.dumps(out, indent=2))94PY95rm -rf "$DATA"96
Files main
Menu

Theme