Ejemplo n.º 1
0
        /// <summary>Runs the benchmark.</summary>
        public void Run()
        {
            if (_benchmark == null)
            {
                throw new InvalidOperationException("Call Init() first.");
            }

            using (BenchmarkHelpers.CaptureConsoleOutput(_output))
            {
                try
                {
                    FillProperties(_instance, _benchmark);

                    _output.WriteLine();
                    foreach (var infoLine in EnvironmentInfo.GetCurrent().ToFormattedString())
                    {
                        _output.WriteLine("// {0}", infoLine);
                    }
                    _output.WriteLine();

                    new MethodInvokerLight(_job).Invoke(_job, _operationsPerInvoke, _setupAction, _runCallback, _idleCallback);
                }
                catch (Exception ex)
                {
                    _output.WriteLine(ex);
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
            /// <summary>Saves modified files.</summary>
            public void Save()
            {
                if (!HasChanges)
                {
                    return;
                }

                foreach (var pair in _sourceLines)
                {
                    if (_changedFiles.Contains(pair.Key))
                    {
                        BenchmarkHelpers.WriteFileContent(pair.Key, pair.Value);
                    }
                }

                foreach (var pair in _xmlAnnotations)
                {
                    if (_changedFiles.Contains(pair.Key))
                    {
                        using (var writer = new StreamWriter(pair.Key))
                        {
                            XmlAnnotations.Save(pair.Value, writer);
                        }
                    }
                }
            }
Ejemplo n.º 3
0
            public void Save()
            {
                if (!HasChanges)
                {
                    return;
                }

                foreach (var pair in _sourceLines)
                {
                    if (_changedFiles.Contains(pair.Key))
                    {
                        BenchmarkHelpers.WriteFileContent(pair.Key, pair.Value);
                    }
                }

                var saveSettings = new XmlWriterSettings
                {
                    Indent      = true,
                    IndentChars = "\t"
                };

                foreach (var pair in _xmlAnnotations)
                {
                    if (_changedFiles.Contains(pair.Key))
                    {
                        using (var writer = XmlWriter.Create(pair.Key, saveSettings))
                            pair.Value.Save(writer);
                    }
                }
            }
Ejemplo n.º 4
0
        public static void TestFromTotalNanoseconds()
        {
            var twelveMs = TimeSpan.FromMilliseconds(12);

            AreEqual(twelveMs, BenchmarkHelpers.TimeSpanFromNanoseconds(12 * 1000 * 1000));
            AreEqual(twelveMs, BenchmarkHelpers.TimeSpanFromMicroseconds(12 * 1000));
        }
        public static void TestWriteFileContent()
        {
            Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;

            var expected = @"line1
line2
line3";

            var lines = new[]
            {
                "line1",
                "line2",
                "line3"
            };

            BenchmarkHelpers.WriteFileContent("deleteme.txt", lines);
            var allLines = File.ReadAllText("deleteme.txt");

            // File.WriteAllLines appends line break at the end.
            File.WriteAllLines("deleteme.txt", lines);
            var allLinesWithLineBreak = File.ReadAllText("deleteme.txt");

            File.Delete("deleteme.txt");

            AreEqual(allLines, expected);
            AreEqual(allLinesWithLineBreak, expected + "\r\n");
        }
Ejemplo n.º 6
0
        private static void Main()
        {
            Console.WindowWidth  = 135;
            Console.WindowHeight = 54;

            ConsoleCompetition.Run(typeof(ListCapacityPerfTest).Assembly);

            BenchmarkHelpers.ConsoleDoneWaitForConfirmation();
        }
Ejemplo n.º 7
0
        public void TestPercentile()
        {
            Assert.Throws <ArgumentNullException>(() => BenchmarkHelpers.Percentile(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => BenchmarkHelpers.Percentile(_data, -0.1));
            Assert.Throws <ArgumentOutOfRangeException>(() => BenchmarkHelpers.Percentile(_data, 1.1));

            Assert.AreEqual(BenchmarkHelpers.Percentile(new double[0], 0), 0);
            Assert.AreEqual(BenchmarkHelpers.Percentile(new double[0], 0.5), 0);
            Assert.AreEqual(BenchmarkHelpers.Percentile(new double[0], 1), 0);

            foreach (var pair in _expectedPercentiles)
            {
                Assert.AreEqual(BenchmarkHelpers.Percentile(_data, pair.Key), pair.Value, Delta);
            }
        }
            /// <inheritdoc/>
            protected override async Task <Tuple <decimal, string> > InvokeTestAsync(ExceptionAggregator aggregator)
            {
                var writer = new StringWriter();

                using (BenchmarkHelpers.CaptureConsoleOutput(writer))
                {
                    var result = await base.InvokeTestAsync(aggregator);

                    writer.Flush();
                    var output = writer.GetStringBuilder();
                    if (output.Length == 0)
                    {
                        return(result);
                    }
                    return(new Tuple <decimal, string>(result.Item1, result.Item2 + output));
                }
            }
Ejemplo n.º 9
0
        private static IConfig CreateRunConfig(IConfig config, CompetitionAnalyser runState, AccumulationLogger logger)
        {
            // TODO: better setup?
            var result = BenchmarkHelpers.CreateUnitTestConfig(config ?? DefaultConfig.Instance);

            result.Add(runState);
            result.Add(logger);
            result.Add(
                StatisticColumn.Min,
                ScaledPercentileColumn.S0Column,
                ScaledPercentileColumn.S50Column,
                ScaledPercentileColumn.S85Column,
                ScaledPercentileColumn.S95Column,
                ScaledPercentileColumn.S100Column,
                StatisticColumn.Max);
            return(result);
        }
        public static void TestTryGetTextFromUriFail()
        {
            Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;

            var webUrl =
                "https://gist.githubusercontent.com/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.badfile.txt";
            var webUrl2 =
                "https://gist.githubusercontent.baddomain/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.txt";
            var relativePath = "BadFile.txt";
            var fullPath     = Path.GetFullPath(relativePath);
            var fullPathUri  = new Uri(fullPath).ToString();

            IsNull(BenchmarkHelpers.TryGetTextFromUri(webUrl));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(webUrl2));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(relativePath));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(fullPath));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(fullPathUri));
        }
