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
5a54e85a
Verified
Commit
5a54e85a
authored
4 months ago
by
STEVAN Antoine
Browse files
Options
Downloads
Patches
Plain Diff
do not requite closures to be mutable
parent
42a86916
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
+9
-12
9 additions, 12 deletions
examples/arithmetic.rs
src/lib.rs
+10
-10
10 additions, 10 deletions
src/lib.rs
with
19 additions
and
22 deletions
examples/arithmetic.rs
+
9
−
12
View file @
5a54e85a
...
...
@@ -2,21 +2,19 @@ use clap::Parser;
use
rand
::
Rng
;
fn
arithmetic
(
b
:
&
plnk
::
Bencher
)
{
let
mut
rng
=
rand
::
thread_rng
();
plnk
::
bench
(
b
,
plnk
::
label!
{
operation
:
"addition"
},
||
{
let
a
=
r
ng
.gen
::
<
u128
>
();
let
b
=
r
ng
.gen
::
<
u128
>
();
let
a
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
let
b
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
+
b
)
});
plnk
::
bench
(
b
,
plnk
::
label!
{
operation
:
"substraction"
},
||
{
let
a
=
r
ng
.gen
::
<
u128
>
();
let
b
=
r
ng
.gen
::
<
u128
>
();
let
a
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
let
b
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
-
b
)
});
let
res
=
plnk
::
bench_and_return
(
b
,
plnk
::
label!
{
operation
:
"multiplication"
},
||
{
let
a
=
r
ng
.gen
::
<
u128
>
();
let
b
=
r
ng
.gen
::
<
u128
>
();
let
a
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
let
b
=
r
and
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit_and_return
(||
a
*
b
)
});
...
...
@@ -26,13 +24,13 @@ fn arithmetic(b: &plnk::Bencher) {
fn
arithmetic_multiple
(
b
:
&
plnk
::
Bencher
)
{
plnk
::
bench_multiple
(
b
,
&
mut
[
vec!
[
(
plnk
::
label!
{
operation
:
"addition"
}
.to_string
(),
Box
::
new
(||
{
let
a
=
rand
::
thread_rng
()
.gen
::
<
u128
>
();
let
b
=
rand
::
thread_rng
()
.gen
::
<
u128
>
();
plnk
::
timeit
(||
a
+
b
)
plnk
::
timeit
(||
a
-
b
)
}),
),
(
...
...
@@ -56,9 +54,8 @@ fn arithmetic_multiple(b: &plnk::Bencher) {
}
fn
random
(
b
:
&
plnk
::
Bencher
)
{
let
mut
rng
=
rand
::
thread_rng
();
plnk
::
bench
(
b
,
plnk
::
label!
{
operation
:
"sampling"
},
||
{
plnk
::
timeit
(||
r
ng
.gen
::
<
u128
>
())
plnk
::
timeit
(||
r
and
::
thread_rng
()
.gen
::
<
u128
>
())
});
}
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
10
−
10
View file @
5a54e85a
...
...
@@ -65,9 +65,9 @@ macro_rules! label {
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
pub
fn
bench
<
F
>
(
b
:
&
Bencher
,
label
:
&
str
,
mut
f
:
F
)
pub
fn
bench
<
F
>
(
b
:
&
Bencher
,
label
:
&
str
,
f
:
F
)
where
F
:
Fn
Mut
()
->
Duration
,
F
:
Fn
()
->
Duration
,
{
if
!
b
.quiet
{
eprintln!
(
"bencher: {}, label: {}"
,
b
.name
,
label
);
...
...
@@ -93,7 +93,7 @@ where
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
#[allow(clippy::type_complexity)]
pub
fn
bench_multiple
(
b
:
&
Bencher
,
benches
:
&
mut
[
(
String
,
Box
<
dyn
Fn
Mut
()
->
Duration
>
)
]
)
{
pub
fn
bench_multiple
(
b
:
&
Bencher
,
benches
:
Vec
<
(
String
,
Box
<
dyn
Fn
()
->
Duration
>
)
>
)
{
let
style
=
ProgressStyle
::
with_template
(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}"
,
)
...
...
@@ -105,7 +105,7 @@ pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Du
let
pb_main
=
mpb
.add
(
ProgressBar
::
new
(
benches
.len
()
as
u64
)
.with_style
(
style
.clone
()));
pb_main
.set_message
(
b
.name
.clone
());
for
(
label
,
f
)
in
benches
.iter
_mut
()
{
for
(
label
,
f
)
in
benches
.iter
()
{
let
pb
=
mpb
.add
(
ProgressBar
::
new
(
b
.nb_measurements
as
u64
)
.with_style
(
style
.clone
()));
pb
.set_message
(
label
.to_string
());
...
...
@@ -131,9 +131,9 @@ pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Du
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
pub
fn
bench_and_return
<
F
,
O
>
(
b
:
&
Bencher
,
label
:
&
str
,
mut
f
:
F
)
->
Option
<
O
>
pub
fn
bench_and_return
<
F
,
O
>
(
b
:
&
Bencher
,
label
:
&
str
,
f
:
F
)
->
Option
<
O
>
where
F
:
Fn
Mut
()
->
(
Duration
,
O
),
F
:
Fn
()
->
(
Duration
,
O
),
{
if
!
b
.quiet
{
eprintln!
(
"bencher: {}, label: {}"
,
b
.name
,
label
);
...
...
@@ -164,9 +164,9 @@ where
/// # use plnk::timeit;
/// let t = timeit(|| 1 + 2);
/// ```
pub
fn
timeit
<
F
,
O
>
(
mut
f
:
F
)
->
Duration
pub
fn
timeit
<
F
,
O
>
(
f
:
F
)
->
Duration
where
F
:
Fn
Mut
()
->
O
,
F
:
Fn
()
->
O
,
{
let
start_time
=
Instant
::
now
();
let
_
=
f
();
...
...
@@ -181,9 +181,9 @@ where
/// let (t, res) = timeit_and_return(|| 1 + 2);
/// assert_eq!(res, 3);
/// ```
pub
fn
timeit_and_return
<
F
,
O
>
(
mut
f
:
F
)
->
(
Duration
,
O
)
pub
fn
timeit_and_return
<
F
,
O
>
(
f
:
F
)
->
(
Duration
,
O
)
where
F
:
Fn
Mut
()
->
O
,
F
:
Fn
()
->
O
,
{
let
start_time
=
Instant
::
now
();
let
res
=
f
();
...
...
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