Hi,

This is a revision and update of the original patch as found in (1).

As discussed on irc a while back instead of creating a new function to
handle the gunzip testing, this iteration moves the counters into the
calling code.  I've also expanded the test slightly.

And since I wasn't quite happy with this approach I'm also attaching an
alternative patch (v2) based on the original one.

Comments? OKs? Preferred bike colour?

f.-

1. https://www.varnish-cache.org/patchwork/patch/186/
 bin/varnishd/cache/cache_esi_fetch.c |  1 +
 bin/varnishd/cache/cache_gzip.c      |  8 +++++--
 bin/varnishtest/tests/g00008.vtc     | 45 ++++++++++++++++++++++++++++++++++++
 include/tbl/vsc_f_main.h             |  6 +++++
 4 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/bin/varnishd/cache/cache_esi_fetch.c b/bin/varnishd/cache/cache_esi_fetch.c
index 9091197..18f2357 100644
--- a/bin/varnishd/cache/cache_esi_fetch.c
+++ b/bin/varnishd/cache/cache_esi_fetch.c
@@ -153,6 +153,7 @@ vfp_esi_gzip_init(struct vfp_ctx *vc, struct vfp_entry *vfe)
 	if (vef == NULL)
 		return (VFP_ERROR);
 	vef->vgz = VGZ_NewGzip(vc->wrk->vsl, "G F E");
+	VSC_C_main->n_gzip++;
 	vef->vep = VEP_Init(vc, vc->esi_req, vfp_vep_callback, vef);
 	vef->ibuf_sz = cache_param->gzip_buffer;
 	vef->ibuf = calloc(1L, vef->ibuf_sz);
diff --git a/bin/varnishd/cache/cache_gzip.c b/bin/varnishd/cache/cache_gzip.c
index c32f760..14b89ef 100644
--- a/bin/varnishd/cache/cache_gzip.c
+++ b/bin/varnishd/cache/cache_gzip.c
@@ -86,7 +86,6 @@ VGZ_NewUngzip(struct vsl_log *vsl, const char *id)
 
 	vg = vgz_alloc_vgz(vsl, id);
 	vg->dir = VGZ_UN;
