v1-0004-Add-basic-test-for-compression-functionality.patch

application/octet-stream

Filename: v1-0004-Add-basic-test-for-compression-functionality.patch
Type: application/octet-stream
Part: 0
Message: libpq compression (part 3)

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v1-0004
Subject: Add basic test for compression functionality
File+
src/interfaces/libpq/Makefile 5 2
src/interfaces/libpq/meson.build 7 1
src/interfaces/libpq/t/005_compression.pl 95 0
src/Makefile.global.in 2 0
From 4496401cfa5c4629d5d1fae568d9fd54d7ff5dc7 Mon Sep 17 00:00:00 2001
From: Jacob Burroughs <jburroughs@instructure.com>
Date: Mon, 18 Dec 2023 14:15:18 -0600
Subject: [PATCH v1 4/5] Add basic test for compression functionality

---
 src/Makefile.global.in                    |  2 +
 src/interfaces/libpq/Makefile             |  7 +-
 src/interfaces/libpq/meson.build          |  8 +-
 src/interfaces/libpq/t/005_compression.pl | 95 +++++++++++++++++++++++
 4 files changed, 109 insertions(+), 3 deletions(-)
 create mode 100644 src/interfaces/libpq/t/005_compression.pl

diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 104e5de0fe..ab21065b72 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -195,9 +195,11 @@ with_ldap	= @with_ldap@
 with_libxml	= @with_libxml@
 with_libxslt	= @with_libxslt@
 with_llvm	= @with_llvm@
+with_lz4	= @with_lz4@
 with_system_tzdata = @with_system_tzdata@
 with_uuid	= @with_uuid@
 with_zlib	= @with_zlib@
+with_zstd	= @with_zstd@
 enable_rpath	= @enable_rpath@
 enable_nls	= @enable_nls@
 enable_debug	= @enable_debug@
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index d0a66cfaa0..b0bcbffd66 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -14,6 +14,9 @@ top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
 export with_ssl
+export with_zlib
+export with_lz4
+export with_zstd
 
 PGFILEDESC = "PostgreSQL Access Library"
 
@@ -81,9 +84,9 @@ endif
 # that are built correctly for use in a shlib.
 SHLIB_LINK_INTERNAL = -lpgcommon_shlib -lpgport_shlib
 ifneq ($(PORTNAME), win32)
-SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
+SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm -lz -llz4 -lzstd, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
 else
-SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
+SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm -lz -llz4 -lzstd $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
 endif
 ifeq ($(PORTNAME), win32)
 SHLIB_LINK += -lshell32 -lws2_32 -lsecur32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
diff --git a/src/interfaces/libpq/meson.build b/src/interfaces/libpq/meson.build
index 80e6a15adf..e9b3c22a52 100644
--- a/src/interfaces/libpq/meson.build
+++ b/src/interfaces/libpq/meson.build
@@ -118,8 +118,14 @@ tests += {
       't/002_api.pl',
       't/003_load_balance_host_list.pl',
       't/004_load_balance_dns.pl',
+      't/005_compression.pl',
     ],
-    'env': {'with_ssl': ssl_library},
+    'env': {
+      'with_ssl': ssl_library,
+      'with_zlib': zlib.found() ? 'yes' : 'no',
+      'with_lz4': lz4.found() ? 'yes' : 'no',
+      'with_zstd': zstd.found() ? 'yes' : 'no',
+    },
   },
 }
 
diff --git a/src/interfaces/libpq/t/005_compression.pl b/src/interfaces/libpq/t/005_compression.pl
new file mode 100644
index 0000000000..f029123d2b
--- /dev/null
+++ b/src/interfaces/libpq/t/005_compression.pl
@@ -0,0 +1,95 @@
+# Copyright (c) 2023, PostgreSQL Global Development Group
+use strict;
+use warnings;
+use Config;
+use PostgreSQL::Test::Utils;
+use PostgreSQL::Test::Cluster;
+use Test::More;
+
+my $withZlib = $ENV{with_zlib} eq 'yes';
+my $withLz4 = $ENV{with_lz4} eq 'yes';
+my $withZstd = $ENV{with_zstd} eq 'yes';
+if (!$withZlib && !$withLz4 && !$withZstd)
+{
+	plan skip_all => 'no compression methods available';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('primary');
+$node->init;
+$node->append_conf('postgresql.conf', "libpq_compression = off");
+$node->start;
+
+# Use a string that any reasonable compression algorithm will compress
+my $compressableString = "a" x 1000;
+
+my $result;
+$result = $node->safe_psql("postgres",
+	"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+	connstr => $node->connstr . " compression=on");
+is( (split "\n", $result)[-1], "f|f", 'successfully does not compress if server-side compression is disabled');
+
+$node->append_conf('postgresql.conf', "libpq_compression = on");
+$node->reload;
+
+my $result;
+$result = $node->safe_psql("postgres",
+	"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+	connstr => $node->connstr . " compression=on");
+is( (split "\n", $result)[-1], "t|t", 'successfully compresses bidirectionally with default algorithms');
+
+my $test_algorithm;
+if ($withZlib)
+{
+	$test_algorithm = "gzip";
+}
+elsif($withLz4)
+{
+	$test_algorithm = "lz4";
+}
+elsif($withZstd)
+{
+	$test_algorithm = "zstd";
+}
+$result = $node->safe_psql("postgres",
+	"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+	connstr => $node->connstr . " compression=none;$test_algorithm");
+is( (split "\n", $result)[-1], "f|t", 'successfully compresses unidirectionally with none as first client algorithm');
+
+$node->append_conf('postgresql.conf', "libpq_compression = 'none;$test_algorithm'");
+$node->reload;
+$result = $node->safe_psql("postgres",
+	"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+	"compression=on");
+is( (split "\n", $result)[-1], "t|f", 'successfully compresses unidirectionally with none as first server algorithm');
+
+$node->append_conf('postgresql.conf', 'libpq_compression = on');
+$node->reload;
+
+SKIP: {
+	skip "gzip not available", 1 unless $ENV{with_zlib};
+
+	$result = $node->safe_psql("postgres",
+		"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+		connstr => $node->connstr . " compression=gzip");
+	is( (split "\n", $result)[-1], "t|t", 'successfully compresses bidirectionally with gzip');
+}
+
+SKIP: {
+	skip "lz4 not available", 1 unless $ENV{with_lz4};
+
+	$result = $node->safe_psql("postgres",
+		"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+		connstr => $node->connstr . " compression=lz4");
+	is( (split "\n", $result)[-1], "t|t", 'successfully compresses bidirectionally with lz4');
+}
+
+SKIP: {
+	skip "zstd not available", 1 unless $ENV{with_zstd};
+
+	$result = $node->safe_psql("postgres",
+		"SELECT '$compressableString'; SELECT rx_socket_bytes < rx_pq_bytes, tx_socket_bytes < tx_pq_bytes FROM pg_stat_network_traffic;",
+		connstr => $node->connstr . " compression=zstd");
+	is( (split "\n", $result)[-1], "t|t", 'successfully compresses bidirectionally with zstd');
+}
+
+done_testing();
-- 
2.42.0