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
24212e07
Verified
Commit
24212e07
authored
4 months ago
by
STEVAN Antoine
Browse files
Options
Downloads
Patches
Plain Diff
add LabeledFnTimed with Into implementation
parent
2ef9eb72
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#9125
passed
4 months ago
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/arithmetic.rs
+6
-3
6 additions, 3 deletions
examples/arithmetic.rs
src/lib.rs
+24
-7
24 additions, 7 deletions
src/lib.rs
with
30 additions
and
10 deletions
examples/arithmetic.rs
+
6
−
3
View file @
24212e07
...
...
@@ -10,7 +10,8 @@ fn arithmetic(b: &plnk::Bencher) {
let
b
=
rand
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
+
b
)
},
),
)
.into
(),
(
plnk
::
label!
{
operation
:
"subtraction"
}
.to_string
(),
plnk
::
closure!
{
...
...
@@ -18,7 +19,8 @@ fn arithmetic(b: &plnk::Bencher) {
let
b
=
rand
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
-
b
)
},
),
)
.into
(),
(
plnk
::
label!
{
operation
:
"multiplication"
}
.to_string
(),
plnk
::
closure!
{
...
...
@@ -26,7 +28,8 @@ fn arithmetic(b: &plnk::Bencher) {
let
b
=
rand
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
*
b
)
},
),
)
.into
(),
]);
eprintln!
(
"last value: {}"
,
res
.unwrap
());
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
24
−
7
View file @
24212e07
...
...
@@ -11,6 +11,17 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
pub
type
FnTimed
<
T
>
=
Box
<
dyn
Fn
()
->
TimeWithValue
<
T
>>
;
pub
struct
LabeledFnTimed
<
T
>
{
label
:
String
,
func
:
FnTimed
<
T
>
,
}
impl
<
T
>
From
<
(
String
,
FnTimed
<
T
>
)
>
for
LabeledFnTimed
<
T
>
{
fn
from
((
label
,
func
):
(
String
,
FnTimed
<
T
>
))
->
Self
{
LabeledFnTimed
{
label
,
func
}
}
}
#[derive(Debug,
Clone)]
/// hold information about the benchmark to run
pub
struct
Bencher
{
...
...
@@ -115,7 +126,7 @@ impl Bencher {
/// - the measurements will be printed to STDOUT or to file as JSON
/// - the result of the last run will be returned
#[allow(clippy::type_complexity)]
pub
fn
bench_multiple_and_return
<
O
>
(
&
self
,
benches
:
Vec
<
(
String
,
FnTimed
<
O
>
)
>
)
->
Option
<
O
>
{
pub
fn
bench_multiple_and_return
<
O
>
(
&
self
,
benches
:
Vec
<
Labeled
FnTimed
<
O
>>
)
->
Option
<
O
>
{
let
style
=
ProgressStyle
::
with_template
(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}"
,
)
...
...
@@ -129,14 +140,14 @@ impl Bencher {
let
mut
res
=
None
;
let
mut
append
=
self
.append
;
for
(
label
,
f
)
in
benches
.iter
()
{
for
LabeledFnTimed
{
label
,
f
unc
}
in
benches
.iter
()
{
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
..
self
.nb_measurements
{
let
TimeWithValue
{
t
,
v
}
=
f
();
let
TimeWithValue
{
t
,
v
}
=
f
unc
();
res
=
Some
(
v
);
times
.push
(
t
.as_nanos
());
...
...
@@ -156,25 +167,31 @@ impl Bencher {
/// see [`Self::bench_multiple_and_return`]
#[allow(clippy::type_complexity)]
pub
fn
bench_multiple
<
O
>
(
&
self
,
benches
:
Vec
<
(
String
,
FnTimed
<
O
>
)
>
)
{
pub
fn
bench_multiple
<
O
>
(
&
self
,
benches
:
Vec
<
Labeled
FnTimed
<
O
>>
)
{
self
.bench_multiple_and_return
(
benches
);
}
/// see [`Self::bench_multiple_and_return`]
pub
fn
bench_and_return
<
O
>
(
&
self
,
label
:
impl
ToString
,
f
:
FnTimed
<
O
>
)
->
Option
<
O
>
{
self
.bench_multiple_and_return
(
vec!
[(
label
.to_string
(),
f
)])
self
.bench_multiple_and_return
(
vec!
[
LabeledFnTimed
{
label
:
label
.to_string
(),
func
:
f
,
}])
}
/// see [`Self::bench_multiple_and_return`]
pub
fn
bench
<
O
>
(
&
self
,
label
:
impl
ToString
,
f
:
FnTimed
<
O
>
)
{
self
.bench_multiple_and_return
(
vec!
[(
label
.to_string
(),
f
)]);
self
.bench_multiple_and_return
(
vec!
[
LabeledFnTimed
{
label
:
label
.to_string
(),
func
:
f
,
}]);
}
}
#[macro_export]
macro_rules!
closure
{
(
$
(
$body:stmt
);
*
$
(;)
?
)
=>
{
Box
::
new
(
move
||
{
$
(
$body
)
*
})
Box
::
new
(
move
||
{
$
(
$body
)
*
})
as
plnk
::
FnTimed
<
_
>
};
}
...
...
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