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
e74726ae
Verified
Commit
e74726ae
authored
3 months ago
by
STEVAN Antoine
Browse files
Options
Downloads
Patches
Plain Diff
add support for a log file
parent
5a54e85a
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
+10
-2
10 additions, 2 deletions
examples/arithmetic.rs
src/lib.rs
+94
-3
94 additions, 3 deletions
src/lib.rs
with
104 additions
and
5 deletions
examples/arithmetic.rs
+
10
−
2
View file @
e74726ae
...
...
@@ -66,17 +66,25 @@ struct Args {
#[arg(short,
long)]
multiple
:
bool
,
#[arg(short,
long)]
log_file
:
Option
<
String
>
,
}
fn
main
()
{
let
args
=
Args
::
parse
();
let
bencher
=
plnk
::
Bencher
::
new
(
args
.nb_measurements
);
let
bencher
=
match
args
.log_file
{
Some
(
path
)
=>
plnk
::
Bencher
::
new
(
args
.nb_measurements
)
.with_file
(
path
.into
())
.overwrite
(),
None
=>
plnk
::
Bencher
::
new
(
args
.nb_measurements
),
};
if
args
.multiple
{
arithmetic_multiple
(
&
bencher
.with_name
(
"arithmetic"
));
}
else
{
arithmetic
(
&
bencher
.with_name
(
"arithmetic"
));
}
random
(
&
bencher
.with_name
(
"random"
));
random
(
&
bencher
.with_name
(
"random"
)
.append
()
);
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
94
−
3
View file @
e74726ae
#![doc
=
include_str
!
(
"../README.md"
)]
use
std
::
time
::{
Duration
,
Instant
};
use
std
::{
fs
::{
create_dir_all
,
File
},
io
::
Write
,
path
::
PathBuf
,
time
::{
Duration
,
Instant
},
};
use
indicatif
::{
MultiProgress
,
ProgressBar
,
ProgressStyle
};
...
...
@@ -13,6 +18,9 @@ pub struct Bencher {
name
:
String
,
quiet
:
bool
,
file
:
Option
<
PathBuf
>
,
append
:
bool
,
}
impl
Bencher
{
...
...
@@ -21,6 +29,8 @@ impl Bencher {
nb_measurements
,
name
:
""
.to_string
(),
quiet
:
false
,
file
:
None
,
append
:
false
,
}
}
...
...
@@ -30,6 +40,39 @@ impl Bencher {
nb_measurements
:
self
.nb_measurements
,
name
:
name
.to_string
(),
quiet
:
self
.quiet
,
file
:
self
.file
.clone
(),
append
:
self
.append
,
}
}
/// add a file to a bencher
pub
fn
with_file
(
&
self
,
path
:
PathBuf
)
->
Self
{
Self
{
nb_measurements
:
self
.nb_measurements
,
name
:
self
.name
.clone
(),
quiet
:
self
.quiet
,
file
:
Some
(
path
),
append
:
self
.append
,
}
}
/// append to file
pub
fn
append
(
&
self
)
->
Self
{
Self
{
nb_measurements
:
self
.nb_measurements
,
name
:
self
.name
.clone
(),
quiet
:
self
.quiet
,
file
:
self
.file
.clone
(),
append
:
true
,
}
}
/// overwrite file
pub
fn
overwrite
(
&
self
)
->
Self
{
Self
{
nb_measurements
:
self
.nb_measurements
,
name
:
self
.name
.clone
(),
quiet
:
self
.quiet
,
file
:
self
.file
.clone
(),
append
:
false
,
}
}
...
...
@@ -82,7 +125,29 @@ where
pb
.inc
(
1
);
}
println!
(
"{}"
,
label!
{
label
:
label
,
name
:
b
.name
,
times
:
times
});
let
output
=
format!
(
"{}"
,
label!
{
label
:
label
,
name
:
b
.name
,
times
:
times
});
if
let
Some
(
path
)
=
&
b
.file
{
create_dir_all
(
path
.parent
()
.unwrap
())
.expect
(
&
format!
(
"could not create parent directory of log file '{}'"
,
path
.to_str
()
.unwrap
()
));
let
mut
f
=
File
::
options
()
.write
(
true
)
.append
(
b
.append
)
.truncate
(
!
b
.append
)
.create
(
true
)
.open
(
path
)
.expect
(
&
format!
(
"could not open log file '{}'"
,
path
.to_str
()
.unwrap
()
));
writeln!
(
&
mut
f
,
"{}"
,
output
)
.expect
(
&
format!
(
"could not write to log file '{}'"
,
path
.to_str
()
.unwrap
()
));
}
else
{
println!
(
"{}"
,
output
);
}
}
/// run a given piece of code a bunch of times and output the measurements
...
...
@@ -105,6 +170,8 @@ pub fn bench_multiple(b: &Bencher, benches: Vec<(String, Box<dyn Fn() -> Duratio
let
pb_main
=
mpb
.add
(
ProgressBar
::
new
(
benches
.len
()
as
u64
)
.with_style
(
style
.clone
()));
pb_main
.set_message
(
b
.name
.clone
());
let
mut
append
=
b
.append
;
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
());
...
...
@@ -118,7 +185,31 @@ pub fn bench_multiple(b: &Bencher, benches: Vec<(String, Box<dyn Fn() -> Duratio
pb_main
.inc
(
1
);
pb
.finish_and_clear
();
println!
(
"{}"
,
label!
{
label
:
label
,
name
:
b
.name
,
times
:
times
});
let
output
=
format!
(
"{}"
,
label!
{
label
:
label
,
name
:
b
.name
,
times
:
times
});
if
let
Some
(
path
)
=
&
b
.file
{
create_dir_all
(
path
.parent
()
.unwrap
())
.expect
(
&
format!
(
"could not create parent directory of log file '{}'"
,
path
.to_str
()
.unwrap
()
));
println!
(
"append: {}"
,
append
);
let
mut
f
=
File
::
options
()
.write
(
true
)
.append
(
append
)
.truncate
(
!
append
)
.create
(
true
)
.open
(
path
)
.expect
(
&
format!
(
"could not open log file '{}'"
,
path
.to_str
()
.unwrap
()
));
writeln!
(
&
mut
f
,
"{}"
,
output
)
.expect
(
&
format!
(
"could not write to log file '{}'"
,
path
.to_str
()
.unwrap
()
));
append
=
true
;
}
else
{
println!
(
"{}"
,
output
);
}
}
pb_main
.finish_with_message
(
format!
(
"{} done"
,
b
.name
));
...
...
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