r4139 - in trunk/varnish-tools/perl/Varnish-API: . t

sky at projects.linpro.no sky at projects.linpro.no
Wed Jul 15 00:55:55 CEST 2009


Author: sky
Date: 2009-07-15 00:55:55 +0200 (Wed, 15 Jul 2009)
New Revision: 4139

Added:
   trunk/varnish-tools/perl/Varnish-API/t/stats.t
Modified:
   trunk/varnish-tools/perl/Varnish-API/API.xs
Log:
support stats, get the stats pointer, get stat fields and get list of stats/type of stats

Modified: trunk/varnish-tools/perl/Varnish-API/API.xs
===================================================================
--- trunk/varnish-tools/perl/Varnish-API/API.xs	2009-07-14 22:04:10 UTC (rev 4138)
+++ trunk/varnish-tools/perl/Varnish-API/API.xs	2009-07-14 22:55:55 UTC (rev 4139)
@@ -32,12 +32,71 @@
     return (rv);
 }
 
+
+SV*
+get_field_type() {
+		 dTHX;
+	     HV* fields;
+	     fields = newHV();
+#define MAC_STAT(type, l, field, description)	\
+	{ \
+	  char * tmp = #field; \
+	  hv_store(fields, #type, strlen(#type), newSVpv(&tmp[1], 1),0); \
+		}\
+		
+#include <varnish/stat_field.h>
+#undef MAC_STAT
+       return newRV_noinc((SV*) fields);
+}
+
+SV*
+get_field_descriptions() {
+		 dTHX;
+	     HV* fields;
+	     fields = newHV();
+#define MAC_STAT(type, l, field, description)	\
+	  hv_store(fields, #type, strlen(#type), newSVpv(description, 0),0); \
+		
+#include <varnish/stat_field.h>
+#undef MAC_STAT
+       return newRV_noinc((SV*) fields);
+}
+
+IV
+get_stat(struct varnish_stats *VSL_stats, const char* stat) {
+
+
+#define MAC_STAT(type, l, field, description)	\
+  if(!strcmp(#type, stat)) {			\
+    return VSL_stats->type;			\
+  }				   \
+			   
+#include <varnish/stat_field.h>
+#undef MAC_STAT
+
+}
+
+
 MODULE = Varnish::API		PACKAGE = Varnish::API		
 
 
 INCLUDE: const-xs.inc
 
+SV*
+VSL_GetStatFieldTypes()
+	CODE:
+	RETVAL = get_field_type();
+	OUTPUT:
+	RETVAL
 
+SV*
+VSL_GetStatFieldDescriptions()
+	CODE:
+	RETVAL = get_field_descriptions();
+	OUTPUT:
+	RETVAL
+	
+
 unsigned int
 SHMLOG_ID(logentry)
 	SV* logentry;
@@ -131,10 +190,26 @@
 	VSL_OpenLog(data, varnish_name);
 
 
-struct varnish_stats *
+SV*
 VSL_OpenStats(varnish_name)
 	const char *	varnish_name
+	PPCODE:
+	struct varnish_stats *stats = VSL_OpenStats(varnish_name);
+	ST(0) = newSViv((IV)stats);
+	sv_2mortal(ST(0));
+	XSRETURN(1);
 
+SV*
+VSL_GetStat(sd, stat)
+	SV* sd
+	const char * stat
+	CODE:
+	struct varnish_stats * stats = (struct varnish_stats *)SvIV(sd);
+	IV nr = get_stat(stats, stat);
+	RETVAL = newSViv(nr);
+	OUTPUT:
+	RETVAL
+
 int
 varnish_instance(n_arg, name, namelen, dir, dirlen)
 	const char *	n_arg

Added: trunk/varnish-tools/perl/Varnish-API/t/stats.t
===================================================================
--- trunk/varnish-tools/perl/Varnish-API/t/stats.t	                        (rev 0)
+++ trunk/varnish-tools/perl/Varnish-API/t/stats.t	2009-07-14 22:55:55 UTC (rev 4139)
@@ -0,0 +1,20 @@
+
+
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+
+use Test::More tests => 4;
+BEGIN { use_ok('Varnish::API') };
+
+use Sys::Hostname qw(hostname);
+
+
+my $stats = Varnish::API::VSL_OpenStats(hostname);
+my $fields = Varnish::API::VSL_GetStatFieldTypes();
+is($fields->{n_smf}, 'i');
+my $description = Varnish::API::VSL_GetStatFieldDescriptions();
+is($description->{n_smf}, 'N struct smf');
+like(Varnish::API::VSL_GetStat($stats, "n_smf"), qr /^\d+$/);



More information about the varnish-commit mailing list