Re: [xslt] profiling info to memory buffer



Daniel Veillard wrote:
>   Hum, I'm unlikely to change it. But providing a patch which gives 
> a cleaner API would be best for long term stability of your code :-)

How about something like the function below? It returns this type of 
document (for my sample input):

<?xml version="1.0"?>
<profile>
<template rank="1" match="*" name=""
	mode="" calls="6" time="48" average="8"/>
<template rank="2" match="item2|item3" name=""
	mode="" calls="10" time="30" average="3"/>
<template rank="3" match="item1" name=""
	mode="" calls="5" time="17" average="3"/>
</profile>



// apply stylesheet -- unfortunatly, FILE* has to be
// non-NULL to get profiling info
r = xsltApplyStylesheetUser(s,d,NULL,NULL,stderr,c);

// call this function to get an xmlDocPtr with the profiling info in it
xmlDocPtr
xsltGetProfileInformation(xsltTransformContextPtr ctxt)
{
	xmlDocPtr ret = NULL;
	xmlNodePtr root, child;
	char buf[4096];
	
	xsltStylesheetPtr s,style;
	xsltTemplatePtr *templates;
	xsltTemplatePtr template;
	int nb=0,max=0,i,j,total,totalt;
	FILE *output = stdout;
	
	nb = 0;
	max = 10000; // MAX_TEMPLATES
	templates = xmlMalloc(max * sizeof(xsltTemplatePtr));
	if (templates == NULL) return;
	
	style = ctxt->style;
	while (style != NULL)
	{
		template = style->templates;
		while (template != NULL)
		{
		    if (nb >= max)
		        break;
		
		    if (template->nbCalls > 0)
		        templates[nb++] = template;
		    template = template->next;
		}
		
		style = (xsltStylesheetPtr) xsltNextImport(style);
	}
	
	for (i = 0;i < nb -1;i++)
	{
		for (j = i + 1; j < nb; j++)
		{
		    if ((templates[i]->time <= templates[j]->time) ||
		        ((templates[i]->time == templates[j]->time) &&
		         (templates[i]->nbCalls <= templates[j]->nbCalls)))
		    {
		        template = templates[j];
		        templates[j] = templates[i];
		        templates[i] = template;
		    }
		}
	}
	ret = xmlNewDoc("1.0");
	root   = xmlNewDocNode(ret,NULL,"profile",NULL);
	xmlDocSetRootElement(ret,root);
	
	total = 0;
	totalt = 0;
	for (i = 0;i < nb;i++)
	{
		child  = xmlNewChild(root,NULL,"template",NULL);
		sprintf(buf,"%d",i+1);
		xmlSetProp(child,"rank",buf);
		xmlSetProp(child,"match",templates[i]->match);
		xmlSetProp(child,"name",templates[i]->name);
		xmlSetProp(child,"mode",templates[i]->mode);
		
		sprintf(buf,"%d",templates[i]->nbCalls);
		xmlSetProp(child,"calls",buf);
		
		sprintf(buf,"%d",templates[i]->time);
		xmlSetProp(child,"time",buf);
		
		sprintf(buf,"%d",templates[i]->time/templates[i]->nbCalls);
		xmlSetProp(child,"average",buf);
	};
	
	return ret;
};





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]