"...git@developer.sourcefind.cn:kecinstone/2024-pra-vllm.git" did not exist on "c8e7eb1eb3e3862cde4f7c27952d2eb9104ef416"
benchmark_multi_inst.py 2.9 KB
Newer Older
zhyh2010's avatar
zhyh2010 committed
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import argparse
import subprocess
import ffmpeg
import os
import sys

V4L2_CODEC_NAMES = {
    "h264": "h264_v4l2m2m",
    "hevc": "hevc_v4l2m2m",
    "vp9": "vp9_v4l2m2m",
    "avs2": "avs2_v4l2m2m",
    "mjpeg": "mjpeg_v4l2m2m",
}

CUVID_CODEC_NAMES = {
    "h264": "h264_cuvid",
    "hevc": "hevc_cuvid",
    "vp9": "vp9_cuvid",
    "avs2": "avs2_cuvid",
    "mjpeg": "mjpeg_cuvid",
}

def get_input_codec(input_file):
    """获取输入文件的编码格式(小写)"""
    try:
        probe = ffmpeg.probe(input_file, select_streams='v:0', show_entries='stream=codec_name')
        return probe['streams'][0]['codec_name'].lower()
    except Exception:
        return os.path.splitext(input_file)[1].replace('.', '').lower()

def run_multi_instance_benchmark(input_file, codec_type, threads, log_file_path):
    """运行多实例基准测试"""
    if not os.path.isfile(input_file):
        print(f"Error: Input file '{input_file}' does not exist.", file=sys.stderr)
        sys.exit(1)
    
    input_codec = get_input_codec(input_file)
    
    if codec_type == "cuda":
        stream = ffmpeg.input(input_file, hwaccel='cuda')
    elif codec_type == "v4l2":
        decoder = V4L2_CODEC_NAMES.get(input_codec, input_codec)
        stream = ffmpeg.input(input_file, vcodec=decoder)
    elif codec_type == "cuvid":
        decoder = CUVID_CODEC_NAMES.get(input_codec, input_codec)
        stream = ffmpeg.input(input_file, vcodec=decoder)
    elif codec_type == "vaapi":
        stream = ffmpeg.input(input_file, hwaccel='vaapi')
    else:
        print(f"Unknown codec type: {codec_type}", file=sys.stderr)
        sys.exit(1)
    
    cmd = ffmpeg.output(stream, 'pipe:', format='null').global_args('-benchmark', '-y').compile()
    processes = []
    
    print(f"Starting {threads} concurrent ffmpeg processes...", file=sys.stderr)
    
    with open(log_file_path, 'w') as log_f:
        log_f.write(f"# Multi-instance benchmark log\n")
        log_f.write(f"# Command: {' '.join(cmd)}\n")
        log_f.write(f"# Threads: {threads}\n\n")
        
        for i in range(threads):
            p = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=log_f)
            processes.append(p)
        
        for p in processes:
            p.wait()
            
    print("All processes finished.", file=sys.stderr)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Multi-instance FFmpeg Benchmark")
    parser.add_argument("-i", "--input", required=True, help="Input video file")
    parser.add_argument("-c", "--codec", default="cuda", choices=["cuda", "cuvid", "v4l2", "vaapi"], help="Codec type")
    parser.add_argument("-n", "--threads", type=int, default=6, help="Number of concurrent processes")
    parser.add_argument("-o", "--output", required=True, help="Output log file")
    args = parser.parse_args()
    
    run_multi_instance_benchmark(args.input, args.codec, args.threads, args.output)