Example #1
0
        private static bool TryFixBenchmarkAttribute(
            AnnotateContext annotateContext,
            string fileName, int firstCodeLine,
            CompetitionTarget competitionTarget)
        {
            var result          = false;
            var sourceFileLines = annotateContext.GetFileLines(fileName);

            for (var i = firstCodeLine - 2; i >= 0; i--)
            {
                var line = sourceFileLines[i];
                if (_breakIfRegex.IsMatch(line))
                {
                    break;
                }

                var line2 = _attributeRegex.Replace(
                    line,
                    m => FixAttributeContent(m, competitionTarget), 1);
                if (line2 != line)
                {
                    annotateContext.ReplaceLine(fileName, i, line2);
                    result = true;
                    break;
                }
            }
            return(result);
        }
Example #2
0
        private static bool TryFixBenchmarkAttribute(
            AnnotateContext annotateContext,
            string fileName, int firstCodeLine,
            CompetitionTarget competitionTarget,
            CompetitionState competitionState)
        {
            var sourceFileLines = annotateContext.TryGetFileLines(fileName, competitionState);

            if (sourceFileLines.Count == 0)
            {
                return(false);
            }

            bool attributeFixed = false;

            for (var i = firstCodeLine - 2; i >= 0; i--)
            {
                var line = sourceFileLines[i];
                if (_breakIfRegex.IsMatch(line))
                {
                    break;
                }

                bool hasMatch = false;
                var  line2    = _attributeRegex.Replace(
                    line,
                    m => FixAttributeContent(m, competitionTarget, out hasMatch), 1);

                if (hasMatch)
                {
                    if (line2 != line)
                    {
                        annotateContext.ReplaceLine(fileName, i, line2);
                    }

                    attributeFixed = true;
                    break;
                }
            }
            return(attributeFixed);
        }
        private bool TryFixBenchmarkResource(
            AnnotateContext annotateContext, string xmlFileName,
            CompetitionTarget competitionTarget)
        {
            var competitionName = competitionTarget.CompetitionName;
            var candidateName   = competitionTarget.CandidateName;

            var xdoc        = annotateContext.GetXmlAnnotation(xmlFileName);
            var competition = GetOrAdd(xdoc.Root, CompetitionTargetHelpers.CompetitionNode, competitionName);
            var candidate   = GetOrAdd(competition, CompetitionTargetHelpers.CandidateNode, candidateName);

            var minText = !competitionTarget.IgnoreMin ? competitionTarget.MinText : null;
            // Always prints
            var maxText = competitionTarget.MaxText;

            SetAttribute(candidate, CompetitionTargetHelpers.MinRatioAttribute, minText);
            SetAttribute(candidate, CompetitionTargetHelpers.MaxRatioAttribute, maxText);

            annotateContext.MarkAsChanged(xmlFileName);

            return(true);
        }
Example #4
0
        private static bool TryFixBenchmarkXmlAnnotation(
            AnnotateContext annotateContext, string xmlFileName,
            CompetitionTarget competitionTarget,
            CompetitionState competitionState)
        {
            Code.AssertArgument(
                competitionTarget.CompetitionMetadata != null, nameof(competitionTarget),
                "Competition metadata cannot be null for xml annotations.");

            var xmlAnnotationDoc = annotateContext.TryGetXmlAnnotation(
                xmlFileName,
                competitionTarget.CompetitionMetadata.UseFullTypeName,
                competitionState);

            if (xmlAnnotationDoc == null)
            {
                return(false);
            }

            XmlAnnotations.AddOrUpdateXmlAnnotation(xmlAnnotationDoc, competitionTarget);
            annotateContext.MarkAsChanged(xmlFileName);

            return(true);
        }
