public static void ComparePerformanceOfCachedAndOnTheFlySegmentStateCalculators(
            string logFilePath, int repetitions, BitMatrix board, Matrix <Cell> cellMatrix,
            Matrix <SegmentState> vertSegStateMatrix, Matrix <SegmentState> horizSegStateMatrix, Matrix <bool> boolMatrix)
        {
            // Cache based segment state calculator:
            CacheBasedSegmentStateCalculator cacheSegStateCalculator = new CacheBasedSegmentStateCalculator(horizSegStateMatrix, vertSegStateMatrix);

            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using cache calculator", repetitions,
                                                         Tuple.Create(board.BottomRight, cacheSegStateCalculator),
                                                         CalculateSegmentStatesUsingCache);

            // Calculation based segment state calculator:
            CalculationBasedSegmentStateCalculator calcSegStateCalculator = new CalculationBasedSegmentStateCalculator(board);

            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using direct BitMatrix-based calculator", repetitions,
                                                         Tuple.Create(board.BottomRight, calcSegStateCalculator),
                                                         CalculateSegmentStatesUsingCalculationBasedCalculator);

            // Bool Matrix based on the fly segment state calculator:
            OnTheFlyBoolMatrixBasedSegmentStateCalculator boolMatrixBasedSegStateCalculator
                = new OnTheFlyBoolMatrixBasedSegmentStateCalculator(boolMatrix);

            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using direct bool matrix based calculator", repetitions,
                                                         Tuple.Create(boolMatrix.BottomRight, boolMatrixBasedSegStateCalculator),
                                                         CalculateSegmentStatesUsingOnTheFlyBoolBoolBasedCalculator);

            CellMatrixBasedSegmentStateCalculator cellCacheSegStateCalculator
                = new CellMatrixBasedSegmentStateCalculator(board, cellMatrix);

            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate all segments using cell matrix-based cache calculator",
                                                         repetitions, Tuple.Create(board.BottomRight, cellCacheSegStateCalculator),
                                                         CalculateSegmentStatesUsingCellCacheBasedCalculator);

            /* Compare segment state calculation times over random points for cached and calculated segment state calculators,
             * to make sure the cache calculator is not just benefiting from memory cache effects due to the sequence of points:
             */
            Core.Point[] randomPoints = DataGenerator.GenerateRandomPoints(board.TopLeft, board.BottomRight, 10000);

            // Cache based segment state calculator:
            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using cache calculator", repetitions,
                                                         Tuple.Create(randomPoints, cacheSegStateCalculator),
                                                         CalculateSegmentStatesOnRandomPointsUsingCache);

            PerformanceTestHelper.TimeActionWithArgument(logFilePath, "Calculate segments on random points using cell matrix-based cache calculator", repetitions,
                                                         Tuple.Create(randomPoints, cellCacheSegStateCalculator),
                                                         CalculateSegmentStatesOnRandomPointsUsingCache);

            // Calculation based segment state calculator:
            PerformanceTestHelper.TimeActionWithArgument(logFilePath,
                                                         "Calculate segments on random points using on the fly calculator", repetitions,
                                                         Tuple.Create(randomPoints, calcSegStateCalculator),
                                                         CalculateSegmentStatesOnRandomPointsUsingCalculationBasedCalculator);

            // Bool Matrix based on the fly segment state calculator:
            PerformanceTestHelper.TimeActionWithArgument(logFilePath,
                                                         "Calculate segments on random points using on the fly bool matrix based calculator",
                                                         repetitions, Tuple.Create(randomPoints, boolMatrixBasedSegStateCalculator),
                                                         CalculateSegmentStatesOnRandomPointsUsingOnTheFlyBoolBoolBasedCalculator);
        }
        public static void TestBitMatrix(string logFilePath, int repetitions)
        {
            string bitMatrixType = "BitMatrix using an int array";
            string title;

            title = String.Format("Construct and populate a {0}", bitMatrixType);
            BitMatrix bm = PerformanceTestHelper.TimeFunction(logFilePath, title, repetitions, ConstructBitMatrix);

            title = String.Format("Read a {0}", bitMatrixType);
            PerformanceTestHelper.TimeActionWithArgument(logFilePath, title, repetitions, bm, ReadBitMatrix);

            title = String.Format("Clone a {0}", bitMatrixType);
            PerformanceTestHelper.TimeActionWithArgument(logFilePath, title, repetitions, bm, CloneBitMatrix);
        }
