Skip to content

Commit c380e4c

Browse files
committed
Generalize README benchmark
1 parent 68939a9 commit c380e4c

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

benchmarks/decoders/benchmark_decoders_library.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import subprocess
55
import timeit
6-
from concurrent.futures import ThreadPoolExecutor
6+
from concurrent.futures import ThreadPoolExecutor, wait
77
from itertools import product
88

99
import matplotlib.pyplot as plt
@@ -306,6 +306,7 @@ def generate_video(command):
306306
print(command)
307307
print(" ".join(command))
308308
subprocess.check_call(command)
309+
return True
309310

310311

311312
def generate_videos(
@@ -321,6 +322,7 @@ def generate_videos(
321322
executor = ThreadPoolExecutor(max_workers=20)
322323
video_count = 0
323324

325+
futures = []
324326
for resolution, duration, fps, gop_size, encoding, pix_fmt in product(
325327
resolutions, durations, fpses, gop_sizes, encodings, pix_fmts
326328
):
@@ -342,9 +344,12 @@ def generate_videos(
342344
pix_fmt,
343345
outfile,
344346
]
345-
executor.submit(generate_video, command)
347+
futures.append(executor.submit(generate_video, command))
346348
video_count += 1
347349

350+
wait(futures)
351+
for f in futures:
352+
assert f.result()
348353
executor.shutdown(wait=True)
349354
print(f"Generated {video_count} videos")
350355

@@ -442,21 +447,21 @@ def plot_data(df_data, plot_path):
442447

443448
def run_benchmarks(
444449
decoder_dict,
445-
video_paths,
450+
video_files_paths,
446451
num_uniform_samples,
447452
min_runtime_seconds,
448453
benchmark_video_creation,
449454
):
450455
results = []
451456
df_data = []
452-
print(f"video_paths={video_paths}")
457+
print(f"video_files_paths={video_files_paths}")
453458
verbose = False
454459
for decoder_name, decoder in decoder_dict.items():
455-
for video_path in video_paths:
456-
print(f"video={video_path}, decoder={decoder_name}")
460+
for video_file_path in video_files_paths:
461+
print(f"video={video_file_path}, decoder={decoder_name}")
457462
# We only use the VideoDecoder to get the metadata and get
458463
# the list of PTS values to seek to.
459-
simple_decoder = VideoDecoder(video_path)
464+
simple_decoder = VideoDecoder(video_file_path)
460465
duration = simple_decoder.metadata.duration_seconds
461466
pts_list = [
462467
i * duration / num_uniform_samples for i in range(num_uniform_samples)
@@ -465,16 +470,16 @@ def run_benchmarks(
465470
metadata_string = f"{metadata.codec} {metadata.width}x{metadata.height}, {metadata.duration_seconds}s {metadata.average_fps}fps"
466471
if verbose:
467472
print(
468-
f"video={video_path}, decoder={decoder_name}, pts_list={pts_list}"
473+
f"video={video_file_path}, decoder={decoder_name}, pts_list={pts_list}"
469474
)
470475
seeked_result = benchmark.Timer(
471476
stmt="decoder.get_frames_from_video(video_file, pts_list)",
472477
globals={
473-
"video_file": video_path,
478+
"video_file": video_file_path,
474479
"pts_list": pts_list,
475480
"decoder": decoder,
476481
},
477-
label=f"video={video_path} {metadata_string}",
482+
label=f"video={video_file_path} {metadata_string}",
478483
sub_label=decoder_name,
479484
description=f"{num_uniform_samples} seek()+next()",
480485
)
@@ -483,7 +488,7 @@ def run_benchmarks(
483488
)
484489
df_item = {}
485490
df_item["decoder"] = decoder_name
486-
df_item["video"] = video_path
491+
df_item["video"] = video_file_path
487492
df_item["description"] = results[-1].description
488493
df_item["frame_count"] = num_uniform_samples
489494
df_item["median"] = results[-1].median
@@ -498,11 +503,11 @@ def run_benchmarks(
498503
consecutive_frames_result = benchmark.Timer(
499504
stmt="decoder.get_consecutive_frames_from_video(video_file, consecutive_frames_to_extract)",
500505
globals={
501-
"video_file": video_path,
506+
"video_file": video_file_path,
502507
"consecutive_frames_to_extract": num_consecutive_nexts,
503508
"decoder": decoder,
504509
},
505-
label=f"video={video_path} {metadata_string}",
510+
label=f"video={video_file_path} {metadata_string}",
506511
sub_label=decoder_name,
507512
description=f"{num_consecutive_nexts} next()",
508513
)
@@ -513,7 +518,7 @@ def run_benchmarks(
513518
)
514519
df_item = {}
515520
df_item["decoder"] = decoder_name
516-
df_item["video"] = video_path
521+
df_item["video"] = video_file_path
517522
df_item["description"] = results[-1].description
518523
df_item["frame_count"] = num_consecutive_nexts
519524
df_item["median"] = results[-1].median
@@ -524,18 +529,18 @@ def run_benchmarks(
524529
df_item["fps_p25"] = 1.0 * num_consecutive_nexts / results[-1]._p25
525530
df_data.append(df_item)
526531

527-
first_video_path = video_paths[0]
532+
first_video_file_path = video_files_paths[0]
528533
if benchmark_video_creation:
529-
simple_decoder = VideoDecoder(first_video_path)
534+
simple_decoder = VideoDecoder(first_video_file_path)
530535
metadata = simple_decoder.metadata
531536
metadata_string = f"{metadata.codec} {metadata.width}x{metadata.height}, {metadata.duration_seconds}s {metadata.average_fps}fps"
532537
creation_result = benchmark.Timer(
533538
stmt="create_torchcodec_decoder_from_file(video_file)",
534539
globals={
535-
"video_file": first_video_path,
540+
"video_file": first_video_file_path,
536541
"create_torchcodec_decoder_from_file": create_torchcodec_decoder_from_file,
537542
},
538-
label=f"video={first_video_path} {metadata_string}",
543+
label=f"video={first_video_file_path} {metadata_string}",
539544
sub_label="TorchcodecNonCompiled",
540545
description="create()+next()",
541546
)

benchmarks/decoders/generate_readme_chart.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def main() -> None:
4242
gop_sizes = [600]
4343
durations = [10]
4444
pix_fmts = ["yuv420p"]
45-
ffmpeg_path = "/usr/local/bin/ffmpeg"
46-
videos_path = "/tmp/videos"
47-
shutil.rmtree(videos_path)
48-
os.makedirs(videos_path)
45+
ffmpeg_path = "ffmpeg"
46+
videos_dir_path = "/tmp/torchcodec_benchmarking_videos"
47+
shutil.rmtree(videos_dir_path, ignore_errors=True)
48+
os.makedirs(videos_dir_path)
4949
generate_videos(
5050
resolutions,
5151
encodings,
@@ -54,9 +54,9 @@ def main() -> None:
5454
durations,
5555
pix_fmts,
5656
ffmpeg_path,
57-
videos_path,
57+
videos_dir_path,
5858
)
59-
video_paths = glob.glob(f"{videos_path}/*.mp4")
59+
video_files_paths = glob.glob(f"{videos_dir_path}/*.mp4")
6060

6161
decoder_dict = {}
6262
decoder_dict["TorchCodec"] = TorchcodecNonCompiledWithOptions()
@@ -73,7 +73,7 @@ def main() -> None:
7373
num_uniform_samples = 10
7474
df_data = run_benchmarks(
7575
decoder_dict,
76-
video_paths,
76+
video_files_paths,
7777
num_uniform_samples,
7878
10,
7979
False,

0 commit comments

Comments
 (0)