|
| 1 | +#![allow(unused_crate_dependencies)] // False positives because there are both a library and a binary. |
| 2 | +#![allow(clippy::print_stderr)] |
| 3 | +#![allow(clippy::print_stdout)] |
| 4 | + |
| 5 | +use std::io::Write; |
| 6 | + |
| 7 | +use anyhow::Context; |
| 8 | +use ironrdp_pdu::rdp::capability_sets::{CmdFlags, EntropyBits}; |
| 9 | +use ironrdp_server::{ |
| 10 | + bench::encoder::UpdateEncoder, BitmapUpdate, DesktopSize, DisplayUpdate, PixelFormat, RdpServerDisplayUpdates, |
| 11 | +}; |
| 12 | +use tokio::{fs::File, io::AsyncReadExt}; |
| 13 | + |
| 14 | +#[tokio::main(flavor = "current_thread")] |
| 15 | +async fn main() -> Result<(), anyhow::Error> { |
| 16 | + setup_logging()?; |
| 17 | + let mut args = pico_args::Arguments::from_env(); |
| 18 | + |
| 19 | + if args.contains(["-h", "--help"]) { |
| 20 | + println!("Usage: perfenc [OPTIONS] <RGBX_INPUT_FILENAME>"); |
| 21 | + println!(); |
| 22 | + println!("Measure the performance of the IronRDP server encoder, given a raw RGBX video input file."); |
| 23 | + println!(); |
| 24 | + println!("Options:"); |
| 25 | + println!(" --width <WIDTH> Width of the display (default: 3840)"); |
| 26 | + println!(" --height <HEIGHT> Height of the display (default: 2400)"); |
| 27 | + println!(" --codec <CODEC> Codec to use (default: RemoteFX)"); |
| 28 | + println!(" Valid values: RemoteFX, Bitmap, None"); |
| 29 | + std::process::exit(0); |
| 30 | + } |
| 31 | + |
| 32 | + let width = args.opt_value_from_str("--width")?.unwrap_or(3840); |
| 33 | + let height = args.opt_value_from_str("--height")?.unwrap_or(2400); |
| 34 | + let codec = args.opt_value_from_str("--codec")?.unwrap_or_else(OptCodec::default); |
| 35 | + |
| 36 | + let filename: String = args.free_from_str().context("missing RGBX input filename")?; |
| 37 | + let file = File::open(&filename) |
| 38 | + .await |
| 39 | + .with_context(|| format!("Failed to open file: {}", filename))?; |
| 40 | + |
| 41 | + let mut flags = CmdFlags::all(); |
| 42 | + |
| 43 | + #[allow(unused)] |
| 44 | + let (remotefx, qoicodec) = match codec { |
| 45 | + OptCodec::RemoteFX => (Some((EntropyBits::Rlgr3, 0)), None::<u8>), |
| 46 | + OptCodec::Bitmap => { |
| 47 | + flags -= CmdFlags::SET_SURFACE_BITS; |
| 48 | + (None, None) |
| 49 | + } |
| 50 | + OptCodec::None => (None, None), |
| 51 | + }; |
| 52 | + let mut encoder = UpdateEncoder::new(DesktopSize { width, height }, flags, remotefx); |
| 53 | + |
| 54 | + let mut total_raw = 0u64; |
| 55 | + let mut total_enc = 0u64; |
| 56 | + let mut n_updates = 0u64; |
| 57 | + let mut updates = DisplayUpdates::new(file, DesktopSize { width, height }); |
| 58 | + while let Some(up) = updates.next_update().await { |
| 59 | + if let DisplayUpdate::Bitmap(ref up) = up { |
| 60 | + total_raw += up.data.len() as u64; |
| 61 | + } else { |
| 62 | + eprintln!("Invalid update"); |
| 63 | + break; |
| 64 | + } |
| 65 | + let mut iter = encoder.update(up); |
| 66 | + loop { |
| 67 | + let Some(frag) = iter.next().await else { |
| 68 | + break; |
| 69 | + }; |
| 70 | + let len = frag?.data.len() as u64; |
| 71 | + total_enc += len; |
| 72 | + } |
| 73 | + n_updates += 1; |
| 74 | + print!("."); |
| 75 | + std::io::stdout().flush().unwrap(); |
| 76 | + } |
| 77 | + println!(); |
| 78 | + |
| 79 | + let ratio = total_enc as f64 / total_raw as f64; |
| 80 | + let percent = 100.0 - ratio * 100.0; |
| 81 | + println!("Encoder: {:?}", encoder); |
| 82 | + println!("Nb updates: {:?}", n_updates); |
| 83 | + println!( |
| 84 | + "Sum of bytes: {}/{} ({:.2}%)", |
| 85 | + bytesize::ByteSize(total_enc), |
| 86 | + bytesize::ByteSize(total_raw), |
| 87 | + percent, |
| 88 | + ); |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +struct DisplayUpdates { |
| 93 | + file: File, |
| 94 | + desktop_size: DesktopSize, |
| 95 | +} |
| 96 | + |
| 97 | +impl DisplayUpdates { |
| 98 | + fn new(file: File, desktop_size: DesktopSize) -> Self { |
| 99 | + Self { file, desktop_size } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[async_trait::async_trait] |
| 104 | +impl RdpServerDisplayUpdates for DisplayUpdates { |
| 105 | + async fn next_update(&mut self) -> Option<DisplayUpdate> { |
| 106 | + let stride = self.desktop_size.width as usize * 4; |
| 107 | + let frame_size = stride * self.desktop_size.height as usize; |
| 108 | + let mut buf = vec![0u8; frame_size]; |
| 109 | + if self.file.read_exact(&mut buf).await.is_err() { |
| 110 | + return None; |
| 111 | + } |
| 112 | + |
| 113 | + let up = DisplayUpdate::Bitmap(BitmapUpdate { |
| 114 | + x: 0, |
| 115 | + y: 0, |
| 116 | + width: self.desktop_size.width.try_into().unwrap(), |
| 117 | + height: self.desktop_size.height.try_into().unwrap(), |
| 118 | + format: PixelFormat::RgbX32, |
| 119 | + data: buf.into(), |
| 120 | + stride, |
| 121 | + }); |
| 122 | + Some(up) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +fn setup_logging() -> anyhow::Result<()> { |
| 127 | + use tracing::metadata::LevelFilter; |
| 128 | + use tracing_subscriber::prelude::*; |
| 129 | + use tracing_subscriber::EnvFilter; |
| 130 | + |
| 131 | + let fmt_layer = tracing_subscriber::fmt::layer().compact(); |
| 132 | + |
| 133 | + let env_filter = EnvFilter::builder() |
| 134 | + .with_default_directive(LevelFilter::WARN.into()) |
| 135 | + .with_env_var("IRONRDP_LOG") |
| 136 | + .from_env_lossy(); |
| 137 | + |
| 138 | + tracing_subscriber::registry() |
| 139 | + .with(fmt_layer) |
| 140 | + .with(env_filter) |
| 141 | + .try_init() |
| 142 | + .context("failed to set tracing global subscriber")?; |
| 143 | + |
| 144 | + Ok(()) |
| 145 | +} |
| 146 | + |
| 147 | +enum OptCodec { |
| 148 | + RemoteFX, |
| 149 | + Bitmap, |
| 150 | + None, |
| 151 | +} |
| 152 | + |
| 153 | +impl Default for OptCodec { |
| 154 | + fn default() -> Self { |
| 155 | + Self::RemoteFX |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl core::str::FromStr for OptCodec { |
| 160 | + type Err = anyhow::Error; |
| 161 | + |
| 162 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 163 | + match s { |
| 164 | + "remotefx" => Ok(Self::RemoteFX), |
| 165 | + "bitmap" => Ok(Self::Bitmap), |
| 166 | + "none" => Ok(Self::None), |
| 167 | + _ => Err(anyhow::anyhow!("unknown codec: {}", s)), |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments