#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
compare_tokens_multi.py

统计多个模型在指定类别文件下（如 coding.json, knowledge.json, math.json 等）
的 time 平均值。
"""

import json
from pathlib import Path

# ========= 配置区：按需修改 ========= #

# 关键词：必须是 ["coding", "knowledge", "math", "reasoning", "roleplay", "writing"]
KEYWORD = "writing"

# 19个模型文件夹路径
MODEL_DIRS = [
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
]

# =================================== #


def load_json(path: Path):
    """读取 JSON（数组形式）。"""
    with path.open("r", encoding="utf-8") as f:
        return json.load(f)


def calc_avg_time(model_file: Path):
    """计算某个模型文件的 time 平均值"""
    if not model_file.exists():
        print(f"[Skip] 文件不存在: {model_file}")
        return None, 0, 0

    data = load_json(model_file)
    total_sum = 0
    count = 0

    for item in data:
        time = item.get("time")
        if isinstance(time, (int, float)):  # 确保是数字
            total_sum += time
            count += 1

    if count == 0:
        return None, 0, len(data)

    avg = total_sum / count
    return avg, count, len(data)


def main():
    print(f"\n=== 多模型 {KEYWORD}.json 的 time 平均值 ===")
    for model_dir in MODEL_DIRS:
        model_file = model_dir / f"{KEYWORD}.json"
        avg, count, total = calc_avg_time(model_file)
        if avg is None:
            print(f"{model_dir.name} 的 {KEYWORD}.json 没有有效的 time 数据 (有效 0/{total})")
            continue
        print(f"{model_dir.name} 的 {KEYWORD}.json time 平均值为: {avg:.2f} (有效 {count}/{total})")


if __name__ == "__main__":
    main()
