r5421 - in trunk/varnish-cache: bin/varnishtest/tests lib/libvmod_std
phk at varnish-cache.org
phk at varnish-cache.org
Mon Oct 11 13:56:58 CEST 2010
Author: phk
Date: 2010-10-11 13:56:58 +0200 (Mon, 11 Oct 2010)
New Revision: 5421
Added:
trunk/varnish-cache/bin/varnishtest/tests/m00002.vtc
Modified:
trunk/varnish-cache/lib/libvmod_std/vmod.vcc
trunk/varnish-cache/lib/libvmod_std/vmod_std.c
Log:
Add REAL std.random(REAL low, REAL high) with associated testcase.
Example usage:
sub vcl_fetch {
/* Spread TTLs -10%..+5% because CMS timestamps whole minutes */
set beresp.ttl = beresp.ttl * random(0.90, 1.05);
}
This usage was one of the very first VCL examples we ever wrote on
a whiteboard, we just never got a round tuit until now.
Fixes: #793
Added: trunk/varnish-cache/bin/varnishtest/tests/m00002.vtc
===================================================================
--- trunk/varnish-cache/bin/varnishtest/tests/m00002.vtc (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/tests/m00002.vtc 2010-10-11 11:56:58 UTC (rev 5421)
@@ -0,0 +1,38 @@
+# $Id$
+
+test "Test std.random"
+
+# needs random generator
+random
+
+server s1 {
+ rxreq
+ txresp -proto HTTP/1.0 -nolen -bodylen 9
+} -start
+
+varnish v1 -vcl+backend {
+ import std from "${topbuild}/lib/libvmod_std/.libs/libvmod_std.so.1" ;
+
+ sub vcl_fetch {
+ set beresp.http.rnd1 = std.random(0,1);
+ set beresp.http.rnd2 = std.random(0,10);
+ set beresp.http.rnd3 = std.random(8,10);
+ set beresp.http.rnd4 = std.random(99,100);
+ }
+} -start
+
+# NB: Do not change the number 1
+# NB: Only srandom(1) is standardized as deterministic.
+varnish v1 -cliok "debug.srandom 1"
+
+client c1 {
+ txreq
+ rxresp
+ expect resp.http.rnd1 == 0.420
+ expect resp.http.rnd2 == 1.972
+ expect resp.http.rnd3 == 8.783
+ expect resp.http.rnd4 == 99.399
+ expect resp.bodylen == 9
+} -run
+
+
Modified: trunk/varnish-cache/lib/libvmod_std/vmod.vcc
===================================================================
--- trunk/varnish-cache/lib/libvmod_std/vmod.vcc 2010-10-11 11:49:21 UTC (rev 5420)
+++ trunk/varnish-cache/lib/libvmod_std/vmod.vcc 2010-10-11 11:56:58 UTC (rev 5421)
@@ -30,3 +30,4 @@
Function STRING toupper(PRIV_CALL, STRING_LIST)
Function STRING tolower(PRIV_VCL, STRING_LIST)
Function VOID set_ip_tos(INT)
+Function REAL random(REAL, REAL)
Modified: trunk/varnish-cache/lib/libvmod_std/vmod_std.c
===================================================================
--- trunk/varnish-cache/lib/libvmod_std/vmod_std.c 2010-10-11 11:49:21 UTC (rev 5420)
+++ trunk/varnish-cache/lib/libvmod_std/vmod_std.c 2010-10-11 11:56:58 UTC (rev 5421)
@@ -120,3 +120,16 @@
priv->free = free;
return (0);
}
+
+double
+vmod_random(struct sess *sp, double lo, double hi)
+{
+ double a;
+
+ (void)sp;
+
+ a = random() / (double)(1ULL<<32);
+ a *= hi - lo;
+ a += lo;
+ return (a);
+}
More information about the varnish-commit
mailing list