-	VSC_C_main->n_gunzip++;
 
 	/*
 	 * Max memory usage according to zonf.h:
@@ -106,7 +105,6 @@ VGZ_NewGzip(struct vsl_log *vsl, const char *id)
 
 	vg = vgz_alloc_vgz(vsl, id);
 	vg->dir = VGZ_GZ;
-	VSC_C_main->n_gzip++;
 
 	/*
 	 * From zconf.h:
@@ -284,6 +282,7 @@ VDP_gunzip(struct req *req, enum vdp_action act, void **priv,
 
 	if (act == VDP_INIT) {
 		vg = VGZ_NewUngzip(req->vsl, "U D -");
+		VSC_C_main->n_gunzip++;
 		AN(vg);
 		if (vgz_getmbuf(vg))
 			return (-1);
@@ -405,10 +404,15 @@ vfp_gzip_init(struct vfp_ctx *vc, struct vfp_entry *vfe)
 		if (http_GetHdr(vc->http, H_Content_Encoding, NULL))
 			return (VFP_NULL);
 		vg = VGZ_NewGzip(vc->wrk->vsl, vfe->vfp->priv1);
+		VSC_C_main->n_gzip++;
 	} else {
 		if (!http_HdrIs(vc->http, H_Content_Encoding, "gzip"))
 			return (VFP_NULL);
 		vg = VGZ_NewUngzip(vc->wrk->vsl, vfe->vfp->priv1);
+		if (vfe->vfp->priv2 == VFP_GUNZIP)
+			VSC_C_main->n_gunzip++;
+		else
+			VSC_C_main->n_gunzip_test++;
 	}
 	if (vg == NULL)
 		return (VFP_ERROR);
diff --git a/bin/varnishtest/tests/g00008.vtc b/bin/varnishtest/tests/g00008.vtc
new file mode 100644
index 0000000..b821822
--- /dev/null
+++ b/bin/varnishtest/tests/g00008.vtc
@@ -0,0 +1,45 @@
+varnishtest "Test gzip/gunzip counters"
+
+server s1 {
+	rxreq
+	txresp -gzipbody "foo"
+	rxreq
+	txresp -gzipbody "bar"
+	rxreq
+	txresp -gzipbody "baz"
+	rxreq
+	txresp -body "qux"
+} -start
+
+varnish v1 -vcl+backend {
+	sub vcl_backend_response {
+		if (bereq.url == "/baz") {
+			set beresp.do_gunzip = true;
+		} else if (bereq.url == "/qux") {
+			set beresp.do_gzip = true;
+		}
+	}
+} -start
+
+client c1 {
+	# gunzip_test + gunzip (delivery)
+	txreq -url "/foo"
+	rxresp
+	expect resp.body == "foo"
+	# gunzip_test
+	txreq -url "/bar" -hdr "Accept-Encoding: gzip"
+	rxresp
+	expect resp.bodylen == 26
+	# gunzip
+	txreq -url "/baz"
+	rxresp
+	expect resp.body == "baz"
+	# gzip
+	txreq -url "/qux" -hdr "Accept-Encoding: gzip"
+	rxresp
+	expect resp.bodylen == 23
+} -run
+
+varnish v1 -expect n_gzip == 1
+varnish v1 -expect n_gunzip == 2
+varnish v1 -expect n_gunzip_test == 2
diff --git a/include/tbl/vsc_f_main.h b/include/tbl/vsc_f_main.h
index f714e33..4c51830 100644
--- a/include/tbl/vsc_f_main.h
+++ b/include/tbl/vsc_f_main.h
@@ -618,11 +618,17 @@ VSC_F(n_gzip,			uint64_t, 0, 'a', info,
     "Gzip operations",
 	""
 )
+
 VSC_F(n_gunzip,			uint64_t, 0, 'a', info,
     "Gunzip operations",
 	""
 )
 
+VSC_F(n_gunzip_test,		uint64_t, 0, 'a', info,
+    "Gunzip test operations",
+	""
+)
+
 /*--------------------------------------------------------------------*/
 
 VSC_F(vsm_free,			uint64_t, 0, 'g', diag,
 bin/varnishd/cache/cache_gzip.c  | 42 +++++++++++++++++++++++++------------
 bin/varnishtest/tests/g00008.vtc | 45 ++++++++++++++++++++++++++++++++++++++++
 include/tbl/vsc_f_main.h         |  6 ++++++
 3 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/bin/varnishd/cache/cache_gzip.c b/bin/varnishd/cache/cache_gzip.c
index c32f760..30e6be2 100644
--- a/bin/varnishd/cache/cache_gzip.c
+++ b/bin/varnishd/cache/cache_gzip.c
@@ -68,7 +68,7 @@ struct vgz {
  */
 
 static struct vgz *
-vgz_alloc_vgz(struct vsl_log *vsl, const char *id)
+vgz_new_gunzip(struct vsl_log *vsl, const char *id)
 {
 	struct vgz *vg;
 
@@ -76,6 +76,15 @@ vgz_alloc_vgz(struct vsl_log *vsl, const char *id)
 	AN(vg);
 	vg->vsl = vsl;
 	vg->id = id;
+	vg->dir = VGZ_UN;
+
+	/*
+	 * Max memory usage according to zonf.h:
+	 *	mem_needed = "a few kb" + (1 << (windowBits))
+	 * Since we don't control windowBits, we have to assume
+	 * it is 15, so 34-35KB or so.
+	 */
+	assert(Z_OK == inflateInit2(&vg->vz, 31));
 	return (vg);
 }
 
@@ -84,17 +93,18 @@ VGZ_NewUngzip(struct vsl_log *vsl, const char *id)
 {
 	struct vgz *vg;
 
-	vg = vgz_alloc_vgz(vsl, id);
-	vg->dir = VGZ_UN;
+	vg = vgz_new_gunzip(vsl, id);
 	VSC_C_main->n_gunzip++;
+	return (vg);
+}
 
-	/*
-	 * Max memory usage according to zonf.h:
-	 *	mem_needed = "a few kb" + (1 << (windowBits))
-	 * Since we don't control windowBits, we have to assume
-	 * it is 15, so 34-35KB or so.
-	 */
-	assert(Z_OK == inflateInit2(&vg->vz, 31));
+static struct vgz *
+VGZ_NewUngzipTest(struct vsl_log *vsl, const char *id)
+{
+	struct vgz *vg;
+
+	vg = vgz_new_gunzip(vsl, id);
+	VSC_C_main->n_gunzip_test++;
 	return (vg);
 }
 
@@ -104,9 +114,11 @@ VGZ_NewGzip(struct vsl_log *vsl, const char *id)
 	struct vgz *vg;
 	int i;
 
-	vg = vgz_alloc_vgz(vsl, id);
+	ALLOC_OBJ(vg, VGZ_MAGIC);
+	AN(vg);
+	vg->vsl = vsl;
+	vg->id = id;
 	vg->dir = VGZ_GZ;
-	VSC_C_main->n_gzip++;
 
 	/*
 	 * From zconf.h:
@@ -125,6 +137,7 @@ VGZ_NewGzip(struct vsl_log *vsl, const char *id)
 	    cache_param->gzip_memlevel,		/* memLevel */
 	    Z_DEFAULT_STRATEGY);
 	assert(Z_OK == i);
+	VSC_C_main->n_gzip++;
 	return (vg);
 }
 
