PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/Rhino.Etl.Tests/Branches/BranchesFixture.cs

http://github.com/ayende/rhino-etl
C# | 76 lines | 62 code | 14 blank | 0 comment | 0 complexity | b730e606ccac2d6f8e0565f91412d858 MD5 | raw file
 1using System;
 2using Rhino.Etl.Core;
 3using Xunit;
 4using Rhino.Etl.Core.Infrastructure;
 5
 6namespace Rhino.Etl.Tests.Branches
 7{
 8    public abstract class BranchesFixture : BaseFibonacciTest
 9    {
10        [Fact]
11        public void CanBranchThePipeline()
12        {
13            using (var process = CreateBranchingProcess(30, 2))
14                process.Execute();
15
16            AssertFibonacci(30, 2);
17        }
18
19        [Fact] 
20        public void CanBranchThePipelineEfficiently()
21        {
22            const int iterations = 30000;
23            const int childOperations = 10;
24
25            var initialMemory = GC.GetTotalMemory(true);
26
27            using (var process = CreateBranchingProcess(iterations, childOperations))
28                process.Execute();
29
30            var finalMemory = GC.GetTotalMemory(true);
31            var consumedMemory = finalMemory - initialMemory;
32            var tooMuchMemory = Math.Pow(2, 20);
33            
34            Assert.True(consumedMemory < tooMuchMemory, "Consuming too much memory - (" + consumedMemory.ToString() + " >= " + tooMuchMemory + ")");
35            AssertFibonacci(iterations, childOperations);
36        }
37
38        protected abstract EtlProcess CreateBranchingProcess(int iterations, int childOperations);
39
40        protected static void AssertFibonacci(int iterations, int repetitionsPerIteration)
41        {
42            AssertTotalItems(iterations * repetitionsPerIteration);
43
44            AssertRepetitions(repetitionsPerIteration);
45        }
46
47        private static void AssertRepetitions(int repetitionsPerIteration)
48        {
49            int wrongRepetitions = Use.Transaction("test", cmd =>
50            {
51                cmd.CommandText =
52string.Format(@"    SELECT count(*) 
53    FROM (
54        SELECT id, count(*) as count
55        FROM Fibonacci
56        GROUP BY id
57        HAVING count(*) <> {0}
58    ) as ignored", repetitionsPerIteration);
59                return (int)cmd.ExecuteScalar();
60            });
61
62            Assert.Equal(1 /* 1 is repetated twice the others */, wrongRepetitions);
63        }
64
65        private static void AssertTotalItems(int expectedCount)
66        {
67            int totalCount = Use.Transaction("test", cmd =>
68                                                     {
69                                                         cmd.CommandText = "SELECT count(*) FROM Fibonacci";
70                                                         return (int) cmd.ExecuteScalar();
71                                                     });
72            
73            Assert.Equal(expectedCount, totalCount);
74        }
75    }
76}