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); }
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(); } }