Skip to content

Commit

Permalink
Basic timings
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Sep 21, 2024
1 parent 9e44fc3 commit d9ca96d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
43 changes: 37 additions & 6 deletions src/Draco.Compiler.Fuzzer/TuiTracer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Draco.Compiler.Api.Syntax;
using Draco.Coverage;
Expand Down Expand Up @@ -42,6 +43,11 @@ public override string ToString()

private double bestCoveragePercent = 0;

// Timings
private readonly Label fuzzesPerSecondLabel;
private readonly Label averageTimePerFuzzLabel;
private readonly Stopwatch stopwatch = Stopwatch.StartNew();

// Current input
private readonly TextView currentInputTextView;
private readonly TextView minimizedInputTextView;
Expand Down Expand Up @@ -105,13 +111,33 @@ public TuiTracer()
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Width = Dim.Percent(70),
Height = Dim.Sized(5),
};
coverageFrameView.Add(
currentCoverageLabel, this.currentCoverageProgressBar, this.currentCoveragePercentLabel,
bestCoverageLabel, this.bestCoverageProgressBar, this.bestCoveragePercentLabel);

// Timings
this.fuzzesPerSecondLabel = new Label("Fuzz/s: 0")
{
X = 0,
Y = 0,
};
this.averageTimePerFuzzLabel = new Label("Avg/fuzz: 0ms")
{
X = 0,
Y = 2,
};
var timingsFrameView = new FrameView("Timings")
{
X = Pos.Right(coverageFrameView),
Y = 0,
Width = Dim.Fill(),
Height = Dim.Height(coverageFrameView),
};
timingsFrameView.Add(this.fuzzesPerSecondLabel, this.averageTimePerFuzzLabel);

// Current input
this.currentInputTextView = new()
{
Expand Down Expand Up @@ -219,10 +245,9 @@ public TuiTracer()
faultFrameView.Add(this.faultListView, this.selectedFaultItemTextView);

this.Add(
coverageFrameView,
coverageFrameView, timingsFrameView,
this.inputFrameView,
this.inputQueueFrameView,
faultFrameView);
this.inputQueueFrameView, faultFrameView);

Application.Top.Add(this);
}
Expand All @@ -240,7 +265,7 @@ public void InputDequeued(SyntaxTree input, IReadOnlyCollection<SyntaxTree> inpu
if (itemIndex >= 0) this.inputQueueList.RemoveAt(itemIndex);
}

public void EndOfMinimization(SyntaxTree input, SyntaxTree minimizedInput, CoverageResult coverage, TimeSpan elapsed)
public void EndOfMinimization(SyntaxTree input, SyntaxTree minimizedInput, CoverageResult coverage)
{
var currentCoveragePercent = CoverageToPercentage(coverage);
this.bestCoveragePercent = Math.Max(this.bestCoveragePercent, currentCoveragePercent);
Expand All @@ -256,9 +281,15 @@ public void EndOfMinimization(SyntaxTree input, SyntaxTree minimizedInput, Cover

++this.minimizedInputCounter;
this.inputFrameView.Title = $"Input (Tested: {this.minimizedInputCounter})";

var totalElapsed = this.stopwatch.Elapsed;
var fuzzesPerSecond = this.minimizedInputCounter / totalElapsed.TotalSeconds;
var averageMillisecondsPerFuzz = totalElapsed.TotalMilliseconds / this.minimizedInputCounter;
this.fuzzesPerSecondLabel.Text = $"Fuzz/s: {fuzzesPerSecond:0.00}";
this.averageTimePerFuzzLabel.Text = $"Avg/fuzz: {averageMillisecondsPerFuzz:0.00}ms";
}

public void EndOfMutations(SyntaxTree input, int mutationsFound, TimeSpan elapsed)
public void EndOfMutations(SyntaxTree input, int mutationsFound)
{
}

Expand Down
9 changes: 2 additions & 7 deletions src/Draco.Fuzzing/Fuzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private readonly record struct ExecutionResult(

private readonly Queue<TInput> inputQueue = new();
private readonly HashSet<TCoverage> discoveredCoverages = new();
private readonly Stopwatch stopwatch = new();

/// <summary>
/// Enqueues the given input.
Expand Down Expand Up @@ -95,15 +94,12 @@ public void EnqueueRange(IEnumerable<TInput> inputs)
public void Fuzz(CancellationToken cancellationToken)
{
this.ForceAllTypeInitializersToRun();
this.stopwatch.Start();
while (!cancellationToken.IsCancellationRequested && this.inputQueue.TryDequeue(out var input))
{
this.Tracer.InputDequeued(input, this.inputQueue);
var start = this.stopwatch.Elapsed;
// Minimize the input
var (minimalInput, executionResult) = this.Minimize(input);
var endOfMinimization = this.stopwatch.Elapsed;
this.Tracer.EndOfMinimization(input, minimalInput, executionResult.UncompressedCoverage, endOfMinimization - start);
this.Tracer.EndOfMinimization(input, minimalInput, executionResult.UncompressedCoverage);
if (executionResult.FaultResult.IsFaulted)
{
// NOTE: Should we not skip mutating this input?
Expand All @@ -118,8 +114,7 @@ public void Fuzz(CancellationToken cancellationToken)
this.Enqueue(mutated);
++mutationsFound;
}
var endOfMutations = this.stopwatch.Elapsed;
this.Tracer.EndOfMutations(minimalInput, mutationsFound, endOfMutations - endOfMinimization);
this.Tracer.EndOfMutations(minimalInput, mutationsFound);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Draco.Fuzzing/ITracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public interface ITracer<TInput>
/// <param name="input">The original input.</param>
/// <param name="minimizedInput">The minimized input.</param>
/// <param name="coverage">The coverage of the minimized input.</param>
/// <param name="elapsed">The time it took to minimize the input.</param>
public void EndOfMinimization(TInput input, TInput minimizedInput, CoverageResult coverage, TimeSpan elapsed);
public void EndOfMinimization(TInput input, TInput minimizedInput, CoverageResult coverage);

/// <summary>
/// Called when mutation of an input finishes.
/// </summary>
/// <param name="input">The original input.</param>
/// <param name="mutationsFound">The number of mutations found.</param>
/// <param name="elapsed">The time it took to mutate the input.</param>
public void EndOfMutations(TInput input, int mutationsFound, TimeSpan elapsed);
public void EndOfMutations(TInput input, int mutationsFound);

/// <summary>
/// Called when an input faulted.
Expand Down

0 comments on commit d9ca96d

Please sign in to comment.