You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Noel O'Brien <no...@newbay.com> on 2009/07/22 09:23:04 UTC

Executing XPath Expressions in a Beanshell Post-Processor

Hi, 

I have to extract an correlate many values from an XML feed. I then need to sort the entries, store them, and compare them against subsequent feeds. 

Given the complexity of the task, I decided to do it with a Beanshell Post-Processor. I first spiked the code in Java and everything worked. However when I moved it into JMeter/Beanshell, the evaluation of the XPath expressions always returns 0 nodes. Making the Document namespace-aware makes no difference. The document parses fine, and printing out "new String(data, "UTF-8")" displays the correct data returned by the sampler. 

Can anyone think of any reasons why this could be happening? 

Here's the code: 

>>>>>>>>>>>>>>>>>>>> 

import org.w3c.dom.*; 
import javax.xml.xpath.*; 
import javax.xml.parsers.*; 
import java.io.*; 
import java.nio.*; 
import java.nio.channels.*; 
import org.xml.sax.*; 


log.info("Starting "+getSourceFileInfo()); 
log.debug("Parameters: " + Parameters); 
startTime = System.currentTimeMillis(); 

params = Parameters.split("\\|"); 

sns = params[0]; 

// Method for retrieving Node text value 
public static String getNodeValue(Node node) 
{ 
if(!node.hasChildNodes()) return ""; 
else return node.getFirstChild().getNodeValue(); 
} 

// Build XML document from the response 
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
domFactory.setNamespaceAware(false); 
DocumentBuilder builder = domFactory.newDocumentBuilder(); 
InputSource inStream = new InputSource(); 
inStream.setCharacterStream(new StringReader(new String(data, "UTF-8"))); 
Document doc = builder.parse(inStream); 
XPath xpath = XPathFactory.newInstance().newXPath(); 


// Create XPath Expressions 
XPathExpression updatedExpr = xpath.compile("/feed/entry/updated"); 
XPathExpression displayNameExpr = xpath.compile("/feed/entry/author/name"); 
XPathExpression statusExpr = xpath.compile("/feed/entry/summary"); 
XPathExpression snsExpr = xpath.compile("/feed/entry/source/author/name"); 

// Evaluate XPath Expressions 
NodeList updatedNodes = (NodeList)updatedExpr.evaluate(doc, XPathConstants.NODESET); 
NodeList displayNameNodes = (NodeList)displayNameExpr.evaluate(doc, XPathConstants.NODESET); 
NodeList statusNodes = (NodeList)statusExpr.evaluate(doc, XPathConstants.NODESET); 
NodeList snsNodes = (NodeList)snsExpr.evaluate(doc, XPathConstants.NODESET); 

System.out.println("updatedNodes: "+updatedNodes.getLength()); 
System.out.println("displayNameNodes: "+displayNameNodes.getLength()); 
System.out.println("statusNodes: "+statusNodes.getLength()); 
System.out.println("snsNodes: "+snsNodes.getLength()); 

// Create data types 
/* HashSet<List> set = new HashSet<List>(); 
TreeMap<Date, HashSet<List>> tree = new TreeMap<Date, HashSet<List>>();*/ 

// Put values in data structure 
for(int i =0; i < updatedNodes.getLength(); i++) 
{ 
String u = getNodeValue(updatedNodes.item(i)); 
String d = getNodeValue(displayNameNodes.item(i)); 
String s1 = getNodeValue(statusNodes.item(i)); 
String s2 = getNodeValue(snsNodes.item(i)); 

// do more stuff 
} 


// Store tree in Object variable 
//vars.putObject("individual.friends.statuses.tree", tree); 

finishTime = System.currentTimeMillis(); 
log.info("Finished "+getSourceFileInfo()+" (" + (finishTime-startTime) + "ms)"); 

<<<<<<<<<<<<<<<<<<<< 

JMeter: 2.3.2 
Java 1.6.0_12 
OS: Linux 

Regards, 
Noel 

Re: Executing XPath Expressions in a Beanshell Post-Processor

