Skip to content

Commit

Permalink
new bench that tests random command ordering, to confuse the branch p…
Browse files Browse the repository at this point in the history
…redictor (#12)

* new bench that tests random command ordering, to confuse the branch predictor

* import reordering
  • Loading branch information
fabi321 committed Jun 18, 2023
1 parent 74e95c9 commit a0d86d0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vncserver = { version ="0.2", optional = true}
[dev-dependencies]
criterion = {version = "0.5", features = ["async_tokio"]}
rstest = "0.17"
rand = "0.8.5"

[features]
default = ["vnc"]
Expand Down
29 changes: 25 additions & 4 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use breakwater::{
use criterion::{
BenchmarkId, Criterion, {criterion_group, criterion_main},
};
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::{sync::Arc, time::Duration};

const FRAMEBUFFER_WIDTH: usize = 1920;
Expand All @@ -21,15 +23,34 @@ async fn invoke_parse_pixelflut_commands(
}

fn from_elem(c: &mut Criterion) {
let draw_commands = get_commands_to_draw_rect(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, 0x123456);
let draw_commands = draw_commands.as_bytes();
let mut draw_commands =
get_commands_to_draw_rect(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, 0x123456);
let ordered_draw_commands = draw_commands.join("");

c.bench_with_input(
BenchmarkId::new(
"parse_draw_commands",
"parse_draw_commands_ordered",
format!("{FRAMEBUFFER_WIDTH} x {FRAMEBUFFER_HEIGHT}"),
),
&draw_commands,
&ordered_draw_commands.as_bytes(),
|b, input| {
let fb = Arc::new(FrameBuffer::new(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT));
let parser_state = ParserState::default();
b.to_async(tokio::runtime::Runtime::new().unwrap())
.iter(|| invoke_parse_pixelflut_commands(input, &fb, parser_state.clone()));
},
);

let mut rng = thread_rng();
draw_commands.shuffle(&mut rng);
let shuffled_draw_commands = draw_commands.join("");

c.bench_with_input(
BenchmarkId::new(
"parse_draw_commands_shuffled",
format!("{FRAMEBUFFER_WIDTH} x {FRAMEBUFFER_HEIGHT}"),
),
&shuffled_draw_commands.as_bytes(),
|b, input| {
let fb = Arc::new(FrameBuffer::new(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT));
let parser_state = ParserState::default();
Expand Down
6 changes: 3 additions & 3 deletions src/test/helpers/pixelflut_commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn get_commands_to_draw_rect(width: usize, height: usize, color: u32) -> String {
let mut draw_commands = String::new();
pub fn get_commands_to_draw_rect(width: usize, height: usize, color: u32) -> Vec<String> {
let mut draw_commands = Vec::new();

for x in 0..width {
for y in 0..height {
draw_commands += &format!("PX {x} {y} {color:06x}\n");
draw_commands.push(format!("PX {x} {y} {color:06x}\n"));
}
}

Expand Down

0 comments on commit a0d86d0

Please sign in to comment.