|
5 | 5 |
|
6 | 6 | def print_json(data): |
7 | 7 | print(json.dumps(data, indent=2, ensure_ascii=False)) |
| 8 | + print("-" * 60) |
8 | 9 |
|
9 | 10 | def print_timing(fn_name, elapsed): |
10 | 11 | print(f"⏱ {fn_name}: {elapsed:.3f}s") |
11 | | - print("-" * 60) |
| 12 | + print("=" * 60) |
12 | 13 |
|
13 | 14 | async def timed(fn_name, coro): |
14 | 15 | start = time.perf_counter() |
15 | 16 | result = await coro |
16 | 17 | elapsed = time.perf_counter() - start |
17 | | - print_timing(fn_name, elapsed) |
18 | | - return result |
| 18 | + return fn_name, elapsed, result |
19 | 19 |
|
20 | 20 | async def main(): |
21 | 21 | url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK" |
22 | 22 | url2 = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ" |
23 | 23 |
|
24 | | - print_json(await timed("Playlist.get", Playlist.get(url1))) |
25 | | - print_json(await timed("Playlist.getInfo", Playlist.getInfo(url1))) |
26 | | - print_json(await timed("Playlist.getVideos", Playlist.getVideos(url1))) |
27 | | - print_json(await timed("Playlist.get (cached)", Playlist.get(url1))) |
28 | | - print_json(await timed("Playlist.get (video+playlist URL)", Playlist.get(url2))) |
| 24 | + for fn, coro in [ |
| 25 | + ("Playlist.get", Playlist.get(url1)), |
| 26 | + ("Playlist.getInfo", Playlist.getInfo(url1)), |
| 27 | + ("Playlist.getVideos", Playlist.getVideos(url1)), |
| 28 | + ("Playlist.get (cached)", Playlist.get(url1)), |
| 29 | + ("Playlist.get (video+playlist URL)", Playlist.get(url2)), |
| 30 | + ]: |
| 31 | + name, elapsed, result = await timed(fn, coro) |
| 32 | + print_timing(name, elapsed) |
| 33 | + print_json(result) |
29 | 34 |
|
30 | 35 | asyncio.run(main()) |
0 commit comments