http://qs321.pair.com?node_id=966999

numberuno has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have been following Perl since 1 month, looks interesting. Now, I have a trouble some requirement. 1. read all .log files in a folder (output of Jmeter execution which are basically XML with .log extention) 2. Search for keywords like 2.a. httpSample's attribute value by key lb 2.b. content of failure, error and errormessage tags 3. Print them as report to HTML so that I can send as email Here is the code I have:

#!/usr/lib/perl use strict; use warnings; use Carp; use File::Find; use XML::Parser; use File::Spec::Functions qw( canonpath ); my $failureMessage = ""; my $failure = ""; my $error = ""; my $value = ""; my $element = ""; #chomp $element; if ( @ARGV == 0 ) { push @ARGV, "C:\\Users\\bijoymeethal\\Desktop\\xml test"; warn "Using default path $ARGV[0]\n Usage: $0 path ...\n"; } open(HTML_FILE, ">BAT_Report.html") || die "Can't open file: $!\n" +; # Print the initial HTML tags print HTML_FILE "<html>\n<body>\n<h1>BAT Report - JMeter Test</h1>\n<h +r><br><table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0 +><tr><th><p>TestPlan</p></th><th><p>Test Failed?</p></th><th><p>Failu +reMessage</p></th><th><p>Link to Source</p></th></tr>"; find( sub { return unless ( /[.]log\z/i and -f ); extract_information(); return; }, @ARGV ); sub extract_information { my( $expat, $element, %attrs ) = @_; #my $line = $expat->current_line; if ($element eq "httpSample") { if( %attrs ) { while( my( $key, $value ) = each( %attrs )) { if ($key eq "lb"){ print "\t$key => $value\n"; } } } }elsif ($element eq "failure"){ $failure = $element; }elsif ($element eq "error"){ $error = $element; }elsif ($element eq "failureMessage"){ $failureMessage = $element; } print HTML_FILE <<"EOF"; <tr><td><p>$value</p><td><p>$failure</p></td><td><p>$failureMessage</p +></td></tr> EOF return; } print HTML_FILE "</body><br></html>"; close (HTML_FILE);

I need help to correct this to the requirement and suggestions.

Use of uninitialized value $element in string eq at XMLGrouper_tr1.pl +line 37. Use of uninitialized value $element in string eq at XMLGrouper_tr1.pl +line 45. Use of uninitialized value $element in string eq at XMLGrouper_tr1.pl +line 47. Use of uninitialized value $element in string eq at XMLGrouper_tr1.pl +line 49.

Replies are listed 'Best First'.
Re: trouble,, need help!
by cdarke (Prior) on Apr 25, 2012 at 07:21 UTC
    You have two variables called $element, which is confusing to say the least. One is a global which you set to an empty string, and one is set in the subroutine extract_information which you are setting from the passed arguments. That $element is undef because no arguments are passed.
Re: trouble,, need help!
by nemesdani (Friar) on Apr 25, 2012 at 06:35 UTC
    You call extract_information without arguments -> $element doesn't get a value.


    I'm too lazy to be proud of being impatient.
Re: trouble,, need help! (parsing apache jmeter xml, preparing html report)
by Anonymous Monk on Apr 25, 2012 at 08:19 UTC

    Your program looks like an unfinished thought :) you never setup the xml parser ...

    JMeter docs hint it comes with a mailer and xsl for fomatting report http://svn.apache.org/viewvc/jmeter/trunk/extras/jmeter-results-report.xsl?view=markup, but if you insist on perl, use XML::Twig; # its pure-perl built on top of XML::Parser

    Write it like this, use autodie for automagic error checking

    #!/usr/bin/perl -- use strict; use warnings; use autodie; use XML::Twig; Main( @ARGV ); exit( 0 ); sub Main { parseJmeter( \*DATA, \*STDOUT ); # DEMO } sub parseJmeter { my( $inFilenameOrHandle, $outHandle ) = @_; my( $error, $value, $failure, $failureMessage ) = ("") x 4; # INIT + TO EMPTY my $t = XML::Twig->new( twig_handlers => { 'failure' => sub { warn $_->path; $failure = $_->text; }, 'failureMessage' => sub { warn $_->path; $failureMessage = $_->text; }, 'error' => sub { warn $_->path; $error = $_->text; }, '/*/*//httpSample' => sub { warn $_->path; ## children $value .= $_->att('lb') . "\n"; }, '/*/httpSample' => sub { # TRIGGERED LAST, the daddy httpSampl +e warn 'YO ', $_->path; $value .= $_->att('lb') . "\n"; ## append print $outHandle qq{ <tr><td><p> $value </p> <td><p> $failure </p></td> <td><p> $failureMessage </p></td></tr> }; ( $error, $value, $failure, $failureMessage ) = ("") x 4; + # RESET TO EMPTY }, }, ); $t->xparse( $inFilenameOrHandle ); $t->purge; return; } __END__ <?xml version="1.0" encoding="UTF-8"?> <testResults version="1.2"> -- HTTP Sample, with nested samples <httpSample t="1392" lt="351" ts="1144371014619" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Listen 1-1" dt="text" de="iso-8859-1" by="12407"> <httpSample t="170" lt="170" ts="1144371015471" s="true" lb="http://www.apache.org/style/style.css" rc="200" rm="OK" tn="Listen 1-1" dt="text" de="ISO-8859-1" by="1002"> <responseHeader class="java.lang.String"></responseHeader> <requestHeader class="java.lang.String">MyHeader: MyValue</request +Header> <responseData class="java.lang.String"></responseData> </httpSample> <httpSample t="200" lt="180" ts="1144371015641" s="true" lb="http://www.apache.org/images/asf_logo_wide.gif" rc="200" rm="OK" tn="Listen 1-1" dt="bin" de="ISO-8859-1" by="586 +6"> <responseHeader class="java.lang.String"></responseHeader> </httpSample> <responseHeader class="java.lang.String"></responseHeader> <requestHeader class="java.lang.String">MyHeader: MyValue</requestHe +ader> <responseData class="java.lang.String"></responseData> <cookies class="java.lang.String"></cookies> <method class="java.lang.String">GET</method> <queryString class="java.lang.String"></queryString> <url>http://www.apache.org/</url> </httpSample> </testResults>

    Since http://search.cpan.org/perldoc/XML::Twig#text doesn't return encoded data, you should look into using http://search.cpan.org/perldoc/XML::Twig#html_encode

    I also recommend perlintro, http://learn.perl.org/books/beginning-perl/, http://perl-tutorial.org/, Modern Perl

      thanks appreciated your help.2 things here, when I executed the script with the same sample you have provided, I got this errors:

      Name "main::DATA" used only once: possible typo at d:\xml test\NewTria +l.pl line 10. not well-formed (invalid token) at line 1, column 4, byte 4 at C:/stra +wberry/perl/vendor/lib/XML/Parser.pm line 187 at d:\xml test\NewTrial.pl line 54

      secondly, I am executing Jmeter on commandline so the results are individual xml files, I have to read through all and filter those values. Thanks in advance! Bijoy

        I got this errors: ... not well-formed (

        This means you did not use the download link, you should use it, How does 'Select Code to Download' Work?

        Name "main::DATA" used only once: possible typo ...

        This means you did not have __END__ or __DATA__ section but still used  \*DATA in the program, in other words, you did not run the program I posted

        secondly, I am executing Jmeter on commandline so the results are individual xml files, I have to read through all and filter those values.

        I understood :)