예제 #3
0
        public void TestDistanceCalculator(string logFilePath, int repetitions, int smallRepetitions)
        {
            SegStateCalculator = new CacheBasedSegmentStateCalculator(HorizSegmentStateMatrix, VertSegmentStateMatrix);
            TankEdgeMatrix     = PerformanceTestHelper.TimeFunction(
                logFilePath, "Calculate tank edge matrix using TankEdgeCalculator", repetitions,
                CalculateTankOuterEdgeMatrix);

            MobileState tankState1 = new MobileState(new Point(20, 6), Direction.UP, isActive: true);

            DistancesFromTank1 = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath, "Test Distance Calculator for tank 1",
                                                                                smallRepetitions, tankState1, CalculateDistancesForTank);

            MobileState tankState2 = new MobileState(new Point(56, 6), Direction.UP, isActive: true);

            DistancesFromTank2 = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath, "Test Distance Calculator for tank 2",
                                                                                smallRepetitions, tankState2, CalculateDistancesForTank);
        }
        public void Test(int repetitions, int smallRepetitions, string logFilePath)
        {
            VerticalSegmentStateMatrix
                = PerformanceTestHelper.TimeFunction(logFilePath,
                                                     "Test calculation of vertical segment state matrix directly from the BitMatrix",
                                                     repetitions, GetVerticalSegmentStateMatrix);

            // Save image for vertical segment state matrix:
            Bitmap segStateBitmap = ImageGenerator.GenerateBoardImage(Board);

            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, VerticalSegmentStateMatrix, Axis.Vertical);
            string segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\VertSegmentMatrix.bmp";

            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);

            HorizontalSegStateMatrix
                = PerformanceTestHelper.TimeFunction(logFilePath,
                                                     "Test calculation of horizontal segment state matrix directly from the BitMatrix",
                                                     repetitions, GetHorizontalSegmentStateMatrix);

            // Save image for horizontal segment state matrix:
            segStateBitmap = ImageGenerator.GenerateBoardImage(Board);
            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, HorizontalSegStateMatrix, Axis.Horizontal);
            segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\HorizSegmentMatrix.bmp";
            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);

            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, VerticalSegmentStateMatrix, Axis.Vertical);
            segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\BiDiSegmentMatrix.bmp";
            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);

            // Test calculation caches:
            PerformanceTestHelper.TimeAction(logFilePath,
                                             "Time cell calculator on challenge board 1", smallRepetitions,
                                             PerformCellCalculation);
            CellMatrix = PerformanceTestHelper.TimeFunction(logFilePath,
                                                            "Time cell and segment calculator on challenge board 1",
                                                            smallRepetitions, PerformCellAndSegmentCalculation);

            // Repeat segment stage calculations using implementation based on pre-calculated cell and segment calculations:
            VerticalSegmentStateMatrix = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath,
                                                                                        "Test calculation of vertical segment state matrix using a cell matrix",
                                                                                        repetitions, Axis.Vertical, GetSegmentStateMatrixUsingCellMatrix);

            // Save image for vertical segment state matrix:
            segStateBitmap = ImageGenerator.GenerateBoardImage(Board);
            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, VerticalSegmentStateMatrix, Axis.Vertical);
            segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\VertSegmentMatrixUsingCellMatrix.bmp";
            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);

            HorizontalSegStateMatrix = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath,
                                                                                      "Test calculation of horizontal segment state matrix using a cell matrix",
                                                                                      repetitions, Axis.Horizontal, GetSegmentStateMatrixUsingCellMatrix);

            // Repeat segment state calculation using Segment matrix calculated from the Cell matrix:
            Matrix <Segment> vertSegmentMatrix = SegmentCalculator.GetSegmentMatrix(CellMatrix, Board, Axis.Vertical);

            VerticalSegmentStateMatrix = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath,
                                                                                        "Test calculation of vertical segment state matrix using a segment matrix",
                                                                                        repetitions, Tuple.Create(vertSegmentMatrix, Board),
                                                                                        (tuple) => SegmentCalculator.GetBoardSegmentStateMatrixFromSegmentMatrix(tuple.Item1, tuple.Item2));

            Matrix <Segment> horizSegmentMatrix = SegmentCalculator.GetSegmentMatrix(CellMatrix, Board, Axis.Horizontal);

            HorizontalSegStateMatrix = PerformanceTestHelper.TimeFunctionWithArgument(logFilePath,
                                                                                      "Test calculation of vertical segment state matrix using a segment matrix",
                                                                                      repetitions, Tuple.Create(horizSegmentMatrix, Board),
                                                                                      (tuple) => SegmentCalculator.GetBoardSegmentStateMatrixFromSegmentMatrix(tuple.Item1, tuple.Item2));

            // Save image for horizontal segment state matrix:
            segStateBitmap = ImageGenerator.GenerateBoardImage(Board);
            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, HorizontalSegStateMatrix, Axis.Horizontal);
            segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\HorizSegmentMatrixUsingCellMatrix.bmp";
            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);

            ImageGenerator.DrawSegmentMatrixOverlay(segStateBitmap, Board, VerticalSegmentStateMatrix, Axis.Vertical);
            segmentMatrixFilePath = @"c:\Competitions\EntelectChallenge2013\temp\BiDiSegmentMatrixUsingCellMatrix.bmp";
            segStateBitmap.Save(segmentMatrixFilePath, ImageFormat.Bmp);
        }
