コード例 #1
0
    public static async Task AsyncMain()
    {
        int a       = 500;
        int b       = 10;
        var results = await TimeMeasurer.Start(() => Function(a, b));

        Console.WriteLine("Waited: {0}", results.Duration.TotalSeconds);
        Console.WriteLine("Result: {0}", results.Result);
    }
コード例 #2
0
        public void Measurer_Test()
        {
            TimeMeasurer _timeMeasurer = new TimeMeasurer();

            _timeMeasurer.Start();
            _timeMeasurer.Stop();
            if (_timeMeasurer.AverageTime != null && _timeMeasurer.AverageTime.HasValue)
            {
                Assert.LessOrEqual(0, _timeMeasurer.AverageTime.Value);
            }
            _timeMeasurer.Reset();
            Assert.IsNull(_timeMeasurer.AverageTime);
        }
コード例 #3
0
        /**
         * Overloaded method. Note that the last parameter can be null if we only want to compare
         * results from different implementations.
         * The second last parameter is used when we also have an expected path retrieved for example from an xml file.
         * The last parameter can be used temporarirly for "debugging" purposes when we want to display the results to the console
         * See comment at class level.
         */
        public void AssertExpectedResultsOrAssertImplementationsWithEachOther(
            IList <Edge> edgesForBigGraph,
            Vertex startVertex,
            Vertex endVertex,
            int numberOfPathsToFind,
            IList <PathFinderFactory> pathFinderFactoriesForImplementationsToTest,
            IList <Path> expectedListOfPaths,
            string optionalPathToResourceXmlFile,
            bool shouldTestResultsWithImplementationsAgainstEachOther = false
            )
        {
            // TODO: clean up this method e.g. regarding "shouldAlsoTestResultsWithImplementationsAgainstEachOther"
            //this.SetConsoleOutputDesired(ConsoleOutputDesired.TIME_MEASURE);
            string messagePrefixWithInformationAboutXmlSourcefileWithTestData = optionalPathToResourceXmlFile == null ? "" : "Xml file with test data: " + optionalPathToResourceXmlFile + " . ";

            output("Number of edges in the graph to be tested : " + edgesForBigGraph.Count);
            IDictionary <string, IList <Path> > shortestPathsPerImplementation = new Dictionary <string, IList <Path> >();

            // the parameter GraphEdgesValidationDesired.NO will be used so therefore do the validation once externally here first
            GraphEdgesValidator <Path, Edge, Vertex, Weight> .ValidateEdgesForGraphCreation <Path, Edge, Vertex, Weight>(edgesForBigGraph);

            PathParser <Path, Edge, Vertex, Weight> pathParser = PathParser <Path, Edge, Vertex, Weight> .CreatePathParserDefault(edgesForBigGraph);

            //assertThat("At least some implementation should be used", pathFinderFactoriesForImplementationsToTest.size(), greaterThanOrEqualTo(1));
            // TODO: "hamcrest" syntax similar to above java code
            Assert.That(pathFinderFactoriesForImplementationsToTest.Count >= 1, "At least some implementation should be used");
            for (int i = 0; i < pathFinderFactoriesForImplementationsToTest.Count; i++)
            {
                PathFinderFactory pathFinderFactory = pathFinderFactoriesForImplementationsToTest[i];
                output("Will now test file " + optionalPathToResourceXmlFile + " with impl " + pathFinderFactory.GetType().Name);
                TimeMeasurer tm         = TimeMeasurer.Start();
                PathFinder   pathFinder = pathFinderFactory.CreatePathFinder(
                    edgesForBigGraph,
                    GraphEdgesValidationDesired.NO                 // do the validation one time instead of doing it for each pathFinderFactory
                    );
                IList <Path> shortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, numberOfPathsToFind);
                Assert.IsNotNull(shortestPaths);
                //assertThat("At least some path should be found", shortestPaths.size(), greaterThanOrEqualTo(1));
                Assert.That(shortestPaths.Count >= 1, "At least some path should be found"); // TODO "hamcrest" syntax as java above
                output(
                    messagePrefixWithInformationAboutXmlSourcefileWithTestData
                    + "Seconds: " + tm.GetSeconds()
                    + ". Implementation: " + pathFinder.GetType().Name,
                    ConsoleOutputDesired.TIME_MEASURE
                    );
                if (isAllConsoleOutputDesired())
                {
                    DisplayListOfShortestPath(shortestPaths);
                    output("Implementation class for above and below output: " + pathFinderFactory.GetType().Name);
                    DisplayAsPathStringsWhichCanBeUsedInXml(shortestPaths, pathParser);
                }
                shortestPathsPerImplementation.Add(pathFinder.GetType().Name, shortestPaths);
            }
            IList <string> nameOfImplementations = new List <string>(shortestPathsPerImplementation.Keys);

            if (expectedListOfPaths != null)
            {
                AssertResultsWithExpectedPaths(
                    expectedListOfPaths,
                    optionalPathToResourceXmlFile,
                    shortestPathsPerImplementation
                    );
            }
            else // if(expectedListOfPaths != null) {
            if (shouldTestResultsWithImplementationsAgainstEachOther)
            {
                AssertResultsWithImplementationsAgainstEachOther(
                    optionalPathToResourceXmlFile,
                    shortestPathsPerImplementation
                    );
            }
        }