Posted by sebb <se...@gmail.com>.
On 24/07/2009, Noel O'Brien <no...@newbay.com> wrote:
> Hi,
>
>  There are no errors in the jmeter log, only info logged by the script:
>
>  2009/07/24 10:52:23 INFO - jmeter.util.BeanShellTestElement: Starting /home/nobrien/projects/newbay/main/sng/sng-acceptance-tests/test/jmeter/data/scripts/ExtractOrderedEntries.bsh
>  2009/07/24 10:52:23 DEBUG - jmeter.util.BeanShellTestElement: Parameters:
>  2009/07/24 10:52:24 INFO - jmeter.util.BeanShellTestElement: Finished /home/nobrien/projects/newbay/main/sng/sng-acceptance-tests/test/jmeter/data/scripts/ExtractOrderedEntries.bsh (341ms)
>
>  However I was playing around and think I may have a lead. I developed the spike script in JEdit and used it's built-in console for running it (this is where everything worked). I discovered though that JEdit seems to be using a different version of Java:
>
>  In JEdit Console:
>
>  /home/nobrien/sandbox/Java/XPath> java -version
>  java version "1.6.0_0"
>  OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu10)
>  OpenJDK Server VM (build 14.0-b08, mixed mode)
>  Process java exited with code 0
>
>  In system console:
>
>  nobrien@olympia:~/sandbox/Java/XPath$ java -version
>  java version "1.6.0_12"
>  Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
>  Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)
>
>  Strangely, the XPath expressions successfully match nodes when the program is run from the IcedTea6 JVM but match nothing when run from the Sun 6 JVM. JMeter is run using the Sun 6 JVM which would explain why the beanshell in my JMeter plan is nbot matching anything either.
>
>  I'm not really sure where to go from here: is it a classpath issue or perhaps a bug in the Sun6 JDK? Does anyone have any suggestions on what to try?

Does XPath Extractor work OK if used to extract one set of nodes (add
a Debug Sampler to show the variables it sets up)?
If so, you could use 4 of them and then process the variables using BeanShell.

Or you could extract the code and use it in BeanShell.

Note that you can also run BeanShell in stand-alone mode for testing.

>  Regards,
>  Noel
>
>
>  ----- "sebb" <se...@gmail.com> wrote:
>  > Check jmeter log file for errors from BeanShell.
>  >
>  > Are you seeing the final log message from the BeanShell script?
>  >
>  > On 22/07/2009, Noel O'Brien <no...@newbay.com> wrote:
>  > > Hi,
>  > >
>  > > I have to extract an correlate many values from an XML feed. I then need to sort the entries, store them, and compare them against subsequent feeds.
>  > >
>  > > Given the complexity of the task, I decided to do it with a Beanshell Post-Processor. I first spiked the code in Java and everything worked. However when I moved it into JMeter/Beanshell, the evaluation of the XPath expressions always returns 0 nodes. Making the Document namespace-aware makes no difference. The document parses fine, and printing out "new String(data, "UTF-8")" displays the correct data returned by the sampler.
>  > >
>  > > Can anyone think of any reasons why this could be happening?
>  > >
>  > > Here's the code:
>  > >
>  > > >>>>>>>>>>>>>>>>>>>>
>  > >
>  > > import org.w3c.dom.*;
>  > > import javax.xml.xpath.*;
>  > > import javax.xml.parsers.*;
>  > > import java.io.*;
>  > > import java.nio.*;
>  > > import java.nio.channels.*;
>  > > import org.xml.sax.*;
>  > >
>  > >
>  > > log.info("Starting "+getSourceFileInfo());
>  > > log.debug("Parameters: " + Parameters);
>  > > startTime = System.currentTimeMillis();
>  > >
>  > > params = Parameters.split("\\|");
>  > >
>  > > sns = params[0];
>  > >
>  > > // Method for retrieving Node text value
>  > > public static String getNodeValue(Node node)
>  > > {
>  > > if(!node.hasChildNodes()) return "";
>  > > else return node.getFirstChild().getNodeValue();
>  > > }
>  > >
>  > > // Build XML document from the response
>  > > DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
>  > > domFactory.setNamespaceAware(false);
>  > > DocumentBuilder builder = domFactory.newDocumentBuilder();
>  > > InputSource inStream = new InputSource();
>  > > inStream.setCharacterStream(new StringReader(new String(data, "UTF-8")));
>  > > Document doc = builder.parse(inStream);
>  > > XPath xpath = XPathFactory.newInstance().newXPath();
>  > >
>  > >
>  > > // Create XPath Expressions
>  > > XPathExpression updatedExpr = xpath.compile("/feed/entry/updated");
>  > > XPathExpression displayNameExpr = xpath.compile("/feed/entry/author/name");
>  > > XPathExpression statusExpr = xpath.compile("/feed/entry/summary");
>  > > XPathExpression snsExpr = xpath.compile("/feed/entry/source/author/name");
>  > >
>  > > // Evaluate XPath Expressions
>  > > NodeList updatedNodes = (NodeList)updatedExpr.evaluate(doc, XPathConstants.NODESET);
>  > > NodeList displayNameNodes = (NodeList)displayNameExpr.evaluate(doc, XPathConstants.NODESET);
>  > > NodeList statusNodes = (NodeList)statusExpr.evaluate(doc, XPathConstants.NODESET);
>  > > NodeList snsNodes = (NodeList)snsExpr.evaluate(doc, XPathConstants.NODESET);
>  > >
>  > > System.out.println("updatedNodes: "+updatedNodes.getLength());
>  > > System.out.println("displayNameNodes: "+displayNameNodes.getLength());
>  > > System.out.println("statusNodes: "+statusNodes.getLength());
>  > > System.out.println("snsNodes: "+snsNodes.getLength());
>  > >
>  > > // Create data types
>  > > /* HashSet<List> set = new HashSet<List>();
>  > > TreeMap<Date, HashSet<List>> tree = new TreeMap<Date, HashSet<List>>();*/
>  > >
>  > > // Put values in data structure
>  > > for(int i =0; i < updatedNodes.getLength(); i++)
>  > > {
>  > > String u = getNodeValue(updatedNodes.item(i));
>  > > String d = getNodeValue(displayNameNodes.item(i));
>  > > String s1 = getNodeValue(statusNodes.item(i));
>  > > String s2 = getNodeValue(snsNodes.item(i));
>  > >
>  > > // do more stuff
>  > > }
>  > >
>  > >
>  > > // Store tree in Object variable
>  > > //vars.putObject("individual.friends.statuses.tree", tree);
>  > >
>  > > finishTime = System.currentTimeMillis();
>  > > log.info("Finished "+getSourceFileInfo()+" (" + (finishTime-startTime) + "ms)");
>  > >
>  > > <<<<<<<<<<<<<<<<<<<<
>  > >
>  > > JMeter: 2.3.2
>  > > Java 1.6.0_12
>  > > OS: Linux
>  > >
>  > > Regards,
>  > >
>  > > Noel
>  > >
>  >
>
> > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
>  > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>  >
>  >

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: Executing XPath Expressions in a Beanshell Post-Processor

