From 9f5996fe2ae52ff817382d662c956973785a85f0 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 6 Nov 2022 00:15:41 +0800 Subject: [PATCH] Reduce default image size to 220x70 and output JPEG to improve performance (181.238 i/s -> 526.350 i/s). --- README.md | 5 ++--- app/controllers/ru_captcha/captcha_controller.rb | 2 +- ext/rucaptcha/src/captcha.rs | 14 ++++---------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 47188db..d221a24 100644 --- a/README.md +++ b/README.md @@ -169,8 +169,7 @@ end ``` Warming up -------------------------------------- - Generate image 18.000 i/100ms + Generate image 51.000 i/100ms Calculating ------------------------------------- - Generate image (2.6.x C Version) 181.238 (± 2.8%) i/s - 918.000 in 5.069689s - Generate image (3.x Rust version) 165.047 (±15.8%) i/s - 792.000 in 5.003023s + Generate image 526.350 (± 2.5%) i/s - 2.652k in 5.041681s ``` diff --git a/app/controllers/ru_captcha/captcha_controller.rb b/app/controllers/ru_captcha/captcha_controller.rb index 601f1b6..48030e4 100644 --- a/app/controllers/ru_captcha/captcha_controller.rb +++ b/app/controllers/ru_captcha/captcha_controller.rb @@ -7,7 +7,7 @@ def index headers["Pragma"] = "no-cache" data = generate_rucaptcha - opts = { disposition: "inline", type: "image/png" } + opts = { disposition: "inline", type: "image/jpeg" } send_data data, opts end end diff --git a/ext/rucaptcha/src/captcha.rs b/ext/rucaptcha/src/captcha.rs index 7e4da34..474bbd8 100644 --- a/ext/rucaptcha/src/captcha.rs +++ b/ext/rucaptcha/src/captcha.rs @@ -1,6 +1,6 @@ use image::{ImageBuffer, Rgb}; use imageproc::drawing::{draw_cubic_bezier_curve_mut, draw_text_mut}; -use imageproc::noise::{gaussian_noise_mut, salt_and_pepper_noise_mut}; +use imageproc::noise::gaussian_noise_mut; use rand::{thread_rng, Rng}; use rusttype::{Font, Scale}; use std::io::Cursor; @@ -195,8 +195,8 @@ impl CaptchaBuilder { pub fn new() -> Self { CaptchaBuilder { length: 4, - width: 300, - height: 100, + width: 220, + height: 70, complexity: 5, } } @@ -247,15 +247,9 @@ impl CaptchaBuilder { ((5 * self.complexity) - 5) as u64, ); - salt_and_pepper_noise_mut( - &mut image, - ((0.001 * self.complexity as f64) - 0.001) as f64, - (0.8 * self.complexity as f64) as u64, - ); - let mut bytes: Vec = Vec::new(); image - .write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png) + .write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Jpeg) .unwrap(); Captcha { text, image: bytes }