78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
||
"""Замер суточного потребления coarena по АККАУНТАМ (не по ключам).
|
||
|
||
Почему не по ключам: все 10 ключей одного аккаунта отдают ОДИН И ТОТ ЖЕ
|
||
`used_usd` (лимит $5/сутки общий на аккаунт). Выборка по ключам видит аккаунт
|
||
столько раз, сколько у него ключей, и завышает сумму в ~9 раз.
|
||
|
||
Запуск: python acct_usage.py [N_аккаунтов] [путь_к_keys_bank.jsonl]
|
||
"""
|
||
import json
|
||
import random
|
||
import sys
|
||
import urllib.error
|
||
import urllib.request
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
PATH = sys.argv[2] if len(sys.argv) > 2 else r"C:/Users/User/tmp/coarena/keys_bank.jsonl"
|
||
N = int(sys.argv[1]) if len(sys.argv) > 1 else 120
|
||
|
||
rows = []
|
||
bad = 0
|
||
for line in open(PATH, encoding="utf8"):
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
try:
|
||
rows.append(json.loads(line))
|
||
except Exception:
|
||
bad += 1 # живая волна дописывает файл, хвостовая строка рвётся
|
||
|
||
by = {}
|
||
for r in rows:
|
||
by.setdefault(r["email"], []).append(r["key"])
|
||
accts = list(by)
|
||
print("строк ok: %d | битых: %d | аккаунтов: %d" % (len(rows), bad, len(accts)))
|
||
random.seed(23)
|
||
sample = random.sample(accts, min(N, len(accts)))
|
||
|
||
|
||
def probe(acc):
|
||
req = urllib.request.Request("https://coarena.ai/api/v1/limits",
|
||
headers={"Authorization": "Bearer " + by[acc][0]})
|
||
try:
|
||
with urllib.request.urlopen(req, timeout=25) as x:
|
||
d = json.loads(x.read().decode())
|
||
t = (d.get("data") or {}).get("today") or {}
|
||
return float(t.get("used_usd") or 0), int(t.get("requests") or 0), None
|
||
except urllib.error.HTTPError as e:
|
||
return None, None, e.code
|
||
except Exception as e:
|
||
return None, None, type(e).__name__
|
||
|
||
|
||
with ThreadPoolExecutor(max_workers=12) as ex:
|
||
res = list(ex.map(probe, sample))
|
||
ok = [r for r in res if r[0] is not None]
|
||
errs = {}
|
||
for r in res:
|
||
if r[0] is None:
|
||
errs[r[2]] = errs.get(r[2], 0) + 1
|
||
if not ok:
|
||
print("все пробы упали:", errs)
|
||
raise SystemExit(1)
|
||
|
||
tot = sum(r[0] for r in ok)
|
||
reqs = sum(r[1] for r in ok)
|
||
zero = sum(1 for r in ok if r[0] == 0)
|
||
spent = sum(1 for r in ok if r[0] > 0)
|
||
capped = sum(1 for r in ok if r[0] >= 4.9)
|
||
avg = tot / len(ok)
|
||
print("выборка %d акк | ok %d | ошибки %s" % (len(sample), len(ok), errs))
|
||
print("сумма used_usd: $%.2f | запросов: %d" % (tot, reqs))
|
||
print("нулевых %d (%.0f%%) | с расходом %d | упёрлись в $5: %d"
|
||
% (zero, 100 * zero / len(ok), spent, capped))
|
||
print("средний на аккаунт: $%.4f | медиана: $%.4f" % (avg, sorted(r[0] for r in ok)[len(ok) // 2]))
|
||
print("ЭКСТРАПОЛЯЦИЯ на %d аккаунтов: $%.0f/сутки (потолок $%d)"
|
||
% (len(accts), avg * len(accts), 5 * len(accts)))
|