From 2095e4c4dc8c09d9521fa3d2c4a9ee621ce6283f Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 13:44:58 -0400 Subject: [PATCH 1/8] Make test.fib much more naive If we want to actually be able to use this to compare performance, we need a much more naive implementation. --- salt/modules/test.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/salt/modules/test.py b/salt/modules/test.py index 902c0af3e5..ad360f83a1 100644 --- a/salt/modules/test.py +++ b/salt/modules/test.py @@ -253,8 +253,10 @@ def arg_repr(*args, **kwargs): def fib(num): ''' - Return a Fibonacci sequence up to but not including the passed number, - and the time it took to compute in seconds. Used for performance tests. + Return the num-th Fibonacci number, and the time it took to compute in + seconds. Used for performance tests. + + This function is designed to have terrible performance. CLI Example: @@ -262,14 +264,13 @@ def fib(num): salt '*' test.fib 3 ''' - num = int(num) start = time.time() - fib_a, fib_b = 0, 1 - ret = [0] - while fib_b < num: - ret.append(fib_b) - fib_a, fib_b = fib_b, fib_a + fib_b - return ret, time.time() - start + if num < 2: + return num, time.time() - start + num1, _ = fib(num-1) + num2, _ = fib(num-2) + totaltime = time.time() - start + return num1 + num2, totaltime def collatz(start): From 27768fcf672d7e7a390da7b641e7b0911ec597d8 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 14:04:31 -0400 Subject: [PATCH 2/8] Get rid of timing delay by using helper method --- salt/modules/test.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/salt/modules/test.py b/salt/modules/test.py index ad360f83a1..7f95b23791 100644 --- a/salt/modules/test.py +++ b/salt/modules/test.py @@ -267,10 +267,15 @@ def fib(num): start = time.time() if num < 2: return num, time.time() - start - num1, _ = fib(num-1) - num2, _ = fib(num-2) - totaltime = time.time() - start - return num1 + num2, totaltime + return _fib(num-1) + _fib(num-2), time.time() - start + +def _fib(num): + ''' + Helper method for test.fib, doesn't calculate the time. + ''' + if num < 2: + return num + return _fib(num-1), _fib(num-2) def collatz(start): From 361f407d90ac2ab329c2dad4e798f16014731c31 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 14:06:32 -0400 Subject: [PATCH 3/8] Fix typo --- salt/modules/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/modules/test.py b/salt/modules/test.py index 7f95b23791..54a5d33b3b 100644 --- a/salt/modules/test.py +++ b/salt/modules/test.py @@ -275,7 +275,7 @@ def _fib(num): ''' if num < 2: return num - return _fib(num-1), _fib(num-2) + return _fib(num-1) + _fib(num-2) def collatz(start): From db76878903dc1b5cd09fee7806188566b5cc1c95 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 14:42:09 -0400 Subject: [PATCH 4/8] Fix lint --- salt/modules/test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/modules/test.py b/salt/modules/test.py index 54a5d33b3b..96ee68d7cf 100644 --- a/salt/modules/test.py +++ b/salt/modules/test.py @@ -269,6 +269,7 @@ def fib(num): return num, time.time() - start return _fib(num-1) + _fib(num-2), time.time() - start + def _fib(num): ''' Helper method for test.fib, doesn't calculate the time. From cc8785e98015effa7779802749250ba19eb7a2e0 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 14:48:48 -0400 Subject: [PATCH 5/8] Fix the test.fib tests --- tests/integration/client/syndic.py | 6 +++--- tests/integration/modules/publish.py | 4 ++-- tests/integration/modules/test.py | 6 +++--- tests/integration/shell/call.py | 9 +++------ 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/integration/client/syndic.py b/tests/integration/client/syndic.py index 63c2a1620b..8967453d2e 100644 --- a/tests/integration/client/syndic.py +++ b/tests/integration/client/syndic.py @@ -28,9 +28,9 @@ class TestSyndic(integration.SyndicCase): self.assertEqual( self.run_function( 'test.fib', - ['40'], - )[0][-1], - 34 + ['20'], + )[0], + 6765 ) diff --git a/tests/integration/modules/publish.py b/tests/integration/modules/publish.py index 9813ef5a39..3e6952199e 100644 --- a/tests/integration/modules/publish.py +++ b/tests/integration/modules/publish.py @@ -88,10 +88,10 @@ class PublishModuleTest(integration.ModuleCase, ''' ret = self.run_function( 'publish.full_data', - ['minion', 'test.fib', 40] + ['minion', 'test.fib', 20] ) self.assertTrue(ret) - self.assertEqual(ret['minion']['ret'][0][-1], 34) + self.assertEqual(ret['minion']['ret'][0], 6765) def test_kwarg(self): ''' diff --git a/tests/integration/modules/test.py b/tests/integration/modules/test.py index a768241ae7..b2b2f900af 100644 --- a/tests/integration/modules/test.py +++ b/tests/integration/modules/test.py @@ -73,9 +73,9 @@ class TestModuleTest(integration.ModuleCase, self.assertEqual( self.run_function( 'test.fib', - ['40'], - )[0][-1], - 34 + ['20'], + )[0], + 6765 ) def test_collatz(self): diff --git a/tests/integration/shell/call.py b/tests/integration/shell/call.py index b11f909187..6548ecf550 100644 --- a/tests/integration/shell/call.py +++ b/tests/integration/shell/call.py @@ -34,18 +34,15 @@ class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn): out = self.run_call('-l quiet test.fib 3') expect = ['local:', - ' |_', - ' - 0', - ' - 1', - ' - 1', - ' - 2'] + ' - 2', + ' - 3.09944152832e-06',] self.assertEqual(expect, out[:-1]) def test_text_output(self): out = self.run_call('-l quiet --out txt test.fib 3') expect = [ - 'local: ([0, 1, 1, 2]' + 'local: (2,' ] self.assertEqual(''.join(expect), ''.join(out).rsplit(",", 1)[0]) From 99744b9568219b618f9e1380514cbde90131f59c Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 15:56:09 -0400 Subject: [PATCH 6/8] Fix lint --- tests/integration/shell/call.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/shell/call.py b/tests/integration/shell/call.py index 6548ecf550..0e697ee9f0 100644 --- a/tests/integration/shell/call.py +++ b/tests/integration/shell/call.py @@ -35,7 +35,7 @@ class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn): expect = ['local:', ' - 2', - ' - 3.09944152832e-06',] + ' - 3.09944152832e-06'] self.assertEqual(expect, out[:-1]) def test_text_output(self): From 5b6d4101198144799b8c904e733811e01771c23a Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 15:58:27 -0400 Subject: [PATCH 7/8] Fix calltest --- tests/integration/shell/call.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/integration/shell/call.py b/tests/integration/shell/call.py index 0e697ee9f0..9365fa958f 100644 --- a/tests/integration/shell/call.py +++ b/tests/integration/shell/call.py @@ -34,15 +34,14 @@ class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn): out = self.run_call('-l quiet test.fib 3') expect = ['local:', - ' - 2', - ' - 3.09944152832e-06'] + ' - 2'] self.assertEqual(expect, out[:-1]) def test_text_output(self): out = self.run_call('-l quiet --out txt test.fib 3') expect = [ - 'local: (2,' + 'local: (2' ] self.assertEqual(''.join(expect), ''.join(out).rsplit(",", 1)[0]) From c4dd68bbc7165e824bae927e2edc4b6cbaaa656d Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Sat, 11 Apr 2015 16:07:59 -0400 Subject: [PATCH 8/8] Force num to int --- salt/modules/test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/modules/test.py b/salt/modules/test.py index 96ee68d7cf..38780f8085 100644 --- a/salt/modules/test.py +++ b/salt/modules/test.py @@ -264,6 +264,7 @@ def fib(num): salt '*' test.fib 3 ''' + num = int(num) start = time.time() if num < 2: return num, time.time() - start