Posted by Noel O'Brien <no...@newbay.com>.
Hi, 

There are no errors in the jmeter log, only info logged by the script: 

2009/07/24 10:52:23 INFO - jmeter.util.BeanShellTestElement: Starting /home/nobrien/projects/newbay/main/sng/sng-acceptance-tests/test/jmeter/data/scripts/ExtractOrderedEntries.bsh 
2009/07/24 10:52:23 DEBUG - jmeter.util.BeanShellTestElement: Parameters: 
2009/07/24 10:52:24 INFO - jmeter.util.BeanShellTestElement: Finished /home/nobrien/projects/newbay/main/sng/sng-acceptance-tests/test/jmeter/data/scripts/ExtractOrderedEntries.bsh (341ms) 

However I was playing around and think I may have a lead. I developed the spike script in JEdit and used it's built-in console for running it (this is where everything worked). I discovered though that JEdit seems to be using a different version of Java: 

In JEdit Console: 

/home/nobrien/sandbox/Java/XPath> java -version 
java version "1.6.0_0" 
OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu10) 
OpenJDK Server VM (build 14.0-b08, mixed mode) 
Process java exited with code 0 

In system console: 

nobrien@olympia:~/sandbox/Java/XPath$ java -version 
java version "1.6.0_12" 
Java(TM) SE Runtime Environment (build 1.6.0_12-b04) 
Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode) 

Strangely, the XPath expressions successfully match nodes when the program is run from the IcedTea6 JVM but match nothing when run from the Sun 6 JVM. JMeter is run using the Sun 6 JVM which would explain why the beanshell in my JMeter plan is nbot matching anything either. 

I'm not really sure where to go from here: is it a classpath issue or perhaps a bug in the Sun6 JDK? Does anyone have any suggestions on what to try? 

Regards, 
Noel 

