Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
676edd4c
Unverified
Commit
676edd4c
authored
Apr 06, 2022
by
gaaclarke
Committed by
GitHub
Apr 06, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the ability for microbenchmarks to print the probability of the result. (#101154)
parent
bafac177
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
5 deletions
+76
-5
common.dart
dev/benchmarks/microbenchmarks/lib/common.dart
+66
-0
build_bench.dart
dev/benchmarks/microbenchmarks/lib/stocks/build_bench.dart
+10
-5
No files found.
dev/benchmarks/microbenchmarks/lib/common.dart
View file @
676edd4c
...
...
@@ -3,6 +3,50 @@
// found in the LICENSE file.
import
'dart:convert'
show
json
;
import
'dart:math'
as
math
;
double
_doNormal
(
{
required
double
mean
,
required
double
stddev
,
required
double
x
})
{
return
(
1.0
/
(
stddev
*
math
.
sqrt
(
2.0
*
math
.
pi
)))
*
math
.
pow
(
math
.
e
,
-
0.5
*
math
.
pow
((
x
-
mean
)
/
stddev
,
2.0
));
}
double
_doMean
(
List
<
double
>
values
)
=>
values
.
reduce
((
double
x
,
double
y
)
=>
x
+
y
)
/
values
.
length
;
double
_doStddev
(
List
<
double
>
values
,
double
mean
)
{
double
stddev
=
0.0
;
for
(
final
double
value
in
values
)
{
stddev
+=
(
value
-
mean
)
*
(
value
-
mean
);
}
return
math
.
sqrt
(
stddev
/
values
.
length
);
}
double
_doIntegral
(
{
required
double
Function
(
double
)
func
,
required
double
start
,
required
double
stop
,
required
double
resolution
,
})
{
double
result
=
0.0
;
while
(
start
<
stop
)
{
final
double
value
=
func
(
start
);
result
+=
resolution
*
value
;
start
+=
resolution
;
}
return
result
;
}
/// Probability is defined as the probability that the mean is within the
/// [margin] of the true value.
double
_doProbability
(
{
required
double
mean
,
required
double
stddev
,
required
double
margin
})
{
return
_doIntegral
(
func:
(
double
x
)
=>
_doNormal
(
mean:
mean
,
stddev:
stddev
,
x:
x
),
start:
(
1.0
-
margin
)
*
mean
,
stop:
(
1.0
+
margin
)
*
mean
,
resolution:
0.001
,
);
}
/// This class knows how to format benchmark results for machine and human
/// consumption.
...
...
@@ -33,6 +77,28 @@ class BenchmarkResultPrinter {
_results
.
add
(
_BenchmarkResult
(
description
,
value
,
unit
,
name
));
}
/// Adds a benchmark result to the list of results and a probability of that
/// result.
///
/// The probability is calculated as the probability that the mean is +- 5% of
/// the true value.
///
/// See also [addResult].
void
addResultStatistics
({
required
String
description
,
required
List
<
double
>
values
,
required
String
unit
,
required
String
name
,
})
{
final
double
mean
=
_doMean
(
values
);
final
double
stddev
=
_doStddev
(
values
,
mean
);
const
double
margin
=
0.05
;
final
double
probability
=
_doProbability
(
mean:
mean
,
stddev:
stddev
,
margin:
margin
);
_results
.
add
(
_BenchmarkResult
(
description
,
mean
,
unit
,
name
));
_results
.
add
(
_BenchmarkResult
(
'
$description
- probability margin of error
$margin
'
,
probability
,
'percent'
,
'
${name}
_probability_5pct'
));
}
/// Prints the results added via [addResult] to standard output, once as JSON
/// for computer consumption and once formatted as plain text for humans.
void
printToStdout
()
{
...
...
dev/benchmarks/microbenchmarks/lib/stocks/build_bench.dart
View file @
676edd4c
...
...
@@ -21,6 +21,7 @@ Future<void> main() async {
final
Stopwatch
watch
=
Stopwatch
();
int
iterations
=
0
;
final
List
<
double
>
values
=
<
double
>[];
await
benchmarkWidgets
((
WidgetTester
tester
)
async
{
stocks
.
main
();
...
...
@@ -33,8 +34,10 @@ Future<void> main() async {
final
Element
appState
=
tester
.
element
(
find
.
byType
(
stocks
.
StocksApp
));
binding
.
framePolicy
=
LiveTestWidgetsFlutterBindingFramePolicy
.
benchmark
;
watch
.
start
();
while
(
watch
.
elapsed
<
kBenchmarkTime
)
{
Duration
elapsed
=
Duration
.
zero
;
while
(
elapsed
<
kBenchmarkTime
)
{
watch
.
reset
();
watch
.
start
();
appState
.
markNeedsBuild
();
// We don't use tester.pump() because we're trying to drive it in an
// artificially high load to find out how much CPU each frame takes.
...
...
@@ -43,15 +46,17 @@ Future<void> main() async {
// We use Timer.run to ensure there's a microtask flush in between
// the two calls below.
await
tester
.
pumpBenchmark
(
Duration
(
milliseconds:
iterations
*
16
));
watch
.
stop
();
iterations
+=
1
;
elapsed
+=
Duration
(
microseconds:
watch
.
elapsedMicroseconds
);
values
.
add
(
watch
.
elapsedMicroseconds
.
toDouble
());
}
watch
.
stop
();
});
final
BenchmarkResultPrinter
printer
=
BenchmarkResultPrinter
();
printer
.
addResult
(
printer
.
addResult
Statistics
(
description:
'Stock build'
,
value
:
watch
.
elapsedMicroseconds
/
iteration
s
,
value
s:
value
s
,
unit:
'µs per iteration'
,
name:
'stock_build_iteration'
,
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment