Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asyncio vs purely synchronous performance #76

Closed
indiqn opened this issue Nov 26, 2023 · 3 comments
Closed

Asyncio vs purely synchronous performance #76

indiqn opened this issue Nov 26, 2023 · 3 comments

Comments

@indiqn
Copy link

indiqn commented Nov 26, 2023

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
Copy link
Collaborator

dotX12 commented Dec 3, 2023

@indiqn, hello!
#12

@dotX12
Copy link
Collaborator

dotX12 commented Dec 20, 2023

#78

@dotX12
Copy link
Collaborator

dotX12 commented Feb 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants