kv-tube/benchmark_ytdlp.py

55 lines
1.5 KiB
Python

import time
import sys
import subprocess
import json
import yt_dlp
QUERY = "latest smart technology gadgets reviews"
LIMIT = 20
def test_subprocess():
start = time.time()
cmd = [
sys.executable, "-m", "yt_dlp",
f"ytsearch{LIMIT}:{QUERY}",
"--dump-json",
"--flat-playlist",
"--no-playlist",
"--no-warnings"
]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
out, err = proc.communicate()
end = time.time()
count = len(out.splitlines())
return end - start, count
def test_library():
start = time.time()
ydl_opts = {
'headers': {'User-Agent': 'Mozilla/5.0'},
'skip_download': True,
'extract_flat': True,
'noplaylist': True,
'quiet': True,
'no_warnings': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
res = ydl.extract_info(f"ytsearch{LIMIT}:{QUERY}", download=False)
count = len(res.get('entries', []))
end = time.time()
return end - start, count
if __name__ == "__main__":
print("Benchmarking Subprocess...")
try:
sub_time, sub_count = test_subprocess()
print(f"Subprocess: {sub_time:.4f}s (Fetched {sub_count} items)")
except Exception as e:
print(f"Subprocess Failed: {e}")
print("\nBenchmarking Library...")
try:
lib_time, lib_count = test_library()
print(f"Library: {lib_time:.4f}s (Fetched {lib_count} items)")
except Exception as e:
print(f"Library Failed: {e}")