Browse Source

Define a fibonacci perf test

jonathanvdc 8 years ago
parent
commit
81eb0a5c97

+ 11 - 0
performance/code/fibonacci.alc

@@ -0,0 +1,11 @@
+include "primitives.alh"
+
+Integer function fib(param : Integer):
+	if (param <= 2):
+		return 1!
+	else:
+		return fib(param - 1) + fib(param - 2)!
+
+Void function test_main():
+	fib(input())
+	return!

+ 16 - 0
performance/code/test_harness.alc

@@ -0,0 +1,16 @@
+include "primitives.alh"
+
+Void function test_main()
+
+Void function main():
+	Integer start_time
+	Integer end_time
+	start_time = time()
+	test_main()
+	end_time = time()
+	output(end_time - start_time)
+	
+	while (True):
+		output(input())
+	
+	return!

+ 17 - 0
performance/test_fibonacci.py

@@ -0,0 +1,17 @@
+import unittest
+import utils
+
+
+class TestFibonacci(unittest.TestCase):
+    def test_po_fibonacci(self):
+        self.fibonacci("PO")
+
+    def test_co_fibonacci(self):
+        self.fibonacci("CO")
+
+    def fibonacci(self, mode):
+        print(
+            utils.run_file_single_output(
+                ["test_harness.alc", "fibonacci.alc", "primitives.alc"],
+                [20, 0],
+                mode))

+ 25 - 1
performance/utils.py

@@ -191,7 +191,7 @@ def run_file(files, parameters, mode, handle_output):
 
 def run_file_to_completion(files, parameters, mode):
     """Compiles the given sequence of files, feeds them the given input in the given mode,
-       then collects and returns their output."""
+       and then collects and returns output."""
     results = []
     def handle_output(output):
         """Appends the given output to the list of results."""
@@ -202,3 +202,27 @@ def run_file_to_completion(files, parameters, mode):
         run_file(files, parameters, mode, handle_output)
     except ModelverseTerminated:
         return results
+
+def run_file_fixed_output_count(files, parameters, mode, output_count):
+    """Compiles the given sequence of files, feeds them the given input in the given mode,
+       and then collects and returns a fixed number of outputs."""
+    results = []
+    def handle_output(output):
+        """Appends the given output to the list of results."""
+        if len(results) < output_count:
+            results.append(output)
+            return True
+        else:
+            return False
+
+    run_file(files, parameters, mode, handle_output)
+    return results
+
+def run_file_single_output(files, parameters, mode):
+    """Compiles the given sequence of files, feeds them the given input in the given mode,
+       and then collects and returns a single output."""
+    return run_file_fixed_output_count(files, parameters, mode, 1)[0]
+
+def format_output(output):
+    """Formats the output of `run_file_to_completion` as a string."""
+    return '\n'.join(output)