public Carpenter(HttpClient http, ProgressTask task, HammerSettings settings) { var strengths = GetStrengths(settings.Min, settings.Max); task.MaxValue(strengths.Sum(s => s)); _tool = new Hammer(http, () => Factory.Message(settings), () => task.Increment(1), strengths); }
public Driller(HttpClient http, ProgressTask task, DrillSettings settings) { var requests = (uint)settings.RPS * settings.Duration; var delay = Stopwatch.Frequency / settings.RPS; task.MaxValue(requests); _tool = new Drill(http, () => Factory.Message(settings), () => task.Increment(1), requests, delay); }
/// <summary> /// Runs all puzzles found by the <see cref="Infrastructure.PuzzleLocator"/> and displays their output. /// </summary> public void Run() { var output = new ConcurrentBag <(PuzzleOutput sample, PuzzleOutput puzzle)>(); var sw = new Stopwatch(); var cts = new CancellationTokenSource(); CancellationToken token = cts.Token; // Give us 20s to run all tests, and bail after that. cts.CancelAfter(20_000); int completed = 0; var progressTask = Task.Run(() => AnsiConsole.Progress() .AutoClear(true) .Columns(new ProgressColumn[] { new TaskDescriptionColumn(), // Task description new ProgressBarColumn(), // Progress bar new SpinnerColumn(), // Spinner }) .Start(ctx => { ProgressTask task1 = ctx.AddTask("[blue]Vacationing in the tropics[/]"); task1.MaxValue = PuzzleLocator.Puzzles.Count - 100; task1.StartTask(); while (!ctx.IsFinished && !token.IsCancellationRequested) { double increment = completed - task1.Value; task1.Increment(increment); } }), token); sw.Start(); PuzzleLocator.Puzzles.ParallelForEachAsync(async(puzzleGenericType) => { dynamic puzzle = PuzzleFactory.Build(puzzleGenericType); string?name = puzzleGenericType?.FullName ?? "N/A"; output.Add( ( await RunAsync(name, () => puzzle.ValidateSample(), token), await RunAsync(name, () => puzzle.Solve(), token) )); Interlocked.Increment(ref completed); }).Wait(); sw.Stop(); progressTask.GetAwaiter().GetResult(); OutputRenderer.RenderResults(output); AnsiConsole.Console.MarkupLine($"[yellow]Advent of Code 2020 - Total Run Time: [/][teal]{sw.ElapsedMilliseconds}ms[/]"); }
private static async Task ProcessImageFileSpecAsync(byte[] fileBytes, ImageFileSpec spec, string inputImageOutputDirectory, Table table, ProgressTask overallProgress) { var extension = spec.FileSpecFormat == FileSpecFormat.Jpeg ? "jpg" : "webp"; var filename = $"{spec}.{extension}"; var filePath = Path.Combine(inputImageOutputDirectory, filename); await using var resizedImageStream = await GenerateImageAsync(fileBytes, spec, table); await using var fileStream = File.OpenWrite(filePath); await resizedImageStream.CopyToAsync(fileStream); overallProgress.Increment(OverallProgressIncrementAmount); }
private async Task FetchPackagesDataAsync(ProgressTask task, List <Package> packages) { // Number of concurrent requests // TODO: make variable via settings var numberConcurrentRequests = 4; // Number of total runs needed to make all requests var numberRunsTotal = Math.Ceiling((double)packages.Count / numberConcurrentRequests); // Number to increment progress bar = 100% / number of runs var incrementBy = 100d / numberRunsTotal; ILogger logger = NullLogger.Instance; CancellationToken cancellationToken = CancellationToken.None; SourceCacheContext cache = new(); SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json"); FindPackageByIdResource resource = await repository.GetResourceAsync <FindPackageByIdResource>(); for (var i = 0; i < numberRunsTotal; i++) { // Only take "number of allowed concurrent requests" packages at a time var currentRunPackages = packages .Skip(i * numberConcurrentRequests) .Take(numberConcurrentRequests); var tasks = currentRunPackages .Select(p => resource.GetAllVersionsAsync(p.PackageName, cache, logger, cancellationToken)); await Task.WhenAll(tasks); foreach (var(p, index) in currentRunPackages.Select((item, index) => (item, index))) { var versions = tasks.ElementAt(index).Result; if (versions.Any()) { p.LatestStableVersion = _nuGetVersionService.GetLatestStableVersion(versions); p.LatestVersion = _nuGetVersionService.GetLatestVersion(versions); } } task.Increment(incrementBy); } }
/// <summary> /// Download the file at the specified uri /// </summary> /// <param name="client">The <see cref="HttpClient"/> used to download</param> /// <param name="fileUri">The file uri</param> /// <param name="outputStream">The output <see cref="Stream"/></param> /// <param name="progress">The <see cref="ProgressTask"/> to use</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/></param> /// <returns>A new awaitable <see cref="Task"/></returns> public static async Task DownloadAsync(this HttpClient client, string fileUri, Stream outputStream, ProgressTask progress, CancellationToken cancellationToken = default) { using (HttpResponseMessage response = await client.GetAsync(fileUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) { response.EnsureSuccessStatusCode(); progress.MaxValue(response.Content.Headers.ContentLength ?? 0); progress.StartTask(); var filename = fileUri.Substring(fileUri.LastIndexOf('/') + 1); using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); var buffer = new byte[8192]; while (true) { var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); if (bytesRead == 0) { break; } progress.Increment(bytesRead); await outputStream.WriteAsync(buffer, 0, bytesRead, cancellationToken); } } }
public ProgressColumnFixture(double completed, double total) { Column = new T(); Task = new ProgressTask(1, "Foo", total); Task.Increment(completed); }
public void Increment(double value) => _task.Increment(value);
private static void UpdateProgress(ICollectionContext ctx, ProgressTask progressTask) { progressTask.MaxValue(ctx.ResultTotal); progressTask.Increment(ctx.ResultCount); }
public void Increment() { _task.Increment(_increment); }
public Nailer(HttpClient http, ProgressTask task, NailgunSettings settings) { task.MaxValue(settings.Requests); _tool = new Nailgun(http, () => Factory.Message(settings), () => task.Increment(1), settings.Requests); }
public override int Execute( CommandContext context, ImportSettings settings ) { double last = 0.0; double prog = 0.0; ProgressTask progress = null; var mre = new ManualResetEvent(false); void UpdateProgress(double p) { prog = p * 100d; mre.Set(); } var task = Task.Run(() => { CsvDataReader csv = null; var database = settings.Database; var filename = settings.File; var loader = GetLoader(settings.Provider); var tableName = settings.Table ?? Path.GetFileNameWithoutExtension(filename); Stream s = File.OpenRead(settings.File); s = s.WithReadProgress(UpdateProgress, 0.001); var tr = new StreamReader(s); for (int i = 0; i < settings.Skip; i++) { tr.ReadLine(); } string schemaSpec = null; if (settings.Schema != null) { var schemaFile = settings.Schema; schemaSpec = File.ReadAllText(schemaFile); } else { var schemaFile = filename + ".schema"; if (File.Exists(schemaFile)) { schemaSpec = File.ReadAllText(schemaFile); } } var explicitSchema = schemaSpec == null ? null : Schema.Parse(schemaSpec); var schema = explicitSchema == null ? CsvSchema.Nullable : new CsvSchema(explicitSchema); var opts = new CsvDataReaderOptions { BufferSize = 0x100000, Schema = schema, }; csv = CsvDataReader.Create(tr, opts); loader.Load(csv, tableName, database); mre.Set(); }); // ensures that the progress loop finishes. task.ContinueWith(t => mre.Set()); AnsiConsole.Progress() .Columns(new ProgressColumn[] { new TaskDescriptionColumn(), // Task description new ProgressBarColumn(), // Progress bar new PercentageColumn(), // Percentage new RemainingTimeColumn(), // Remaining time new SpinnerColumn(), } ) .Start(ctx => { progress = ctx.AddTask("Import"); while (!task.IsCompleted) { mre.WaitOne(); var inc = prog - last; last = prog; progress.Increment(inc); mre.Reset(); } if (task.IsFaulted) { throw task.Exception; } else { // make sure it arrives at 100% if (last < 100d) { progress.Increment(100d - last); } } }); return(0); }