Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm][bench] Limit initial samples #86953

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/mono/sample/wasm/browser-bench/BenchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ public virtual void RunStep() { }

public virtual bool HasRunStepAsync => false;

protected virtual int CalculateSteps(int milliseconds, TimeSpan initTs)
protected virtual int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return (int)(milliseconds * InitialSamples / Math.Max(1.0, initTs.TotalMilliseconds));
return (int)(milliseconds * initialSamples / Math.Max(1.0, initTs.TotalMilliseconds));
}

public async Task<Result> RunBatch(BenchTask task, int milliseconds)
{
DateTime start = DateTime.Now;
DateTime end;
int initialSamples = InitialSamples;
try
{
// run one to eliminate possible startup overhead and do GC collection
Expand All @@ -79,15 +80,35 @@ public async Task<Result> RunBatch(BenchTask task, int milliseconds)
GC.Collect();

start = DateTime.Now;
for (currentStep = 0; currentStep < InitialSamples; currentStep++)
if (HasRunStepAsync)
await RunStepAsync();
else
RunStep();
if (HasRunStepAsync)
await RunStepAsync();
else
RunStep();
end = DateTime.Now;

// try to limit initial samples to 1s
var oneTs = end - start;
var maxInitMs = 1000;
if (oneTs.TotalMilliseconds > 0 && oneTs.TotalMilliseconds*InitialSamples > maxInitMs)
initialSamples = (int)(maxInitMs/oneTs.TotalMilliseconds);

if (initialSamples > 1) {
GC.Collect();

start = DateTime.Now;
for (currentStep = 0; currentStep < initialSamples; currentStep++)
if (HasRunStepAsync)
await RunStepAsync();
else
RunStep();
end = DateTime.Now;
} else {
// we already have the 1st measurement
initialSamples = 1;
}

var initTs = end - start;
int steps = CalculateSteps(milliseconds, initTs);
int steps = CalculateSteps(milliseconds, initTs, initialSamples);

start = DateTime.Now;
for (currentStep = 0; currentStep < steps; currentStep++)
Expand All @@ -101,14 +122,14 @@ public async Task<Result> RunBatch(BenchTask task, int milliseconds)

var ts = end - start;

return new Result { span = ts + initTs, steps = steps + InitialSamples, taskName = task.Name, measurementName = Name };
return new Result { span = ts + initTs, steps = steps + initialSamples, taskName = task.Name, measurementName = Name };
}
catch (Exception ex)
{
end = DateTime.Now;
var ts = end - start;
Console.WriteLine(ex);
return new Result { span = ts, steps = currentStep + InitialSamples, taskName = task.Name, measurementName = Name + " " + ex.Message };
return new Result { span = ts, steps = currentStep + initialSamples, taskName = task.Name, measurementName = Name + " " + ex.Message };
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/mono/sample/wasm/browser-bench/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public class TextInfoToTitleCase : TextInfoMeasurement

public abstract class StringsCompare : StringMeasurement
{
public override int InitialSamples => 100;

protected string strAsciiSuffix;
protected string strAsciiPrefix;
protected string needleSameAsStrEnd;
Expand Down
14 changes: 7 additions & 7 deletions src/mono/sample/wasm/browser-bench/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class PartialSend_1BMeasurement : WebSocketMeasurement
public override int InitialSamples => 1000;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1]);

protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return 250_000;
}
Expand All @@ -112,7 +112,7 @@ public PartialSend_64KBMeasurement()
buffer[i] = (byte)(i & 0xff);
}
}
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return 3000;
}
Expand All @@ -137,7 +137,7 @@ public PartialSend_1MBMeasurement()
buffer[i] = (byte)(i & 0xff);
}
}
protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return 100;
}
Expand All @@ -154,9 +154,9 @@ public class PartialReceive_1BMeasurement : WebSocketReceiveMeasurement
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1]);

protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return MaxLength - InitialSamples - 100;
return MaxLength - initialSamples - 100;
}

public override void RunStep()
Expand All @@ -177,7 +177,7 @@ public class PartialReceive_10KBMeasurement : WebSocketReceiveMeasurement
public override int InitialSamples => 1;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);

protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return 500;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public class PartialReceive_100KBMeasurement : WebSocketReceiveMeasurement
public override int InitialSamples => 1;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[bufferSize]);

protected override int CalculateSteps(int milliseconds, TimeSpan initTs)
protected override int CalculateSteps(int milliseconds, TimeSpan initTs, int initialSamples)
{
return MaxMessages - 1;
}
Expand Down