From 0b124ba50ce02e75a18ccdb034ee3e8c1eba8930 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Thu, 29 Aug 2024 18:49:11 +1000 Subject: [PATCH] Add a range of marshalling benchmarks --- src/Profile/MarshallingBenchmarks.cs | 53 ++++++++++++++++++++++++++- src/Profile/marshalling_benchmarks.py | 24 ++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/Profile/MarshallingBenchmarks.cs b/src/Profile/MarshallingBenchmarks.cs index a0965aec..e564ce66 100644 --- a/src/Profile/MarshallingBenchmarks.cs +++ b/src/Profile/MarshallingBenchmarks.cs @@ -1,7 +1,6 @@ using CSnakes.Runtime; using BenchmarkDotNet.Attributes; - namespace Profile; [MarkdownExporter] @@ -27,6 +26,58 @@ public void ComplexReturnLazy() mod!.GenerateDataAny(5, "hello", (3.2, "testinput"), false); } + [Benchmark] + public void FunctionReturnsList() + { + mod!.GenerateSequence(); + } + + [Benchmark] + public void FunctionTakesList() + { + List hundredNumbers = []; + for (int i = 0; i < 100; i++) + { + hundredNumbers.Add(i); + } + mod!.ConsumeSequence(hundredNumbers); + } + + [Benchmark] + public void FunctionTakesDictionary() + { + Dictionary hundredNumbers = []; + for (int i = 0; i < 100; i++) + { + hundredNumbers.Add(i.ToString(), i); + } + mod!.ConsumeDictionary(hundredNumbers); + } + + [Benchmark] + public void FunctionReturnsDictionary() + { + mod!.GenerateDictionary(); + } + + [Benchmark] + public void FunctionReturnsTuple() + { + mod!.GenerateTuple(); + } + + [Benchmark] + public void FunctionTakesTuple() + { + mod!.ConsumeTuple((5, "hello", 4.2, true)); + } + + [Benchmark] + public void FunctionTakesValueTypes() + { + mod!.ConsumeValueTypes(5, "hello", 4.2, true); + } + [Benchmark] public void EmptyFunction() { diff --git a/src/Profile/marshalling_benchmarks.py b/src/Profile/marshalling_benchmarks.py index 254e3610..f908b075 100644 --- a/src/Profile/marshalling_benchmarks.py +++ b/src/Profile/marshalling_benchmarks.py @@ -35,6 +35,30 @@ def generate_data_any(a: int, b: str, c: tuple[float, str], d: bool) -> Any: def empty_function() -> None: pass +def generate_sequence() -> list[int]: + return [i for i in range(100)] + +def consume_sequence(sequence: list[int]) -> None: + assert(isinstance(sequence, list) and len(sequence) == 100) + +def generate_sequence_any() -> Any: + return [i for i in range(100)] + +def consume_dictionary(data: dict[str, int]) -> None: + assert(isinstance(data, dict) and len(data) == 100) + +def generate_dictionary() -> dict[str, int]: + return {str(i): i for i in range(100)} + +def generate_tuple() -> tuple[int, str, float, bool]: + return (1, "test", 3.2, True) + +def consume_tuple(data: tuple[int, str, float, bool]) -> None: + assert(isinstance(data, tuple) and len(data) == 4) + +def consume_value_types(a: int, b: str, c: float, d: bool) -> None: + assert(isinstance(a, int) and isinstance(b, str) and isinstance(c, float) and isinstance(d, bool)) + if __name__ == "__main__": # Start timer import time