├── go ├── go.mod └── main.go ├── dotnet ├── dotnet.csproj └── Program.cs ├── README.md ├── elixir └── main.exs ├── nodejs └── main.js ├── python └── main.py └── java ├── Threads.java └── VirtualThreads.java /go/go.mod: -------------------------------------------------------------------------------- 1 | module coroutines 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /dotnet/dotnet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # async-runtimes-benchmarks 2 | 3 | This repository contains a few programs written in various programing languages. 4 | Each program spawns N concurrent tasks. The number of tasks is controlled by the 5 | command line argiment. Each tasks waits 10 seconds and finishes. The programs finish when 6 | all tasks exit. 7 | 8 | The purpose of those programs is to measure the memory footprint of concurrent tasks in 9 | different language runtimes. 10 | -------------------------------------------------------------------------------- /elixir/main.exs: -------------------------------------------------------------------------------- 1 | defmodule Main do 2 | def start() do 3 | num_tasks = case System.argv |> List.first() |> Integer.parse() do 4 | {num, _} -> num 5 | _ -> 10000 6 | end 7 | 8 | tasks = 9 | for _ <- 1..num_tasks do 10 | Task.async(fn -> 11 | :timer.sleep(10000) 12 | end) 13 | end 14 | 15 | Task.await_many(tasks, :infinity) 16 | IO.puts("All tasks completed") 17 | end 18 | end 19 | 20 | Main.start() 21 | 22 | -------------------------------------------------------------------------------- /nodejs/main.js: -------------------------------------------------------------------------------- 1 | const util = require('util'); 2 | 3 | const delay = util.promisify(setTimeout); 4 | 5 | async function runTasks(numTasks) { 6 | const tasks = []; 7 | 8 | for (let i = 0; i < numTasks; i++) { 9 | tasks.push(delay(10000)); 10 | } 11 | 12 | await Promise.all(tasks); 13 | 14 | console.log('All tasks completed'); 15 | } 16 | 17 | const numTasks = parseInt(process.argv[2]) || 10000; 18 | 19 | runTasks(numTasks).catch((error) => { 20 | console.error('Error:', error); 21 | }); 22 | -------------------------------------------------------------------------------- /python/main.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import sys 3 | 4 | async def perform_task(): 5 | await asyncio.sleep(10) 6 | 7 | 8 | async def main(num_tasks): 9 | tasks = [] 10 | 11 | for task_id in range(num_tasks): 12 | task = asyncio.create_task(perform_task()) 13 | tasks.append(task) 14 | 15 | await asyncio.gather(*tasks) 16 | 17 | if __name__ == "__main__": 18 | if len(sys.argv) > 1: 19 | num_tasks = int(sys.argv[1]) 20 | else: 21 | num_tasks = 100000 22 | 23 | asyncio.run(main(num_tasks)) 24 | print("All tasks completed") 25 | 26 | -------------------------------------------------------------------------------- /go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strconv" 7 | "sync" 8 | "time" 9 | ) 10 | 11 | func main() { 12 | numRoutines := 100000 13 | if len(os.Args) > 1 { 14 | n, err := strconv.Atoi(os.Args[1]) 15 | if err == nil { 16 | numRoutines = n 17 | } 18 | } 19 | 20 | var wg sync.WaitGroup 21 | for i := 0; i < numRoutines; i++ { 22 | wg.Add(1) 23 | go func() { 24 | defer wg.Done() 25 | time.Sleep(10 * time.Second) 26 | }() 27 | } 28 | wg.Wait() 29 | fmt.Println("All goroutines finished.") 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /dotnet/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | class Program 7 | { 8 | static async Task Main(string[] args) 9 | { 10 | int numTasks = args.Length > 0 ? int.Parse(args[0]) : 100000; 11 | Console.WriteLine($"Starting {numTasks} tasks"); 12 | 13 | List tasks = new List(); 14 | 15 | for (int i = 0; i < numTasks; i++) 16 | { 17 | Task task = Task.Run(async () => 18 | { 19 | await Task.Delay(TimeSpan.FromSeconds(10)); 20 | }); 21 | 22 | tasks.Add(task); 23 | } 24 | 25 | await Task.WhenAll(tasks); 26 | 27 | Console.WriteLine("All tasks complete"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/Threads.java: -------------------------------------------------------------------------------- 1 | import java.time.Duration; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class Threads { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | int numTasks = 100000; 9 | if (args.length > 0) { 10 | numTasks = Integer.parseInt(args[0]); 11 | } 12 | 13 | List threads = new ArrayList<>(); 14 | 15 | for (int i = 0; i < numTasks; i++) { 16 | Thread thread = new Thread(() -> { 17 | try { 18 | Thread.sleep(Duration.ofSeconds(10)); 19 | } catch (InterruptedException e) { 20 | // Handle exception 21 | } 22 | }); 23 | thread.start(); 24 | threads.add(thread); 25 | } 26 | 27 | // Wait for all threads to complete 28 | for (Thread thread : threads) { 29 | thread.join(); 30 | } 31 | 32 | System.out.println("All fibers complete"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /java/VirtualThreads.java: -------------------------------------------------------------------------------- 1 | import java.time.Duration; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | public class VirtualThreads { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | int numTasks = 100000; 9 | if (args.length > 0) { 10 | numTasks = Integer.parseInt(args[0]); 11 | } 12 | 13 | List threads = new ArrayList<>(); 14 | 15 | for (int i = 0; i < numTasks; i++) { 16 | Thread thread = Thread.startVirtualThread(() -> { 17 | try { 18 | Thread.sleep(Duration.ofSeconds(10)); 19 | } catch (InterruptedException e) { 20 | // Handle exception 21 | } 22 | }); 23 | threads.add(thread); 24 | } 25 | 26 | // Wait for all threads to complete 27 | for (Thread thread : threads) { 28 | thread.join(); 29 | } 30 | 31 | System.out.println("All fibers complete"); 32 | } 33 | } 34 | --------------------------------------------------------------------------------