Exemplo n.º 1
0
        internal static void TestAlgorithms(int from, int to, int size, StartConditions.SeriesKind seriesKind, LogWriter logWriter, int currentLoops, List <TimeSpan> timeSpanAlg1, List <TimeSpan> timeSpanAlg2, List <string> resultsAlg1, List <string> resultsAlg2, CancellationToken ct)
        {
            int[] numbers = null;


            switch (seriesKind)
            {
            case StartConditions.SeriesKind.naturalSeries:
                numbers = StartConditions.GenerateNaturalSeries(from, to, size);
                break;

            case StartConditions.SeriesKind.randomSeries:
                numbers = StartConditions.GenerateRandomSeries(from, to, size);
                break;

            case StartConditions.SeriesKind.shuffledNaturalSeries:
                numbers = StartConditions.GenerateShuffledNaturalSeries(from, to, size);
                break;

            default:
                break;
            }
            // Create array and sum
            int sum = StartConditions.GenerateSum(from, to, size);

            // assembly and class name are supposed to be the same
            const string alg1Name       = "DmitryAlgorithm";
            const string alg2Name       = "AleksAlgorithm";
            const string mainMethodName = "GetIndexes";

            Thread thread1 = new Thread(() =>
                                        SafeExecute(() =>
                                                    RunAlgorithm.Run(numbers, sum, alg1Name, mainMethodName, timeSpanAlg1, resultsAlg1, logWriter),
                                                    Handler));

            thread1.IsBackground = true;

            Thread thread2 = new Thread(() =>
                                        SafeExecute(() =>
                                                    RunAlgorithm.Run(numbers, sum, alg2Name, mainMethodName, timeSpanAlg2, resultsAlg2, logWriter),
                                                    Handler));

            thread2.IsBackground = true;

            // In case of exception thread is closed, but test method continues, just prints the message.
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            string report = "";

            report += $"Timespans for algoritm {alg1Name} & {alg2Name}";
            for (int i = 0; i < currentLoops; i++)
            {
                report += $"\n{timeSpanAlg1[i].TotalMilliseconds} ms, Result: {resultsAlg1[i]}";
                report += $"\n{timeSpanAlg2[i].TotalMilliseconds} ms, Result: {resultsAlg2[i]}";
            }
            logWriter.Write(report);
        }
Exemplo n.º 2
0
        private static void StartTesting(int from, int to, int size, CancellationToken ct)
        {
            ct.Register(() =>
            {
                return;
            });

            string logPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string logName = "log.txt";

            // clear log from previous run info
            File.WriteAllText(Path.Combine(logPath, logName), string.Empty);

            LogWriter logWriter = new LogWriter(logPath, logName);

            int             totalLoops   = 3;
            int             currentLoops = 0;
            List <TimeSpan> timeSpanAlg1 = new List <TimeSpan>();
            List <TimeSpan> timeSpanAlg2 = new List <TimeSpan>();
            List <string>   resultsAlg1  = new List <string>();
            List <string>   resultsAlg2  = new List <string>();

            Random rnd = new Random();

            for (int i = 0; i < totalLoops; i++)
            {
                // call algorithms with different types of incoming arrays: natural series or random series
                logWriter.Write($"Start generating numbers: {DateTime.Now.ToLongTimeString()}");

                //StartConditions.SeriesKind seriesKind =
                //(StartConditions.SeriesKind)rnd.Next(0, Enum.GetNames(typeof(StartConditions.SeriesKind)).Length);
                StartConditions.SeriesKind seriesKind = (StartConditions.SeriesKind)i;

                Console.WriteLine($"\n===========================================");
                Console.WriteLine($"\nRun number {i+1}. Series kind: {seriesKind}\n");

                currentLoops++;

                // testing algorithms
                logWriter.Write($"Start testing algorithms: {DateTime.Now.ToLongTimeString()}");
                Testing.TestAlgorithms(from, to, size, seriesKind, logWriter, currentLoops, timeSpanAlg1, timeSpanAlg2, resultsAlg1, resultsAlg2, ct);
            }

            Console.WriteLine($"\n================================");
            Console.WriteLine($"\nAlgorithm testing has completed. Press any key to exit");
            Console.WriteLine($"================================");
        }