And use it for automatic output compression in FU, as (potentially) faster alternative to Compress::Raw::Zlib. Was also planning to maybe add support for Zstd or Brotli, but given the performance of libdeflate, I'm not sure that's really necessary. Brotli does tend to do a better job at compressing HTML, though.
47 lines
1 KiB
Perl
47 lines
1 KiB
Perl
use v5.36;
|
|
use Test::More;
|
|
use FU::Util qw/gzip_lib gzip_compress/;
|
|
|
|
like gzip_lib, qr/^(|libdeflate|zlib-ng|zlib)$/, gzip_lib;
|
|
|
|
plan skip_all => 'No suitable gzip library found' if !gzip_lib;
|
|
plan skip_all => 'Compress::Zlib not found' if !eval { require Compress::Zlib };
|
|
|
|
my $incompressible = Compress::Zlib::memGzip(join '', map chr(rand 256), 0..93123);
|
|
|
|
for my $str ('', 'Hello world!', 'x'x4096, $incompressible) {
|
|
is Compress::Zlib::memGunzip(gzip_compress(0, $str)), $str;
|
|
is Compress::Zlib::memGunzip(gzip_compress(12, $str)), $str;
|
|
}
|
|
|
|
|
|
done_testing;
|
|
|
|
|
|
__END__
|
|
|
|
# Test for leaks:
|
|
|
|
use Test::LeakTrace;
|
|
diag count_sv;
|
|
for (0..1000) {
|
|
for my $str ('', 'Hello world!', 'x'x4096, $incompressible) {
|
|
local $_ = gzip_lib;
|
|
$_ = gzip_compress(0, $str);
|
|
$_ = gzip_compress(12, $str);
|
|
}
|
|
}
|
|
diag count_sv;
|
|
|
|
|
|
# Compare performance:
|
|
|
|
use Benchmark 'cmpthese';
|
|
open my $F, '<', 'FU.pm';
|
|
local $/ = undef;
|
|
my $data = <$F>;
|
|
|
|
cmpthese -3, {
|
|
memGzip => 'Compress::Zlib::memGzip($data)',
|
|
gzip_compress => 'gzip_compress(6, $data)',
|
|
};
|