Ejemplo n.º 11
0
        /// <summary>Reads assembly level options config section.</summary>
        /// <param name="assembliesToCheck">Assemblies to check for the config section if the app.config does not contain the section.</param>
        /// <returns>
        /// Configuration section from the app.config, <paramref name="assembliesToCheck"/> or CodeJam.PerfTests assembly (first wins).
        /// </returns>
        public static AppConfigOptions GetAppConfigOptions(
            params Assembly[] assembliesToCheck)
        {
            var section = BenchmarkHelpers.ParseConfigurationSection <PerfTestsSection>(
                SectionName,
                assembliesToCheck.Concat(typeof(CompetitionRunnerBase).Assembly));

            return(section == null
                                ? null
                                : new AppConfigOptions
            {
                AnnotateOnRun = section.AnnotateOnRun,
                IgnoreExistingAnnotations = section.IgnoreExistingAnnotations,
                ReportWarningsAsErrors = section.ReportWarningsAsErrors,
                TroubleshootingMode = section.TroubleshootingMode,
                Loggers = section.Loggers,
                PreviousRunLogUri = section.PreviousRunLogUri,
                TargetPlatform = section.TargetPlatform
            });
        }
Ejemplo n.º 12
0
        private static bool RunHighestPriorityIfWindows([InstantHandle] Action callback)
        {
            if (!IsWindows())
            {
                return(false);
            }

            using (BenchmarkHelpers.SetupHighestPriorityScope(null, ConsoleLogger.Default))
            {
                try
                {
                    callback();
                    return(true);
                }
                catch (Win32Exception)
                {
                    return(false);
                }
            }
        }
        public static void TestTryGetTextFromUri()
        {
            Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;

            var webUrl =
                "https://gist.githubusercontent.com/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.txt";
            var relativePath = @"Assets\SampleFile.txt";
            var fullPath     = Path.GetFullPath(relativePath);
            var fullPathUri  = new Uri(fullPath).ToString();

            // ReSharper disable StringLiteralTypo
            var expectedText = @"A sample of text
Пример текста
テキストのサンプル";

            // ReSharper restore StringLiteralTypo
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(webUrl).ReadToEnd().Replace("\n", "\r\n"), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(relativePath).ReadToEnd(), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(fullPath).ReadToEnd(), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(fullPathUri).ReadToEnd(), expectedText);
        }