Example #5
0
        public static CompetitionTarget[] TryAnnotateBenchmarkFiles(
            [NotNull] CompetitionTarget[] targetsToAnnotate,
            [NotNull] CompetitionState competitionState)
        {
            Code.NotNull(targetsToAnnotate, nameof(targetsToAnnotate));
            Code.NotNull(competitionState, nameof(competitionState));

            var annotatedTargets = new List <CompetitionTarget>();
            var annContext       = new AnnotateContext();
            var logger           = competitionState.Logger;

            foreach (var targetToAnnotate in targetsToAnnotate)
            {
                var target            = targetToAnnotate.Target;
                var targetMethodTitle = target.MethodTitle;

                logger.WriteLineInfo(
                    $"{LogVerbosePrefix} Method {targetMethodTitle}: updating time limits {targetToAnnotate}.");

                // DONTTOUCH: the source should be loaded for checksum validation even if target uses resource annotation.
                string fileName;
                int    firstCodeLine;
                bool   hasSource = SymbolHelpers.TryGetSourceInfo(target.Method, competitionState, out fileName, out firstCodeLine);

                if (!hasSource)
                {
                    continue;
                }

                var competitionMetadata = targetToAnnotate.CompetitionMetadata;
                if (competitionMetadata != null)
                {
                    var resourceFileName = GetResourceFileName(fileName, competitionMetadata);

                    logger.WriteLineInfo(
                        $"{LogVerbosePrefix} Method {targetMethodTitle}: annotating resource file '{resourceFileName}'.");
                    var annotated = TryFixBenchmarkXmlAnnotation(annContext, resourceFileName, targetToAnnotate, competitionState);
                    if (!annotated)
                    {
                        competitionState.WriteMessage(
                            MessageSource.Analyser, MessageSeverity.Warning,
                            $"Method {targetMethodTitle}: could not annotate resource file '{resourceFileName}'.", null);
                        continue;
                    }
                }
                else
                {
                    logger.WriteLineInfo(
                        $"{LogVerbosePrefix} Method {targetMethodTitle}: annotating file '{fileName}', line {firstCodeLine}.");
                    var annotated = TryFixBenchmarkAttribute(annContext, fileName, firstCodeLine, targetToAnnotate, competitionState);
                    if (!annotated)
                    {
                        competitionState.WriteMessage(
                            MessageSource.Analyser, MessageSeverity.Warning,
                            $"Method {targetMethodTitle}: could not annotate source file '{fileName}'.");
                        continue;
                    }
                }

                logger.WriteLineInfo(
                    $"{LogImportantInfoPrefix} Method {targetMethodTitle} updated time limits: {targetToAnnotate}.");
                annotatedTargets.Add(targetToAnnotate);
            }

            annContext.Save();
            return(annotatedTargets.ToArray());
        }
Example #6
0
        private void AnnotateBenchmarkFiles(
            Summary summary, CompetitionAnalyser competitionAnalyser,
            ILogger logger, List <IWarning> warnings)
        {
            var annContext         = new AnnotateContext();
            var competitionTargets = competitionAnalyser.GetCompetitionTargets(summary);
            var newTargets         = competitionAnalyser.GetNewCompetitionTargets(summary);

            if (newTargets.Length == 0)
            {
                logger.WriteLineInfo("All competition benchmarks are in boundary.");
                return;
            }

            foreach (var newTarget in newTargets)
            {
                var targetMethodName = newTarget.CandidateName;

                logger.WriteLineInfo($"Method {targetMethodName}: new boundary [{newTarget.MinText},{newTarget.MaxText}].");

                int    firstCodeLine;
                string fileName;
                bool   hasSource = TryGetSourceInfo(newTarget, out fileName, out firstCodeLine);
                if (!hasSource)
                {
                    throw new InvalidOperationException($"Method {targetMethodName}: could not annotate. Source file not found.");
                }

                if (newTarget.UsesResourceAnnotation)
                {
                    var resourceFileName = Path.ChangeExtension(fileName, ".xml");
                    logger.WriteLineInfo(
                        $"Method {targetMethodName}: annotate resource file {resourceFileName}.");
                    bool annotated = TryFixBenchmarkResource(annContext, resourceFileName, newTarget);
                    if (!annotated)
                    {
                        throw new InvalidOperationException(
                                  $"Method {targetMethodName}: could not annotate resource file {resourceFileName}.");
                    }
                }
                else
                {
                    logger.WriteLineInfo($"Method {targetMethodName}: annotate at line {firstCodeLine}, file {fileName}.");
                    bool annotated = TryFixBenchmarkAttribute(annContext, fileName, firstCodeLine, newTarget);
                    if (!annotated)
                    {
                        throw new InvalidOperationException($"Method {targetMethodName}: could not annotate. Source file {fileName}.");
                    }
                }

                if (RerunIfModified && !competitionAnalyser.LastRun)
                {
                    var message = $"Method {targetMethodName} annotation updated, benchmark has to be restarted";
                    logger.WriteLineInfo(message);
                    warnings.Add(new Warning(nameof(AnnotateSourceAnalyser), message, null));

                    competitionTargets[newTarget.Target] = newTarget;
                    competitionAnalyser.RerunRequested   = true;
                }
                else
                {
                    var message = $"Method {targetMethodName} annotation updated.";
                    logger.WriteLineInfo(message);
                    warnings.Add(new Warning(nameof(AnnotateSourceAnalyser), message, null));
                }
            }

            annContext.Save();
        }