001    package jp.osdl.jbento2.analyzer;
002    
003    import java.io.IOException;
004    import java.io.Reader;
005    import java.util.Iterator;
006    import java.util.List;
007    import java.util.regex.Matcher;
008    import java.util.regex.Pattern;
009    
010    import org.apache.commons.io.IOUtils;
011    
012    public class JMeterSampleParser implements SampleParser {
013    
014        private static Pattern timeStampPattern =
015            Pattern.compile("timeStamp=\"(\\d+)\"");
016        private static Pattern timePattern =
017            Pattern.compile("time=\"(\\d+)\"");
018        private static Pattern labelPattern =
019            Pattern.compile("label=\"([^\"]+)\"");
020        private static Pattern threadNamePattern =
021            Pattern.compile("threadName=\"([^\"]+)\"");
022        private static Pattern responseCodePattern =
023            Pattern.compile("responseCode=\"([^\"]+)\"");
024    
025        public SampleList parse(Reader reader) throws IOException {
026            List lines = IOUtils.readLines(reader);
027            SampleList result = new SampleList();
028            for (Iterator it = lines.iterator(); it.hasNext(); ) {
029                String line = (String)it.next();
030                Sample s = new Sample();
031                try {
032                    Matcher m = null;
033    
034                    m = timeStampPattern.matcher(line);
035                    m.find();
036                    long timeStamp = Long.parseLong(m.group(1));
037                    s.addTime(new Time(timeStamp));
038    
039                    m = timePattern.matcher(line);
040                    m.find();
041                    s.addTime(new Time(timeStamp + Long.parseLong(m.group(1))));
042                    m = threadNamePattern.matcher(line);
043    
044                    m.find();
045                    s.setClientId(m.group(1));
046                    m = labelPattern.matcher(line);
047    
048                    m.find();
049                    s.setRequestType(m.group(1));
050                    m = responseCodePattern.matcher(line);
051    
052                    m.find();
053                    s.setError(Integer.parseInt(m.group(1)) >= 400);
054                    result.add(s);
055                } catch (Exception ex) {
056                    // incomplete line - JMeter was killed.
057                    continue;
058                }
059            }
060            return result;
061        }
062    }