shazamio / ShazamIO

🎵 Is a free asynchronous library from reverse engineered Shazam API written in Python 3.8+ with asyncio and aiohttp.
MIT License
490 stars 69 forks source link

Asyncio vs purely synchronous performance #76

Closed indiqn closed 7 months ago

indiqn commented 10 months ago

I made a small experiment to understand the benefit of using asyncio but so far I found that there's no improvement over making synchronous requests to the API. It takes around 4 seconds for one request and the total time for the program scales with the number of calls. I've even made each request run in its own thread but the result was still the same. I am quite confused by this behaviour.

Does anyone have insights into this ?

import asyncio
from shazamio import Shazam
import threading 

async def run_task(shazam): 
    ret = await shazam.recognize_song('SomeFile.mp3')

def run_all_tasks(iters):
    shazam = Shazam()
    threads = []

    for i in range(iters):
        thread = threading.Thread(target=asyncio.run, args=(run_task(shazam),))
        threads.append(thread)
        thread.start()

    # Wait for all threads to finish
    for thread in threads:
        thread.join()

run_all_tasks(5)
dotX12 commented 10 months ago

@indiqn, hello! https://github.com/dotX12/ShazamIO/issues/12

dotX12 commented 9 months ago

https://github.com/dotX12/ShazamIO/issues/78

dotX12 commented 7 months ago

0.5.0 Release (https://github.com/shazamio/ShazamIO/releases/tag/0.5.0) ! Enormous acceleration and no blocking, real async!

Test 1.

async def main():
    t1 = time.time()

    for i in range(10):
        new_version_path = await shazam.recognize("data/dora.ogg")
        serialized_new_path = Serialize.full_track(new_version_path)
        print(serialized_new_path)
    t2 = time.time()
    print(t2 - t1)

# 7.2795631885528564 - recognize_song (OLD VERSION)
# 3.6113240718841553 - recognize (NEW VERSION)

Test 2.

async def test_old(shazam):
    new_version_path = await shazam.recognize_song("data/dora.ogg")
    serialized_new_path = Serialize.full_track(new_version_path)
    print(serialized_new_path)

async def test_new(shazam):
    new_version_path = await shazam.recognize("data/dora.ogg")
    serialized_new_path = Serialize.full_track(new_version_path)
    print(serialized_new_path)

async def main():
    shazam = Shazam()
    t1 = time.time()
    a = asyncio.gather(*[test_old(shazam) for _ in range(10)])
    a = asyncio.gather(*[test_new(shazam) for _ in range(20)])
    await a
    t2 = time.time()

    print(t2-t1)
    # recognize_song - 5.2275474071502686
    # recognize - 0.5232067108154297