Exemplo n.º 1
0
        /// <summary>Saves stored metrics from competition targets.</summary>
        /// <param name="competitionTargets">Competition targets with metrics to save.</param>
        /// <param name="annotationContext">The annotation context.</param>
        /// <param name="analysis">State of the analysis.</param>
        /// <returns>
        /// <c>true</c>, if metrics were saved successfully.
        /// </returns>
        protected override CompetitionTarget[] TrySaveAnnotationsCore(
            IReadOnlyCollection <CompetitionTarget> competitionTargets, AnnotationContext annotationContext, Analysis analysis)
        {
            var benchmarkType = analysis.RunState.BenchmarkType;
            var targetKey     = new AnnotationTargetKey(benchmarkType.TypeHandle);

            var annotationFile = annotationContext.TryGetDocument(targetKey);

            if (annotationFile == null)
            {
                var origin = TryGetAnnotationLocation(analysis.Summary, analysis);
                if (origin == null)
                {
                    annotationFile = annotationContext.GetUnknownOriginDocument();
                }
                else
                {
                    annotationFile = annotationContext.TryGetDocument(origin);
                    if (annotationFile == null)
                    {
                        annotationFile = ParseAnnotationFile(benchmarkType, origin, analysis);
                        annotationContext.AddDocument(annotationFile);
                    }
                }
                annotationContext.AddTargetKey(annotationFile, targetKey);
            }

            if (!annotationFile.Parsed)
            {
                analysis.WriteSetupErrorMessage(
                    $"Could not find XML annotation file {annotationFile.Origin} for the benchmark. Annotations were not saved.");
                return(Array <CompetitionTarget> .Empty);
            }

            var result = new List <CompetitionTarget>();

            var xmlAnnotationFile = (XmlAnnotationFile)annotationFile;

            foreach (var targetToAnnotate in competitionTargets)
            {
                var target = targetToAnnotate.Target;

                var metrics = targetToAnnotate.MetricValues.Where(m => m.HasUnsavedChanges).ToArray();
                if (metrics.Length == 0)
                {
                    continue;
                }

                result.Add(targetToAnnotate);

                foreach (var metricValue in metrics)
                {
                    analysis.Logger.WriteVerbose(
                        $"Method {target.MethodDisplayInfo}: updating metric {metricValue.Metric} {metricValue}.");
                }
            }

            analysis.Logger.WriteVerboseHint(
                $"Annotating resource file '{annotationFile.Origin}'.");

            XmlAnnotationHelpers.AddOrUpdateXmlAnnotation(
                // ReSharper disable once AssignNullToNotNullAttribute
                xmlAnnotationFile.XmlAnnotationDoc,
                result,
                analysis.RunState.BenchmarkType,
                UseFullTypeName);

            foreach (var targetToAnnotate in competitionTargets)
            {
                var target  = targetToAnnotate.Target;
                var metrics = targetToAnnotate.MetricValues.Where(m => m.HasUnsavedChanges).ToArray();

                foreach (var metricValue in metrics)
                {
                    analysis.Logger.WriteVerboseHint(
                        $"Method {target.MethodDisplayInfo}: metric {metricValue.Metric} {metricValue} updated.");
                }
            }

            return(result.ToArray());
        }
Exemplo n.º 2
0
        private static void TrySaveAnnotationCore(
            CompetitionTarget targetToAnnotate,
            AnnotationContext annotationContext,
            List <CompetitionTarget> result,
            IMessageLogger messageLogger)
        {
            var metrics = targetToAnnotate.MetricValues.Where(m => m.HasUnsavedChanges).ToArray();

            if (metrics.Length == 0)
            {
                return;
            }

            var target    = targetToAnnotate.Target;
            var targetKey = new AnnotationTargetKey(target.Method.MethodHandle);

            var annotationFile = annotationContext.TryGetDocument(targetKey);

            if (annotationFile == null)
            {
                var origin = TryGetAnnotationLocation(targetToAnnotate, messageLogger);
                if (origin == null)
                {
                    annotationFile = annotationContext.GetUnknownOriginDocument();
                }
                else
                {
                    annotationFile = annotationContext.TryGetDocument(origin);
                    if (annotationFile == null)
                    {
                        var soureAnnotationFile = ParseAnnotationFile(target, origin, messageLogger);

                        annotationFile = soureAnnotationFile;
                        annotationContext.AddDocument(annotationFile);
                        foreach (var benchmarkMethod in soureAnnotationFile.BenchmarkMethods.Keys)
                        {
                            var anotherKey = new AnnotationTargetKey(benchmarkMethod);
                            if (!targetKey.Equals(anotherKey))
                            {
                                annotationContext.AddTargetKey(annotationFile, anotherKey);
                            }
                        }
                    }
                    else if (annotationFile is SourceAnnotationFile)
                    {
                        messageLogger.WriteSetupErrorMessage(
                            target,
                            $"The source file '{annotationFile.Origin}' does not contain code for method.");
                        return;
                    }
                }
                annotationContext.AddTargetKey(annotationFile, targetKey);
            }

            if (!annotationFile.Parsed)
            {
                messageLogger.WriteSetupErrorMessage(
                    $"Could not find XML annotation file {annotationFile.Origin} for the benchmark. Annotations were not saved.");
                return;
            }

            var sourceAnnotationFile = (SourceAnnotationFile)annotationFile;

            messageLogger.Logger.WriteVerboseHint(
                $"Method {target.MethodDisplayInfo}: annotating file '{annotationFile.Origin}'");
            // TODO: log line???
            var annotated = TryUpdate(sourceAnnotationFile, targetToAnnotate);

            if (!annotated)
            {
                messageLogger.WriteSetupErrorMessage(
                    target,
                    $"Could not find annotations in source file '{sourceAnnotationFile.Origin}'.");
            }
            else
            {
                result.Add(targetToAnnotate);
                foreach (var metricValue in metrics)
                {
                    messageLogger.Logger.WriteVerboseHint(
                        $"Method {target.MethodDisplayInfo}: metric {metricValue.Metric} {metricValue} updated.");
                }
            }
        }