Esempio n. 1
0
            /// <summary>Adds target key for the document.</summary>
            /// <param name="annotationDocument">The annotation document.</param>
            /// <param name="key">The target key.</param>
            public void AddTargetKey(AnnotationDocument annotationDocument, AnnotationTargetKey key)
            {
                Code.AssertArgument(
                    annotationDocument.Parsed &&
                    ReferenceEquals(annotationDocument.AnnotationContext, Owner),
                    nameof(annotationDocument),
                    "The document is not parsed or does not belongs to the context.");

                _documentsByTargets.Add(key, annotationDocument);
            }
Esempio n. 2
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());
        }
Esempio n. 3
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.");
                }
            }
        }
Esempio n. 4
0
 /// <summary>Determines whether the cache contains annotation document for the specified target key.</summary>
 /// <param name="key">The target key.</param>
 /// <returns><c>true</c> if the cache contains annotation document for the specified target key; otherwise, <c>false</c>.</returns>
 public bool Contains(AnnotationTargetKey key) => _documentsByTargets.ContainsKey(key);
Esempio n. 5
0
 /// <summary>Gets the <see cref="AnnotationDocument"/> by target key.</summary>
 /// <value>The <see cref="AnnotationDocument"/>.</value>
 /// <param name="key">The target key.</param>
 /// <returns><see cref="AnnotationDocument"/> for the target.</returns>
 public AnnotationDocument this[AnnotationTargetKey key] => _documentsByTargets[key];
Esempio n. 6
0
 /// <summary>Adds target key for the document.</summary>
 /// <param name="annotationDocument">The annotation document.</param>
 /// <param name="key">The target key.</param>
 public void AddTargetKey([NotNull] AnnotationDocument annotationDocument, AnnotationTargetKey key)
 {
     Code.NotNull(annotationDocument, nameof(annotationDocument));
     AssertIsInLock();
     _documentsCache.AddTargetKey(annotationDocument, key);
 }
Esempio n. 7
0
        public AnnotationDocument TryGetDocument(AnnotationTargetKey key)
        {
            AssertIsInLock();

            return(_documentsCache.Contains(key) ? _documentsCache[key] : null);
        }