Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
PLNK - a simple Rust benchmark framework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
STEVAN Antoine
PLNK - a simple Rust benchmark framework
Commits
d011b9ee
Verified
Commit
d011b9ee
authored
2 months ago
by
STEVAN Antoine
Browse files
Options
Downloads
Patches
Plain Diff
move stuff to struct methods
parent
b2166613
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/arithmetic.rs
+27
-31
27 additions, 31 deletions
examples/arithmetic.rs
src/lib.rs
+62
-61
62 additions, 61 deletions
src/lib.rs
with
89 additions
and
92 deletions
examples/arithmetic.rs
+
27
−
31
View file @
d011b9ee
...
...
@@ -2,9 +2,7 @@ use clap::Parser;
use
rand
::
Rng
;
fn
arithmetic
(
b
:
&
plnk
::
Bencher
)
{
let
res
=
plnk
::
bench_multiple
(
b
,
vec!
[
let
res
=
b
.bench_multiple
(
vec!
[
(
plnk
::
label!
{
operation
:
"addition"
}
.to_string
(),
Box
::
new
(||
{
...
...
@@ -29,15 +27,13 @@ fn arithmetic(b: &plnk::Bencher) {
plnk
::
timeit
(||
a
*
b
)
}),
),
],
);
]);
eprintln!
(
"last value: {}"
,
res
.unwrap
());
}
fn
random
(
b
:
&
plnk
::
Bencher
)
{
plnk
::
bench
(
b
,
b
.bench
(
plnk
::
label!
{
operation
:
"sampling"
},
Box
::
new
(||
plnk
::
timeit
(||
rand
::
thread_rng
()
.gen
::
<
u128
>
())),
);
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
62
−
61
View file @
d011b9ee
...
...
@@ -97,18 +97,6 @@ impl Bencher {
println!
(
"{}"
,
output
);
}
}
}
#[macro_export]
macro_rules!
label
{
(
$
(
$key:ident
:
$value:expr
),
*
$
(,)
?
)
=>
{{
let
mut
parts
=
Vec
::
new
();
$
(
parts
.push
(
format!
(
"{}: {:?}"
,
stringify!
(
$key
),
$value
));
)
*
&
format!
(
"{{{}}}"
,
parts
.join
(
", "
))
}};
}
/// benchmark multiple pieces of code
///
...
...
@@ -120,7 +108,7 @@ macro_rules! label {
/// - the result of the last run will be returned
#[allow(clippy::type_complexity)]
pub
fn
bench_multiple
<
O
>
(
b
:
&
Bencher
,
&
self
,
benches
:
Vec
<
(
String
,
Box
<
dyn
Fn
()
->
(
Duration
,
O
)
>
)
>
,
)
->
Option
<
O
>
{
let
style
=
ProgressStyle
::
with_template
(
...
...
@@ -132,16 +120,17 @@ pub fn bench_multiple<O>(
let
mpb
=
MultiProgress
::
new
();
let
pb_main
=
mpb
.add
(
ProgressBar
::
new
(
benches
.len
()
as
u64
)
.with_style
(
style
.clone
()));
pb_main
.set_message
(
b
.name
.clone
());
pb_main
.set_message
(
self
.name
.clone
());
let
mut
res
=
None
;
let
mut
append
=
b
.append
;
let
mut
append
=
self
.append
;
for
(
label
,
f
)
in
benches
.iter
()
{
let
pb
=
mpb
.add
(
ProgressBar
::
new
(
b
.nb_measurements
as
u64
)
.with_style
(
style
.clone
()));
let
pb
=
mpb
.add
(
ProgressBar
::
new
(
self
.nb_measurements
as
u64
)
.with_style
(
style
.clone
()));
pb
.set_message
(
label
.to_string
());
let
mut
times
=
vec!
[];
for
_
in
0
..
b
.nb_measurements
{
for
_
in
0
..
self
.nb_measurements
{
let
(
t
,
_res
)
=
f
();
res
=
Some
(
_res
);
times
.push
(
t
.as_nanos
());
...
...
@@ -151,11 +140,11 @@ pub fn bench_multiple<O>(
pb_main
.inc
(
1
);
pb
.finish_and_clear
();
b
.dump
(
label
,
&
times
,
append
);
self
.dump
(
label
,
&
times
,
append
);
append
=
true
;
}
pb_main
.finish_with_message
(
format!
(
"{} done"
,
b
.name
));
pb_main
.finish_with_message
(
format!
(
"{} done"
,
self
.name
));
res
}
...
...
@@ -167,8 +156,20 @@ pub fn bench_multiple<O>(
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT or to file as JSON
/// - the result of the last run will be returned
pub
fn
bench
<
O
>
(
b
:
&
Bencher
,
label
:
impl
ToString
,
f
:
Box
<
dyn
Fn
()
->
(
Duration
,
O
)
>
)
->
Option
<
O
>
{
bench_multiple
(
b
,
vec!
[(
label
.to_string
(),
f
)])
pub
fn
bench
<
O
>
(
&
self
,
label
:
impl
ToString
,
f
:
Box
<
dyn
Fn
()
->
(
Duration
,
O
)
>
)
->
Option
<
O
>
{
self
.bench_multiple
(
vec!
[(
label
.to_string
(),
f
)])
}
}
#[macro_export]
macro_rules!
label
{
(
$
(
$key:ident
:
$value:expr
),
*
$
(,)
?
)
=>
{{
let
mut
parts
=
Vec
::
new
();
$
(
parts
.push
(
format!
(
"{}: {:?}"
,
stringify!
(
$key
),
$value
));
)
*
&
format!
(
"{{{}}}"
,
parts
.join
(
", "
))
}};
}
/// measure the time it takes to do something, and return the result
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment