示例#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>Saved targets, if any.</returns>
        protected override CompetitionTarget[] TrySaveAnnotationsCore(
            IReadOnlyCollection <CompetitionTarget> competitionTargets,
            AnnotationContext annotationContext,
            Analysis analysis)
        {
            var result = new List <CompetitionTarget>();

            foreach (var targetToAnnotate in competitionTargets)
            {
                TrySaveAnnotationCore(targetToAnnotate, annotationContext, result, analysis);
            }
            return(result.ToArray());
        }
示例#2
0
        /// <summary>Saves stored metrics from competition targets.</summary>
        /// <param name="targets">Competition targets with metrics to save.</param>
        /// <param name="annotationContext">The annotation context.</param>
        /// <param name="analysis">State of the analysis.</param>
        /// <returns>Saved targets, if any.</returns>
        public CompetitionTarget[] TrySaveTargets(
            IReadOnlyCollection <CompetitionTarget> targets,
            AnnotationContext annotationContext,
            Analysis analysis)
        {
            Code.NotNull(targets, nameof(targets));
            Code.NotNull(analysis, nameof(analysis));
            Code.BugIf(
                targets.Any(t => t.Target.Type != analysis.RunState.BenchmarkType),
                "Trying to annotate code that does not belongs to the benchmark.");

            var result = TrySaveAnnotationsCore(targets, annotationContext, analysis);

            if (result.Any())
            {
                annotationContext.Save();
            }
            return(result);
        }
示例#3
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());
        }
示例#4
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.");
                }
            }
        }
示例#5
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>Saved targets, if any.</returns>
 protected abstract CompetitionTarget[] TrySaveAnnotationsCore(
     [NotNull] IReadOnlyCollection <CompetitionTarget> competitionTargets,
     [NotNull] AnnotationContext annotationContext,
     [NotNull] Analysis analysis);