@@ -408,7 +421,10 @@ vfp_gzip_init(struct vfp_ctx *vc, struct vfp_entry *vfe)
 	} else {
 		if (!http_HdrIs(vc->http, H_Content_Encoding, "gzip"))
 			return (VFP_NULL);
-		vg = VGZ_NewUngzip(vc->wrk->vsl, vfe->vfp->priv1);
+		if (vfe->vfp->priv2 == VFP_GUNZIP)
+			vg = VGZ_NewUngzip(vc->wrk->vsl, vfe->vfp->priv1);
+		else
+			vg = VGZ_NewUngzipTest(vc->wrk->vsl, vfe->vfp->priv1);
 	}
 	if (vg == NULL)
 		return (VFP_ERROR);
diff --git a/bin/varnishtest/tests/g00008.vtc b/bin/varnishtest/tests/g00008.vtc
new file mode 100644
index 0000000..b821822
--- /dev/null
+++ b/bin/varnishtest/tests/g00008.vtc
@@ -0,0 +1,45 @@
+varnishtest "Test gzip/gunzip counters"
+
+server s1 {
+	rxreq
+	txresp -gzipbody "foo"
+	rxreq
+	txresp -gzipbody "bar"
+	rxreq
+	txresp -gzipbody "baz"
+	rxreq
+	txresp -body "qux"
+} -start
+
+varnish v1 -vcl+backend {
+	sub vcl_backend_response {
+		if (bereq.url == "/baz") {
+			set beresp.do_gunzip = true;
+		} else if (bereq.url == "/qux") {
+			set beresp.do_gzip = true;
+		}
+	}
+} -start
+
+client c1 {
+	# gunzip_test + gunzip (delivery)
+	txreq -url "/foo"
+	rxresp
+	expect resp.body == "foo"
+	# gunzip_test
+	txreq -url "/bar" -hdr "Accept-Encoding: gzip"
+	rxresp
+	expect resp.bodylen == 26
+	# gunzip
+	txreq -url "/baz"
+	rxresp
+	expect resp.body == "baz"
+	# gzip
+	txreq -url "/qux" -hdr "Accept-Encoding: gzip"
+	rxresp
+	expect resp.bodylen == 23
+} -run
+
+varnish v1 -expect n_gzip == 1
+varnish v1 -expect n_gunzip == 2
+varnish v1 -expect n_gunzip_test == 2
diff --git a/include/tbl/vsc_f_main.h b/include/tbl/vsc_f_main.h
index f714e33..4c51830 100644
--- a/include/tbl/vsc_f_main.h
+++ b/include/tbl/vsc_f_main.h
@@ -618,11 +618,17 @@ VSC_F(n_gzip,			uint64_t, 0, 'a', info,
     "Gzip operations",
 	""
 )
+
 VSC_F(n_gunzip,			uint64_t, 0, 'a', info,
     "Gunzip operations",
 	""
 )
 
+VSC_F(n_gunzip_test,		uint64_t, 0, 'a', info,
+    "Gunzip test operations",
+	""
+)
+
 /*--------------------------------------------------------------------*/
 
 VSC_F(vsm_free,			uint64_t, 0, 'g', diag,
_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to