From 672b89fe47853b93fc208b870424e045a5a0e5ce Mon Sep 17 00:00:00 2001
From: Martin Lange <martin.lange@ufz.de>
Date: Wed, 7 Dec 2022 22:18:41 +0100
Subject: [PATCH] add benchmarks for file-based data in outputs

---
 benchmarks/README.md      |  9 ++++---
 benchmarks/sdk/test_io.py | 52 +++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/benchmarks/README.md b/benchmarks/README.md
index 2c6828b9..0f2f9cd3 100644
--- a/benchmarks/README.md
+++ b/benchmarks/README.md
@@ -28,10 +28,13 @@ Groups left to right:
 
 ### Push & pull
 
-Push & pull using numpy arrays (`np`) and xarray arrays (`xr`).  
-(xarray benchmarks include a call to `fm.tools.assign_time`)
+Push & pull using numpy arrays, with and without units conversion. 
 
-![tools](https://git.ufz.de/FINAM/finam/-/jobs/artifacts/main/raw/bench/bench-sdk-io.svg?job=benchmark)
+![sdk-io](https://git.ufz.de/FINAM/finam/-/jobs/artifacts/main/raw/bench/bench-sdk-io.svg?job=benchmark)
+
+Push & pull using zero memory limit. I.e. everything written to and re-read from file.
+
+![sdk-io-mem](https://git.ufz.de/FINAM/finam/-/jobs/artifacts/main/raw/bench/bench-sdk-io-mem.svg?job=benchmark)
 
 ## Data
 
diff --git a/benchmarks/sdk/test_io.py b/benchmarks/sdk/test_io.py
index 3b294445..d9b20453 100644
--- a/benchmarks/sdk/test_io.py
+++ b/benchmarks/sdk/test_io.py
@@ -1,4 +1,5 @@
 import datetime as dt
+import tempfile
 import unittest
 
 import pytest
@@ -6,12 +7,7 @@ import pytest
 import finam as fm
 
 
-class TestPushPull(unittest.TestCase):
-    @pytest.fixture(autouse=True)
-    def setupBenchmark(self, benchmark):
-        self.benchmark = benchmark
-        self.counter = 0
-
+class TestPushPullBase(unittest.TestCase):
     def push_pull(self):
         # Trick the shared memory check in the output
         data = self.data[self.counter % 2]
@@ -22,7 +18,7 @@ class TestPushPull(unittest.TestCase):
         self.counter += 1
         return data
 
-    def setup_link(self, grid, target_units):
+    def setup_link(self, grid, target_units, memory_limit=None, tempdir=None):
         self.time = dt.datetime(2000, 1, 1)
         info1 = fm.Info(time=self.time, grid=grid, units="mm")
         info2 = fm.Info(time=self.time, grid=grid, units=target_units)
@@ -35,11 +31,21 @@ class TestPushPull(unittest.TestCase):
         self.out = fm.Output(name="Output")
         self.inp = fm.Input(name="Input")
 
+        self.out.memory_limit = memory_limit
+        self.out.memory_location = tempdir
+
         self.out >> self.inp
         self.inp.ping()
         self.out.push_info(info1)
         self.inp.exchange_info(info2)
 
+
+class TestPushPull(TestPushPullBase):
+    @pytest.fixture(autouse=True)
+    def setupBenchmark(self, benchmark):
+        self.benchmark = benchmark
+        self.counter = 0
+
     @pytest.mark.benchmark(group="sdk-io")
     def test_push_pull_np_01_2x1(self):
         grid = fm.UniformGrid((2, 1))
@@ -123,3 +129,35 @@ class TestPushPull(unittest.TestCase):
         self.setup_link(grid, target_units="L/m^2")
         data = self.benchmark(self.push_pull)
         self.assertEqual(fm.UNITS.Unit("L/m^2"), data.units)
+
+    @pytest.mark.benchmark(group="sdk-io-mem")
+    def test_push_pull_file_01_2x1(self):
+        grid = fm.UniformGrid((2, 1))
+        with tempfile.TemporaryDirectory() as td:
+            self.setup_link(grid, target_units="m", memory_limit=0, tempdir=td)
+            self.benchmark(self.push_pull)
+            self.out.finalize()
+
+    @pytest.mark.benchmark(group="sdk-io-mem")
+    def test_push_pull_file_02_512x256(self):
+        grid = fm.UniformGrid((512, 256))
+        with tempfile.TemporaryDirectory() as td:
+            self.setup_link(grid, target_units="m", memory_limit=0, tempdir=td)
+            self.benchmark(self.push_pull)
+            self.out.finalize()
+
+    @pytest.mark.benchmark(group="sdk-io-mem")
+    def test_push_pull_file_03_1024x512(self):
+        grid = fm.UniformGrid((1024, 512))
+        with tempfile.TemporaryDirectory() as td:
+            self.setup_link(grid, target_units="m", memory_limit=0, tempdir=td)
+            self.benchmark(self.push_pull)
+            self.out.finalize()
+
+    @pytest.mark.benchmark(group="sdk-io-mem")
+    def test_push_pull_file_04_2048x1024(self):
+        grid = fm.UniformGrid((2048, 1024))
+        with tempfile.TemporaryDirectory() as td:
+            self.setup_link(grid, target_units="m", memory_limit=0, tempdir=td)
+            self.benchmark(self.push_pull)
+            self.out.finalize()
-- 
GitLab