Ejemplo n.º 14
0
 public IReadOnlyDictionary <Type, CompetitionState> Run(
     [NotNull] Assembly assembly,
     [NotNull] ICompetitionConfig competitionConfig) =>
 Run(BenchmarkHelpers.GetBenchmarkTypes(assembly), competitionConfig);
Ejemplo n.º 15
0
 public IReadOnlyDictionary <Type, CompetitionState> Run(
     [NotNull] Assembly assembly,
     [CanBeNull] CompetitionFeatures competitionFeatures = null) =>
 Run(BenchmarkHelpers.GetBenchmarkTypes(assembly), competitionFeatures);
Ejemplo n.º 16
0
        public static XDocument[] TryParseXmlAnnotationDocsFromLog(
            [NotNull] string logUri,
            [NotNull] CompetitionState competitionState)
        {
            Code.NotNullNorEmpty(logUri, nameof(logUri));
            Code.NotNull(competitionState, nameof(competitionState));

            var result = new List <XDocument>();

            var logger = competitionState.Logger;

            logger.WriteLineInfo($"{LogVerbosePrefix} Downloading '{logUri}'.");

            using (var reader = BenchmarkHelpers.TryGetTextFromUri(logUri, TimeSpan.FromSeconds(15)))
            {
                if (reader == null)
                {
                    competitionState.WriteMessage(
                        MessageSource.Analyser, MessageSeverity.SetupError,
                        $"Could not load log content from '{logUri}'.");
                    return(Array <XDocument> .Empty);
                }

                logger.WriteLineInfo($"{LogVerbosePrefix} Downloaded '{logUri}'.");

                var    buffer = new StringBuilder();
                int    lineNumber = 0, xmlStartLineNumber = -1;
                string logLine;
                while ((logLine = reader.ReadLine()) != null)
                {
                    lineNumber++;

                    if (logLine.StartsWith(LogAnnotationStart, StringComparison.OrdinalIgnoreCase))
                    {
                        xmlStartLineNumber = lineNumber;
                    }
                    else if (logLine.StartsWith(LogAnnotationEnd, StringComparison.OrdinalIgnoreCase))
                    {
                        if (buffer.Length > 0)
                        {
                            var xmlAnnotationDoc = TryParseXmlAnnotationDocFromLogCore(
                                buffer.ToString(), competitionState, logUri, xmlStartLineNumber);
                            if (xmlAnnotationDoc != null)
                            {
                                result.Add(xmlAnnotationDoc);
                            }
                        }
                        buffer.Length = 0;

                        xmlStartLineNumber = -1;
                    }
                    else if (xmlStartLineNumber >= 0)
                    {
                        buffer.Append(logLine);
                    }
                }

                if (buffer.Length > 0)
                {
                    var xmlAnnotationDoc = TryParseXmlAnnotationDocFromLogCore(
                        buffer.ToString(), competitionState, logUri, xmlStartLineNumber);
                    if (xmlAnnotationDoc != null)
                    {
                        result.Add(xmlAnnotationDoc);
                    }
                }
            }

            return(result.ToArray());
        }
Ejemplo n.º 17
0
 public override void Setup()
 {
     BenchmarkHelpers.ClearRecords();
     BenchmarkHelpers.InsertRecords(50000);
 }