Esempio n. 1
0
    private async Task AsyncFunctionThatCouldTimeoutUnderHeavyLoad(CancellationToken cancel)
    {
        AmbientStopwatch stopwatch = new AmbientStopwatch(true);

        for (int count = 0; count < 9; ++count)
        {
            await Task.Delay(100);

            cancel.ThrowIfCancellationRequested();
        }
        // if we finished before getting cancelled, we must have been scheduled within about 10 milliseconds on average, or we must be running using a paused ambient clock
    }
Esempio n. 2
0
 internal RequestTracker(RequestType requestType)
 {
     _requestType = requestType;
     _stopwatch   = new AmbientStopwatch(true);
     requestType.PendingRequests?.Increment();
 }
Esempio n. 3
0
    /// <summary>
    /// Performs the disk audit, reporting results into <paramref name="statusBuilder"/>.
    /// </summary>
    /// <param name="statusBuilder">A <see cref="StatusResultsBuilder"/> to write the results into.</param>
    /// <param name="cancel">The optional <see cref="CancellationToken"/>.</param>
    public async Task Audit(StatusResultsBuilder statusBuilder, CancellationToken cancel = default)
    {
        statusBuilder.NatureOfSystem = StatusNatureOfSystem.Leaf;
        statusBuilder.AddProperty("_Path", _driveInfo.Name);
        statusBuilder.AddProperty("_VolumeLabel", _driveInfo.VolumeLabel);
        statusBuilder.AddProperty("DriveFormat", _driveInfo.DriveFormat);
        statusBuilder.AddProperty("DriveType", _driveInfo.DriveType);
        statusBuilder.AddProperty("AvailableFreeBytes", _driveInfo.AvailableFreeSpace);
        statusBuilder.AddProperty("TotalFreeBytes", _driveInfo.TotalFreeSpace);
        statusBuilder.AddProperty("TotalBytes", _driveInfo.TotalSize);
        if (!string.IsNullOrEmpty(_testPath))
        {
            StatusResultsBuilder readBuilder = new StatusResultsBuilder("Read");
            statusBuilder.AddChild(readBuilder);
            try
            {
                // attempt to read a file (if one exists)
                foreach (string file in Directory.EnumerateFiles(Path.Combine(_driveInfo.RootDirectory.FullName, _testPath)))
                {
                    AmbientStopwatch s = AmbientStopwatch.StartNew();
                    try
                    {
                        using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            byte[] b = new byte[1];
                            await fs.ReadAsync(b, 0, 1, cancel);

                            await fs.FlushAsync();
                        }
                        readBuilder.AddProperty("ResponseMs", s.ElapsedMilliseconds);
                        readBuilder.AddOkay("Ok", "Success", "The read operation succeeded.");
                        break;
                    }
                    catch (IOException) // this will be thrown if the file cannot be accessed because it is open exclusively by another process (this happens a lot with temp files)
                    {
                        // just move on and try the next file
                        continue;
                    }
                }
            }
            catch (Exception e)
            {
                readBuilder.AddException(e);
            }
            if (!_readonly)
            {
                StatusResultsBuilder writeBuilder = new StatusResultsBuilder("Write");
                statusBuilder.AddChild(writeBuilder);
                try
                {
                    // attempt to write a temporary file
                    string           targetPath = Path.Combine(_driveInfo.RootDirectory.FullName, Guid.NewGuid().ToString("N"));
                    AmbientStopwatch s          = AmbientStopwatch.StartNew();
                    using (FileStream fs = new FileStream(targetPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose))
                    {
                        byte[] b = new byte[1];
                        await fs.WriteAsync(b, 0, 1);

                        await fs.FlushAsync();

                        readBuilder.AddProperty("ResponseMs", s.ElapsedMilliseconds);
                        writeBuilder.AddOkay("Ok", "Success", "The write operation succeeded.");
                    }
                }
                catch (Exception e)
                {
                    writeBuilder.AddException(e);
                }
            }
        }
    }