----- "sebb" <se...@gmail.com> wrote: 
> Check jmeter log file for errors from BeanShell. 
> 
> Are you seeing the final log message from the BeanShell script? 
> 
> On 22/07/2009, Noel O'Brien <no...@newbay.com> wrote: 
> > Hi, 
> > 
> > I have to extract an correlate many values from an XML feed. I then need to sort the entries, store them, and compare them against subsequent feeds. 
> > 
> > Given the complexity of the task, I decided to do it with a Beanshell Post-Processor. I first spiked the code in Java and everything worked. However when I moved it into JMeter/Beanshell, the evaluation of the XPath expressions always returns 0 nodes. Making the Document namespace-aware makes no difference. The document parses fine, and printing out "new String(data, "UTF-8")" displays the correct data returned by the sampler. 
> > 
> > Can anyone think of any reasons why this could be happening? 
> > 
> > Here's the code: 
> > 
> > >>>>>>>>>>>>>>>>>>>> 
> > 
> > import org.w3c.dom.*; 
> > import javax.xml.xpath.*; 
> > import javax.xml.parsers.*; 
> > import java.io.*; 
> > import java.nio.*; 
> > import java.nio.channels.*; 
> > import org.xml.sax.*; 
> > 
> > 
> > log.info("Starting "+getSourceFileInfo()); 
> > log.debug("Parameters: " + Parameters); 
> > startTime = System.currentTimeMillis(); 
> > 
> > params = Parameters.split("\\|"); 
> > 
> > sns = params[0]; 
> > 
> > // Method for retrieving Node text value 
> > public static String getNodeValue(Node node) 
> > { 
> > if(!node.hasChildNodes()) return ""; 
> > else return node.getFirstChild().getNodeValue(); 
> > } 
> > 
> > // Build XML document from the response 
> > DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
> > domFactory.setNamespaceAware(false); 
> > DocumentBuilder builder = domFactory.newDocumentBuilder(); 
> > InputSource inStream = new InputSource(); 
> > inStream.setCharacterStream(new StringReader(new String(data, "UTF-8"))); 
> > Document doc = builder.parse(inStream); 
> > XPath xpath = XPathFactory.newInstance().newXPath(); 
> > 
> > 
> > // Create XPath Expressions 
> > XPathExpression updatedExpr = xpath.compile("/feed/entry/updated"); 
> > XPathExpression displayNameExpr = xpath.compile("/feed/entry/author/name"); 
> > XPathExpression statusExpr = xpath.compile("/feed/entry/summary"); 
> > XPathExpression snsExpr = xpath.compile("/feed/entry/source/author/name"); 
> > 
> > // Evaluate XPath Expressions 
> > NodeList updatedNodes = (NodeList)updatedExpr.evaluate(doc, XPathConstants.NODESET); 
> > NodeList displayNameNodes = (NodeList)displayNameExpr.evaluate(doc, XPathConstants.NODESET); 
> > NodeList statusNodes = (NodeList)statusExpr.evaluate(doc, XPathConstants.NODESET); 
> > NodeList snsNodes = (NodeList)snsExpr.evaluate(doc, XPathConstants.NODESET); 
> > 
> > System.out.println("updatedNodes: "+updatedNodes.getLength()); 
> > System.out.println("displayNameNodes: "+displayNameNodes.getLength()); 
> > System.out.println("statusNodes: "+statusNodes.getLength()); 
> > System.out.println("snsNodes: "+snsNodes.getLength()); 
> > 
> > // Create data types 
> > /* HashSet<List> set = new HashSet<List>(); 
> > TreeMap<Date, HashSet<List>> tree = new TreeMap<Date, HashSet<List>>();*/ 
> > 
> > // Put values in data structure 
> > for(int i =0; i < updatedNodes.getLength(); i++) 
> > { 
> > String u = getNodeValue(updatedNodes.item(i)); 
> > String d = getNodeValue(displayNameNodes.item(i)); 
> > String s1 = getNodeValue(statusNodes.item(i)); 
> > String s2 = getNodeValue(snsNodes.item(i)); 
> > 
> > // do more stuff 
> > } 
> > 
> > 
> > // Store tree in Object variable 
> > //vars.putObject("individual.friends.statuses.tree", tree); 
> > 
> > finishTime = System.currentTimeMillis(); 
> > log.info("Finished "+getSourceFileInfo()+" (" + (finishTime-startTime) + "ms)"); 
> > 
> > <<<<<<<<<<<<<<<<<<<< 
> > 
> > JMeter: 2.3.2 
> > Java 1.6.0_12 
> > OS: Linux 
> > 
> > Regards, 
> > 
> > Noel 
> > 
> 
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org 
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org 
> 
> 

