All Implemented Interfaces:
Direct Known Subclasses:

@AutoService(LogParser.class) public class TCLogParser extends Object implements LogParser

Currently the parser only handles GET/POST requests. It's easy enough to add support for other request methods by changing checkMethod. The is a complete rewrite of a tool I wrote for myself earlier. The older algorithm was basic and did not provide the same level of flexibility I want, so I wrote a new one using a totally new algorithm. This implementation reads one line at a time using BufferedReader. When it gets to the end of the file and the sampler needs to get more requests, the parser will re-initialize the BufferedReader. The implementation uses StringTokenizer to create tokens.

The parse algorithm is the following:

  1. cleans the entry by looking for backslash "\"
  2. looks to see if GET or POST is in the line
  3. tokenizes using quotes "
  4. finds the token with the request method
  5. gets the string of the token and tokenizes it using space
  6. finds the first token beginning with slash character
  7. tokenizes the string using question mark "?"
  8. get the path from the first token
  9. returns the second token and checks it for parameters
  10. tokenizes the string using ampersand "&"
  11. parses each token to name/value pairs

Extending this class is fairly simple. Most access logs use the same format starting from the request method. Therefore, changing the implementation of cleanURL(string) method should be sufficient to support new log formats. Tomcat uses common log format, so any webserver that uses the format should work with this parser. Servers that are known to use non standard formats are IIS and Netscape.

  • Field Details

    • log

      protected static final org.slf4j.Logger log
    • GET

      public static final String GET
      See Also:
    • POST

      public static final String POST
      See Also:

      protected String RMETHOD
      protected members *
    • URL_PATH

      protected String URL_PATH
      The path to the access log file
    • useFILE

      protected boolean useFILE
    • SOURCE

      protected File SOURCE

      protected String FILENAME
    • READER

      protected BufferedReader READER
    • FILTER

      protected Filter FILTER
      Handles to supporting classes
    • decode

      protected boolean decode
      by default, we probably should decode the parameter values
  • Constructor Details

    • TCLogParser

      public TCLogParser()
    • TCLogParser

      public TCLogParser(String source)
      source - name of the source file
  • Method Details

    • setDecodeParameterValues

      public void setDecodeParameterValues(boolean decodeparams)
      by default decode is set to true. if the parameters shouldn't be decoded, call the method with false
      decodeparams - flag whether parameters should be decoded
    • decodeParameterValue

      public boolean decodeParameterValue()
      decode the parameter values is to true by default
      true if parameter values should be decoded, false otherwise
    • setUseParsedFile

      public void setUseParsedFile(boolean file)
      Calls this method to set whether or not to use the path in the log. We may want to provide the ability to filter the log file later on. By default, the parser uses the file in the log.
      file - flag whether to use the path from the log
    • setFilter

      public void setFilter(Filter filter)
      Use the filter to include/exclude files in the access logs. This is provided as a convenience and reduce the need to spend hours cleaning up log files.
      Specified by:
      setFilter in interface LogParser
      filter - Filter to be used while reading the log lines
    • setSourceFile

      public void setSourceFile(String source)
      Sets the source file.
      Specified by:
      setSourceFile in interface LogParser
      source - name of the source file
    • parse

      public int parse(org.apache.jmeter.testelement.TestElement el, int parseCount)
      parse the entire file.
      el - TestElement to read the lines into
      parseCount - number of max lines to read
      number of read lines, or -1 if an error occurred while reading
    • parseAndConfigure

      public int parseAndConfigure(int count, org.apache.jmeter.testelement.TestElement el)
      parse a set number of lines from the access log. Keep in mind the number of lines parsed will depend on the filter and number of lines in the log. The method returns the actual number of lines parsed.
      Specified by:
      parseAndConfigure in interface LogParser
      count - number of lines to read
      el - TestElement to read lines into
      lines parsed
    • parse

      protected int parse(BufferedReader breader, org.apache.jmeter.testelement.TestElement el, int parseCount)
      The method is responsible for reading each line, and breaking out of the while loop if a set number of lines is given.
      Note: empty lines will not be counted
      breader - BufferedReader to read lines from
      el - TestElement to read lines into
      parseCount - number of lines to read
      number of lines parsed
    • parseLine

      protected int parseLine(String line, org.apache.jmeter.testelement.TestElement el)
      parseLine calls the other parse methods to parse the given text.
      line - single line to be parsed
      el - TestElement in which the line will be added
      number of lines parsed (zero or one, actually)
    • cleanURL

      public String cleanURL(String entry)
      The method cleans the URL using the following algorithm.
      1. check for double quotes
      2. check the request method
      3. tokenize using double quotes
      4. find first token containing request method
      5. tokenize string using space
      6. find first token that begins with "/"
      Example Tomcat log entry: - - [08/Jan/2003:07:03:54 -0500] "GET /addrbook/ HTTP/1.1" 200 1981

      would result in the extracted url /addrbook/

      entry - line from which the url is to be extracted
      cleaned url
    • checkMethod

      public boolean checkMethod(String text)
      The method checks for POST, GET and HEAD methods currently. The other methods aren't supported yet.
      text - text to be checked for HTTP method
      true if method is supported, false otherwise
    • stripFile

      public String stripFile(String url, org.apache.jmeter.testelement.TestElement el)
      Tokenize the URL into two tokens. If the URL has more than one "?", the parse may fail. Only the first two tokens are used. The first token is automatically parsed and set at TCLogParser#URL_PATH.
      url - url which should be stripped from parameters
      el - TestElement to parse url into
      String presenting the parameters, or null when none where found
    • checkURL

      public boolean checkURL(String url)
      Checks the string to make sure it has /path/file?name=value format. If the string doesn't contains a "?", it will return false.
      url - url to check for parameters
      true if url contains a ?, false otherwise
    • checkParamFormat

      public boolean checkParamFormat(String text)
      Checks the string to see if it contains "&" and "=". If it does, return true, so that it can be parsed.
      text - text to be checked for & and =
      true if text contains both & and =, false otherwise
    • convertStringToJMRequest

      public void convertStringToJMRequest(String text, org.apache.jmeter.testelement.TestElement el)
      Convert a single line into XML
      text - to be converted
      el - HTTPSamplerBase which consumes the text
    • convertStringtoNVPair

      public NVPair[] convertStringtoNVPair(String stringparams)
      Parse the string parameters into NVPair[] array. Once they are parsed, it is returned. The method uses parseOneParameter(string) to convert each pair.
      stringparams - String with parameters to be parsed
      array of NVPairs
    • parseOneParameter

      protected NVPair parseOneParameter(String parameter)
      Method expects name and value to be separated by an equal sign "=". The method uses StringTokenizer to make a NVPair object. If there happens to be more than one "=" sign, the others are ignored. The chance of a string containing more than one is unlikely and would not conform to HTTP spec. I should double check the protocol spec to make sure this is accurate.
      parameter - to be parsed
      NVPair with the parsed name and value of the parameter
    • parseParameters

      protected List<String> parseParameters(String parameters)
      Method uses StringTokenizer to convert the string into single pairs. The string should conform to HTTP protocol spec, which means the name/value pairs are separated by the ampersand symbol "&". Someone could write the querystrings by hand, but that would be round about and go against the purpose of this utility.
      parameters - string to be parsed
      List of name/value pairs
    • tokenize

      public StringTokenizer tokenize(String line, String delim)
      Parses the line using java.util.StringTokenizer.
      line - line to be parsed
      delim - delimiter
      StringTokenizer constructed with line and delim
    • close

      public void close()
      Description copied from interface: LogParser
      close the any streams or readers.
      Specified by:
      close in interface LogParser