File size: 1,710 Bytes
330f477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import asyncio
import json
import resource
import sys
import time
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from openmusic_analysis.application import build_service


async def main() -> None:
    parser = argparse.ArgumentParser(description="Measure model load and per-representation latency")
    parser.add_argument("audio", type=Path)
    parser.add_argument("--lyrics", type=Path)
    args = parser.parse_args()
    service = build_service()
    lyrics = args.lyrics.read_text(encoding="utf-8") if args.lyrics else None
    measurements = {}
    started = time.perf_counter()
    await service.load_models()
    measurements["model_startup_seconds"] = time.perf_counter() - started
    representations = ["audio.global", "audio.temporal"]
    if lyrics is not None:
        representations.append("lyrics.global")
    for representation in representations:
        started = time.perf_counter()
        await service.analyze(
            args.audio,
            lyrics=lyrics,
            requested_representations=[representation],
            track_id=None,
            content_identity=None,
        )
        measurements[f"{representation}_seconds"] = time.perf_counter() - started
    measurements["peak_process_rss_platform_units"] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    try:
        import torch

        if torch.cuda.is_available():
            measurements["peak_cuda_memory_bytes"] = torch.cuda.max_memory_allocated()
    except ImportError:
        pass
    print(json.dumps(measurements, indent=2))


if __name__ == "__main__":
    asyncio.run(main())