DEV Community

Cover image for Hyperlambda is 17 times faster than Python with Flask
Thomas Hansen
Thomas Hansen

Posted on • Originally published at ainiro.io

Hyperlambda is 17 times faster than Python with Flask

I just measured the performance of Python with Flask, and compared it towards Magic with Hyperlambda, and the results should shock you. Hyperlambda and Magic is on average 17 times faster than Flask. I did the same test where I measure Fast API versus Hyperlambda, except of course I used Flask instead of Fast API, and the results was as follows.

  • Flask executed between 2,324 and 3,616 HTTP requests, an average of 4,778
  • Hyperlambda executed between 78,000 and 83,361 requests, an average of 80,680

The test creates 50 async tasks or threads, and basically just hammers a CRUD read endpoint that's returning Artist items from my chinook database. The database was copied and pasted from the Hyperlambda solution, so it's the exact same database. The machine is the same, a MacBook Pro M3, and you can find the code below if you wish to reproduce what I did.

Code

To test you can use the code from my Fast API article, but instead of creating a Fast API endpoint you use the following code.

from flask import Flask, jsonify
import sqlite3

app = Flask(__name__)

# Helper function to query SQLite
def get_all_artists():
    conn = sqlite3.connect("Chinook.db")
    conn.row_factory = sqlite3.Row   # Enables dict-like rows
    cursor = conn.cursor()

    cursor.execute("SELECT ArtistId, Name FROM Artist")
    rows = cursor.fetchall()

    conn.close()

    return [dict(row) for row in rows]


@app.route("/artists", methods=["GET"])
def artists():
    return jsonify(get_all_artists())


if __name__ == "__main__":
    app.run(debug=False, port=5001)
Enter fullscreen mode Exit fullscreen mode

The above is a standard solution returning database items using Flask. In case you don't want to check out my previous blog, you can find the "stress testing" code below.

import asyncio
import aiohttp
import time

URL = "http://127.0.0.1:5001/artists"
#URL = "http://127.0.0.1:5000/magic/modules/chinook/artist"

async def worker(session, stop_time, counters):
    while time.time() < stop_time:
        try:
            async with session.get(URL) as resp:
                if resp.status == 200:
                    counters["ok"] += 1
                else:
                    counters["fail"] += 1
        except Exception:
            counters["fail"] += 1

async def main():
    duration = 30  # seconds
    concurrency = 50  # number of parallel workers

    counters = {"ok": 0, "fail": 0}
    stop_time = time.time() + duration

    async with aiohttp.ClientSession() as session:
        tasks = [
            worker(session, stop_time, counters)
            for _ in range(concurrency)
        ]
        await asyncio.gather(*tasks)

    total = counters["ok"] + counters["fail"]
    print(f"Requests sent: {total}")
    print(f"Successful:     {counters['ok']}")
    print(f"Failed:         {counters['fail']}")
    print(f"Req/sec:        {total / duration}")

if __name__ == "__main__":
    asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

To test the Hyperlambda endpoint, change the URL variable you're using, run the test 3 times towards each endpoint, and calculate the average of each run. This should give you a representative average value for each example. To generate the Hyperlambda code, I simply used the CRUD generator to wrap my chinook database. Remember to turn off authorisation requirements before generating your CRUD endpoints to get the equivalent code, and make sure you either pass in limit=1 as a query paremeter to the endpoint, or edit the endpoint manually to return all records. Otherwise the Hyperlambda code will only return 25 items.

Interestingly, Flask seems to be able to accept more throughput than Fast API, but it of course becomes equivalent to "nothing" compared to the amount of throughput Hyperlambda can deal with.

Wrapping up

A Volkswagen Beetle can probably do 120KM per hour. Multiplying that number by 17, and you get 2,040KM per hour. That's about the same as the maximum speed of a modern fighter jet. Implying ...

Hyperlambda versus Python and Flask, is like trying to race a fighter jet with a Volkswagen

As in, yes, the performance difference between Flask and Hyperlambda is the same as the performance difference between a 20 year old Volkswagen and a modern fighter jet!

Top comments (0)