Skip to content

feat(server): add perfenc example #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 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
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/*",
"benches",
"xtask",
"ffi",
]
Expand Down
31 changes: 31 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "benches"
version = "0.1.0"
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
authors.workspace = true
keywords.workspace = true
categories.workspace = true

[[bin]]
name = "perfenc"
path = "src/perfenc.rs"

[dependencies]
anyhow = "1.0.98"
async-trait = "0.1.88"
bytesize = "2.0.1"
ironrdp = { path = "../crates/ironrdp", features = [
"server",
"pdu",
"__bench",
] }
pico-args = "0.5.0"
tokio = { version = "1", features = ["sync", "fs"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing = { version = "0.1", features = ["log"] }

[lints]
workspace = true
193 changes: 193 additions & 0 deletions benches/src/perfenc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#![allow(unused_crate_dependencies)] // False positives because there are both a library and a binary.
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]

use core::time::Duration;
use std::{io::Write, time::Instant};

use anyhow::Context;
use ironrdp::pdu::rdp::capability_sets::{CmdFlags, EntropyBits};
use ironrdp::server::{
bench::encoder::{UpdateEncoder, UpdateEncoderCodecs},
BitmapUpdate, DesktopSize, DisplayUpdate, PixelFormat, RdpServerDisplayUpdates,
};
use tokio::{fs::File, io::AsyncReadExt, time::sleep};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
setup_logging()?;
let mut args = pico_args::Arguments::from_env();

if args.contains(["-h", "--help"]) {
println!("Usage: perfenc [OPTIONS] <RGBX_INPUT_FILENAME>");
println!();
println!("Measure the performance of the IronRDP server encoder, given a raw RGBX video input file.");
println!();
println!("Options:");
println!(" --width <WIDTH> Width of the display (default: 3840)");
println!(" --height <HEIGHT> Height of the display (default: 2400)");
println!(" --codec <CODEC> Codec to use (default: remotefx)");
println!(" Valid values: remotefx, bitmap, none");
println!(" --fps <FPS> Frames per second (default: none)");
std::process::exit(0);
}

let width = args.opt_value_from_str("--width")?.unwrap_or(3840);
let height = args.opt_value_from_str("--height")?.unwrap_or(2400);
let codec = args.opt_value_from_str("--codec")?.unwrap_or_else(OptCodec::default);
let fps = args.opt_value_from_str("--fps")?.unwrap_or(0);

let filename: String = args.free_from_str().context("missing RGBX input filename")?;
let file = File::open(&filename)
.await
.with_context(|| format!("Failed to open file: {}", filename))?;

let mut flags = CmdFlags::all();
let mut update_codecs = UpdateEncoderCodecs::new();

match codec {
OptCodec::RemoteFX => update_codecs.set_remotefx(Some((EntropyBits::Rlgr3, 0))),
OptCodec::Bitmap => {
flags -= CmdFlags::SET_SURFACE_BITS;
}
OptCodec::None => {}
};

let mut encoder = UpdateEncoder::new(DesktopSize { width, height }, flags, update_codecs);

let mut total_raw = 0u64;
let mut total_enc = 0u64;
let mut n_updates = 0u64;
let mut updates = DisplayUpdates::new(file, DesktopSize { width, height }, fps);
while let Some(up) = updates.next_update().await {
if let DisplayUpdate::Bitmap(ref up) = up {
total_raw += up.data.len() as u64;
} else {
eprintln!("Invalid update");
break;
}
let mut iter = encoder.update(up);
loop {
let Some(frag) = iter.next().await else {
break;
};
let len = frag?.data.len() as u64;
total_enc += len;
}
n_updates += 1;
print!(".");
std::io::stdout().flush().unwrap();
}
println!();

let ratio = total_enc as f64 / total_raw as f64;
let percent = 100.0 - ratio * 100.0;
println!("Encoder: {:?}", encoder);
println!("Nb updates: {:?}", n_updates);
println!(
"Sum of bytes: {}/{} ({:.2}%)",
bytesize::ByteSize(total_enc),
bytesize::ByteSize(total_raw),
percent,
);
Ok(())
}

struct DisplayUpdates {
file: File,
desktop_size: DesktopSize,
fps: u64,
last_update_time: Option<Instant>,
}

impl DisplayUpdates {
fn new(file: File, desktop_size: DesktopSize, fps: u64) -> Self {
Self {
file,
desktop_size,
fps,
last_update_time: None,
}
}
}

#[async_trait::async_trait]
impl RdpServerDisplayUpdates for DisplayUpdates {
async fn next_update(&mut self) -> Option<DisplayUpdate> {
let stride = self.desktop_size.width as usize * 4;
let frame_size = stride * self.desktop_size.height as usize;
let mut buf = vec![0u8; frame_size];
if self.file.read_exact(&mut buf).await.is_err() {
return None;
}

let now = Instant::now();
if let Some(last_update_time) = self.last_update_time {
let elapsed = now - last_update_time;
if self.fps > 0 && elapsed < Duration::from_millis(1000 / self.fps) {
sleep(Duration::from_millis(
1000 / self.fps - u64::try_from(elapsed.as_millis()).unwrap(),
))
.await;
}
}
self.last_update_time = Some(now);

let up = DisplayUpdate::Bitmap(BitmapUpdate {
x: 0,
y: 0,
width: self.desktop_size.width.try_into().unwrap(),
height: self.desktop_size.height.try_into().unwrap(),
format: PixelFormat::RgbX32,
data: buf.into(),
stride,
});
Some(up)
}
}

fn setup_logging() -> anyhow::Result<()> {
use tracing::metadata::LevelFilter;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;

let fmt_layer = tracing_subscriber::fmt::layer().compact();

let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::WARN.into())
.with_env_var("IRONRDP_LOG")
.from_env_lossy();

tracing_subscriber::registry()
.with(fmt_layer)
.with(env_filter)
.try_init()
.context("failed to set tracing global subscriber")?;

Ok(())
}

enum OptCodec {
RemoteFX,
Bitmap,
None,
}

impl Default for OptCodec {
fn default() -> Self {
Self::RemoteFX
}
}

impl core::str::FromStr for OptCodec {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"remotefx" => Ok(Self::RemoteFX),
"bitmap" => Ok(Self::Bitmap),
"none" => Ok(Self::None),
_ => Err(anyhow::anyhow!("unknown codec: {}", s)),
}
}
}
3 changes: 2 additions & 1 deletion crates/ironrdp-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rayon = ["dep:rayon"]

# Internal (PRIVATE!) features used to aid testing.
# Don't rely on these whatsoever. They may disappear at any time.
__bench = []
__bench = ["dep:visibility"]

[dependencies]
anyhow = "1.0"
Expand All @@ -46,6 +46,7 @@ x509-cert = { version = "0.2.5", optional = true }
rustls-pemfile = { version = "2.2.0", optional = true }
rayon = { version = "1.10.0", optional = true }
bytes = "1"
visibility = { version = "0.1", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["sync"] }
Expand Down
5 changes: 4 additions & 1 deletion crates/ironrdp-server/src/encoder/fast_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const MAX_FASTPATH_UPDATE_SIZE: usize = 16_374;

const FASTPATH_HEADER_SIZE: usize = 6;

#[allow(unreachable_pub)]
#[cfg_attr(feature = "__bench", visibility::make(pub))]
pub(crate) struct UpdateFragmenter {
code: UpdateCode,
index: usize,
data: Vec<u8>,
#[doc(hidden)] // not part of the public API, used by benchmarks
pub data: Vec<u8>,
position: usize,
}

Expand Down
Loading
Loading