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