perf: refine benches and add somes test to ensure we are testing the correct inputs

This commit is contained in:
Paul Delafosse 2022-05-13 11:12:20 +02:00
parent ab3d5a67f2
commit 64fdb377dc
2 changed files with 141 additions and 16 deletions

View file

@ -1,5 +1,6 @@
use criterion::{ use criterion::{
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration, black_box, criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion,
PlotConfiguration,
}; };
use freedesktop_icons::lookup; use freedesktop_icons::lookup;
use gtk4::{IconLookupFlags, IconTheme, TextDirection}; use gtk4::{IconLookupFlags, IconTheme, TextDirection};
@ -10,42 +11,60 @@ pub fn bench_lookups(c: &mut Criterion) {
group.plot_config(plot_config); group.plot_config(plot_config);
let args = [ let args = [
"user-home", // (Best case) An icon that can be found in the current theme "user-home", // (Best case) An icon that can be found in the current theme
"video-single-display-symbolic", // An icon that can be found in the parent theme "firefox", // An icon that can be found in the hicolor default theme
"firefox", // An icon that can be found in the hicolor default theme
"archlinux-logo", // An icon that resides in /usr/share/pixmaps "archlinux-logo", // An icon that resides in /usr/share/pixmaps
"not-found", // (Worst case) An icon that does not exist "not-found", // (Worst case) An icon that does not exist
]; ];
for arg in args { for arg in args {
group.bench_with_input(BenchmarkId::new("freedesktop-icons", arg), arg, |b, arg| { group.bench_with_input(BenchmarkId::new("freedesktop-icons", arg), arg, |b, arg| {
b.iter(|| lookup(arg).with_theme("Arc").find()); b.iter(|| {
lookup(black_box(arg))
.with_theme(black_box("Adwaita"))
.find()
});
}); });
group.bench_with_input( group.bench_with_input(
BenchmarkId::new("freedesktop-icons-cache", arg), BenchmarkId::new("freedesktop-icons-cache", arg),
arg, arg,
|b, arg| { |b, arg| {
b.iter(|| lookup(arg).with_theme("Arc").with_cache().find()); b.iter(|| {
lookup(black_box(arg))
.with_scale(black_box(1))
.with_size(black_box(24))
.with_theme(black_box("Adwaita"))
.with_cache()
.find()
});
}, },
); );
group.bench_with_input(BenchmarkId::new("linicon", arg), arg, |b, arg| { group.bench_with_input(BenchmarkId::new("linicon", arg), arg, |b, arg| {
b.iter(|| linicon::lookup_icon(arg).from_theme("Arc").next()); b.iter(|| {
linicon::lookup_icon(black_box(arg))
.from_theme(black_box("Adwaita"))
.with_scale(black_box(1))
.with_size(black_box(24))
.next()
});
}); });
group.bench_with_input(BenchmarkId::new("gtk", arg), arg, |b, arg| { group.bench_with_input(BenchmarkId::new("gtk", arg), arg, |b, arg| {
gtk4::init().unwrap(); gtk4::init().unwrap();
let theme = IconTheme::new(); let theme = IconTheme::new();
b.iter(|| { b.iter(|| {
theme.lookup_icon( theme
arg, .lookup_icon(
&[], black_box(arg),
24, black_box(&[]),
1, black_box(24),
TextDirection::None, black_box(1),
IconLookupFlags::empty(), black_box(TextDirection::None),
) black_box(IconLookupFlags::empty()),
)
.icon_name()
}); });
}); });
} }

106
benches/tests.rs Normal file
View file

@ -0,0 +1,106 @@
use freedesktop_icons::lookup;
use gtk4::{IconLookupFlags, IconTheme, TextDirection};
use speculoos::prelude::*;
use std::path::PathBuf;
#[test]
fn gtk_lookup() {
gtk4::init().unwrap();
let theme = IconTheme::new();
let x = theme.lookup_icon(
"firefox",
&[],
24,
1,
TextDirection::None,
IconLookupFlags::empty(),
);
assert!(x.icon_name().is_some())
}
// Linicon sometimes fails with theme that have unknown parents
// This test only ensure we are running the correct function in the benchmarks
// And results are identical.
#[test]
fn linicon() {
// Current theme
let lin_user_home = linicon::lookup_icon("user-home")
.from_theme("Adwaita")
.with_size(24)
.with_scale(1)
.next();
let user_home = lookup("user-home")
.with_theme("Adwaita")
.with_size(24)
.with_scale(1)
.find();
asserting!("Linicon return some icon")
.that(&lin_user_home.unwrap())
.is_ok()
.map(|icon| &icon.path)
.is_equal_to(PathBuf::from(
"/usr/share/icons/Adwaita/24x24/places/user-home.png",
));
// Fixme
asserting!("Our implementation should return the same result as linicon")
.that(&user_home)
.is_some()
.is_equal_to(PathBuf::from(
"/usr/share/icons/Adwaita/24x24/places/user-home.png",
));
// Fallback to hicolor
let lin_firefox = linicon::lookup_icon("firefox")
.from_theme("Adwaita")
.with_size(24)
.with_scale(1)
.next();
let firefox = lookup("firefox")
.with_theme("Adwaita")
.with_size(24)
.with_scale(1)
.find();
asserting!("Linicon return some icon")
.that(&lin_firefox.unwrap())
.is_ok()
.map(|icon| &icon.path)
.is_equal_to(PathBuf::from(
"/usr/share/icons/hicolor/22x22/apps/firefox.png",
));
asserting!("Our implementation should return the same result as linicon")
.that(&firefox)
.is_some()
.is_equal_to(PathBuf::from(
"/usr/share/icons/hicolor/22x22/apps/firefox.png",
));
// pixmaps
let lin_archlinux = linicon::lookup_icon("archlinux-logo")
.from_theme("Adwaita")
.with_size(24)
.with_scale(1)
.next();
let archlinux = lookup("archlinux-logo")
.with_theme("Adwaita")
.with_size(24)
.with_scale(1)
.find();
asserting!("Linicon fails to fallback to pixmaps")
.that(&lin_archlinux)
.is_none();
asserting!("But we succeed")
.that(&archlinux)
.is_some()
.is_equal_to(PathBuf::from("toto"));
}