Re: Executing XPath Expressions in a Beanshell Post-Processor

Posted by sebb <se...@gmail.com>.
Check jmeter log file for errors from BeanShell.

Are you seeing the final log message from the BeanShell script?

On 22/07/2009, Noel O'Brien <no...@newbay.com> wrote:
> Hi,
>
>  I have to extract an correlate many values from an XML feed. I then need to sort the entries, store them, and compare them against subsequent feeds.
>
>  Given the complexity of the task, I decided to do it with a Beanshell Post-Processor. I first spiked the code in Java and everything worked. However when I moved it into JMeter/Beanshell, the evaluation of the XPath expressions always returns 0 nodes. Making the Document namespace-aware makes no difference. The document parses fine, and printing out "new String(data, "UTF-8")" displays the correct data returned by the sampler.
>
>  Can anyone think of any reasons why this could be happening?
>
>  Here's the code:
>
>  >>>>>>>>>>>>>>>>>>>>
>
>  import org.w3c.dom.*;
>  import javax.xml.xpath.*;
>  import javax.xml.parsers.*;
>  import java.io.*;
>  import java.nio.*;
>  import java.nio.channels.*;
>  import org.xml.sax.*;
>
>
>  log.info("Starting "+getSourceFileInfo());
>  log.debug("Parameters: " + Parameters);
>  startTime = System.currentTimeMillis();
>
>  params = Parameters.split("\\|");
>
>  sns = params[0];
>
>  // Method for retrieving Node text value
>  public static String getNodeValue(Node node)
>  {
>  if(!node.hasChildNodes()) return "";
>  else return node.getFirstChild().getNodeValue();
>  }
>
>  // Build XML document from the response
>  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
>  domFactory.setNamespaceAware(false);
>  DocumentBuilder builder = domFactory.newDocumentBuilder();
>  InputSource inStream = new InputSource();
>  inStream.setCharacterStream(new StringReader(new String(data, "UTF-8")));
>  Document doc = builder.parse(inStream);
>  XPath xpath = XPathFactory.newInstance().newXPath();
>
>
>  // Create XPath Expressions
>  XPathExpression updatedExpr = xpath.compile("/feed/entry/updated");
>  XPathExpression displayNameExpr = xpath.compile("/feed/entry/author/name");
>  XPathExpression statusExpr = xpath.compile("/feed/entry/summary");
>  XPathExpression snsExpr = xpath.compile("/feed/entry/source/author/name");
>
>  // Evaluate XPath Expressions
>  NodeList updatedNodes = (NodeList)updatedExpr.evaluate(doc, XPathConstants.NODESET);
>  NodeList displayNameNodes = (NodeList)displayNameExpr.evaluate(doc, XPathConstants.NODESET);
>  NodeList statusNodes = (NodeList)statusExpr.evaluate(doc, XPathConstants.NODESET);
>  NodeList snsNodes = (NodeList)snsExpr.evaluate(doc, XPathConstants.NODESET);
>
>  System.out.println("updatedNodes: "+updatedNodes.getLength());
>  System.out.println("displayNameNodes: "+displayNameNodes.getLength());
>  System.out.println("statusNodes: "+statusNodes.getLength());
>  System.out.println("snsNodes: "+snsNodes.getLength());
>
>  // Create data types
>  /* HashSet<List> set = new HashSet<List>();
>  TreeMap<Date, HashSet<List>> tree = new TreeMap<Date, HashSet<List>>();*/
>
>  // Put values in data structure
>  for(int i =0; i < updatedNodes.getLength(); i++)
>  {
>  String u = getNodeValue(updatedNodes.item(i));
>  String d = getNodeValue(displayNameNodes.item(i));
>  String s1 = getNodeValue(statusNodes.item(i));
>  String s2 = getNodeValue(snsNodes.item(i));
>
>  // do more stuff
>  }
>
>
>  // Store tree in Object variable
>  //vars.putObject("individual.friends.statuses.tree", tree);
>
>  finishTime = System.currentTimeMillis();
>  log.info("Finished "+getSourceFileInfo()+" (" + (finishTime-startTime) + "ms)");
>
>  <<<<<<<<<<<<<<<<<<<<
>
>  JMeter: 2.3.2
>  Java 1.6.0_12
>  OS: Linux
>
>  Regards,
>
> Noel
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org