예제 #5
0
        static void Main(string[] args)
        {
            try
            {
                int repetitions      = 1000;
                int smallRepetitions = 10;

                if (args.Length > 0)
                {
                    int.TryParse(args[0], out repetitions);
                    if (args.Length > 1)
                    {
                        int.TryParse(args[1], out smallRepetitions);
                    }
                }

                string logFilePath = String.Format(
                    @"C:\Competitions\EntelectChallenge2013\temp\PerformanceStats\PerfStats_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd_HHmmss"));

                // Test reading of JSon file:
                ImageGenerator imageGen = new ImageGenerator {
                    Magnification = 2
                };
                BitMatrix board = TestReadingJsonBoardFile(imageGen);

                // ======================
                // Run performance tests:
                Console.WriteLine("Add any comments about this test run:");
                string comments = Console.ReadLine();
                PerformanceTestHelper.WriteCommentsToLog(logFilePath, comments);

                SegmentStateMatrixTester segmentStateMatrixTester = new SegmentStateMatrixTester
                {
                    Board          = board,
                    ImageGenerator = imageGen
                };
                segmentStateMatrixTester.Test(repetitions, smallRepetitions, logFilePath);

                Matrix <SegmentState> vertSegStateMatrix  = segmentStateMatrixTester.VerticalSegmentStateMatrix;
                Matrix <SegmentState> horizSegStateMatrix = segmentStateMatrixTester.HorizontalSegStateMatrix;
                Matrix <Cell>         cellMatrix          = segmentStateMatrixTester.CellMatrix;

                DistanceCalculationTester distCalcTester = new DistanceCalculationTester
                {
                    Board = board,
                    HorizSegmentStateMatrix = horizSegStateMatrix,
                    VertSegmentStateMatrix  = vertSegStateMatrix,
                    CellMatrix = cellMatrix
                };
                distCalcTester.TestDistanceCalculator(logFilePath, repetitions, smallRepetitions);

                // Test time to generate a boolean matrix from a bit matrix:
                Matrix <bool> boolMatrix = PerformanceTestHelper.TimeFunctionWithArgument(
                    logFilePath, "Convert a bit matrix to a bool matrix",
                    repetitions, board, b => b.ConvertToBoolMatrix());

                /* Compare segment state calculation times over the whole board for cached and calculated segment state calculators: */
                SegmentStateCalculationTester.ComparePerformanceOfCachedAndOnTheFlySegmentStateCalculators(
                    logFilePath, repetitions, board, cellMatrix,
                    vertSegStateMatrix, horizSegStateMatrix, boolMatrix);

                // Test construction time for a BitMatrix:
                BitMatrixTester.TestBitMatrix(logFilePath, repetitions);
            }
            catch (Exception exc)
            {
                Console.WriteLine("An unexpected error occurred: {0}", exc);
                Console.WriteLine();
                Console.WriteLine("Stack trace:");
                Console.WriteLine(exc.StackTrace);
                Console.WriteLine();
            }
        }