Beispiel #1
0
        private static string[] TryReadFileContent(string file, IMessageLogger messageLogger)
        {
            try
            {
                return(IoHelpers.ReadFileContent(file));
            }
            catch (IOException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    $"Could not access file '{file}'.", ex);

                return(Array <string> .Empty);
            }
            catch (UnauthorizedAccessException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    $"Could not access file '{file}'.", ex);

                return(Array <string> .Empty);
            }
            catch (DecoderFallbackException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    $"Cannot detect encoding for file '{file}'. Try to save the file as UTF8 or UTF16.", ex);

                return(Array <string> .Empty);
            }
        }
Beispiel #2
0
        public static XDocument TryParseXmlAnnotationDoc(
            string resourcePath,
            IMessageLogger messageLogger)
        {
            try
            {
                using (var stream = File.OpenRead(resourcePath))
                {
                    return(TryParseXmlAnnotationDoc(
                               stream,
                               $"XML annotation '{resourcePath}'",
                               messageLogger));
                }
            }
            catch (IOException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    $"Could not access file '{resourcePath}'.", ex);

                return(null);
            }
            catch (UnauthorizedAccessException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    $"Could not access file '{resourcePath}'.", ex);

                return(null);
            }
        }
        public static SourceAnnotationInfo TryGetSourceInfo(
            [NotNull] Target target,
            [NotNull] IMessageLogger messageLogger)
        {
            SourceAnnotationInfo result = null;

            try
            {
                // ReSharper disable once PossibleNullReferenceException
                var reader = GetReader(target.Method.DeclaringType.Module);

                result = TryGetSourceInfoCore(target, reader, messageLogger);
                if (result == null)
                {
                    messageLogger.WriteSetupErrorMessage(
                        target,
                        "No PDB data available.",
                        $"Ensure that there is actual pdb file for the assembly '{target.Method.DeclaringType?.Module.GetModulePath()}'.");
                }
            }
            catch (COMException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.ExecutionError, target,
                    "Could not parse method symbols.", ex);
            }

            return(result);
        }
Beispiel #4
0
        // ReSharper restore ConvertClosureToMethodGroup

        private static T?TryParseCore <T>(
            Target target, XElement competitionNode,
            string xmlAttributeName,
            Func <string, T> parseCallback,
            T fallbackValue,
            IMessageLogger messageLogger)
            where T : struct
        {
            var attributeValue = competitionNode.Attribute(xmlAttributeName)?.Value;

            if (attributeValue == null)
            {
                return(fallbackValue);
            }

            try
            {
                return(parseCallback(attributeValue));
            }
            catch (FormatException ex)
            {
                messageLogger.WriteExceptionMessage(
                    MessageSeverity.SetupError,
                    target,
                    $"XML annotation for {target.MethodDisplayInfo}: could not parse {xmlAttributeName}.",
                    ex);
                return(null);
            }
        }
Beispiel #5
0
        private static XDocument TryParseXmlAnnotationDocCore(
            [NotNull] Func <XmlReader> readerFactory,
            [NotNull] string sourceDescription,
            [NotNull] IMessageLogger messageLogger)
        {
            Code.NotNull(messageLogger, nameof(messageLogger));
            Code.NotNullNorEmpty(sourceDescription, nameof(sourceDescription));

            XDocument xmlAnnotationDoc;

            using (var reader = readerFactory())
            {
                try
                {
                    xmlAnnotationDoc = XDocument.Load(reader, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
                }
                catch (ArgumentException ex)
                {
                    messageLogger.WriteExceptionMessage(
                        MessageSeverity.SetupError,
                        $"{sourceDescription}: Could not parse XML annotation.", ex);
                    return(null);
                }
                catch (XmlException ex)
                {
                    messageLogger.WriteExceptionMessage(
                        MessageSeverity.SetupError,
                        $"{sourceDescription}: Could not parse XML annotation.", ex);
                    return(null);
                }
            }
            var rootNode = xmlAnnotationDoc.Element(CompetitionBenchmarksRootNode);

            if (rootNode == null)
            {
                messageLogger.WriteSetupErrorMessage(
                    $"{sourceDescription}: root node {CompetitionBenchmarksRootNode} not found.");

                return(null);
            }

            messageLogger.Logger.WriteVerbose($"{sourceDescription}: XML annotation parsed.");
            return(xmlAnnotationDoc);
        }
Beispiel #6
0
        private static bool SpinWait(
            Mutex mutex,
            TimeSpan waitTimeout, TimeSpan spinWaitTimeout,
            IMessageLogger messageLogger)
        {
            Code.InRange(waitTimeout, nameof(spinWaitTimeout), TimeSpan.Zero, _totalWaitTimeout);
            Code.InRange(spinWaitTimeout, nameof(spinWaitTimeout), TimeSpan.Zero, _totalWaitTimeout);

            if (spinWaitTimeout > waitTimeout)
            {
                spinWaitTimeout = waitTimeout;
            }

            bool lockTaken     = false;
            var  totalSpinTime = TimeSpan.Zero;

            while (!lockTaken && totalSpinTime < waitTimeout)
            {
                try
                {
                    lockTaken = mutex.WaitOne(spinWaitTimeout);

                    if (!lockTaken)
                    {
                        messageLogger.WriteInfoMessage($"Another perftest is running, wait timeout {totalSpinTime} of {waitTimeout}.");
                    }
                    else if (totalSpinTime > TimeSpan.Zero)
                    {
                        messageLogger.WriteInfoMessage(
                            $"Another perftest completed, starting. Wait timeout {totalSpinTime} of {waitTimeout}.");
                    }
                }
                catch (AbandonedMutexException ex)
                {
                    // It's ok to swallow abandoned mutex exception as we have no shared resources but logger output
                    // and the log is protected by FileShare.Read lock
                    // see https://msdn.microsoft.com/en-us/library/system.threading.abandonedmutexexception.aspx
                    // for more detail.
                    messageLogger.WriteExceptionMessage(
                        MessageSeverity.Informational,
                        $"Another perftest aborted, starting. Wait timeout {totalSpinTime} of {waitTimeout}.", ex);

                    lockTaken = true;
                }

                totalSpinTime += spinWaitTimeout;
            }

            return(lockTaken);
        }