Home | History | Annotate | Download | only in bridge
      1 /*
      2  * Copyright (C) 2013 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.caliper.bridge;
     18 
     19 import static java.util.concurrent.TimeUnit.SECONDS;
     20 
     21 import com.google.caliper.util.Parser;
     22 import com.google.caliper.util.ShortDuration;
     23 
     24 import java.math.BigDecimal;
     25 import java.util.regex.Matcher;
     26 import java.util.regex.Pattern;
     27 
     28 import javax.inject.Inject;
     29 
     30 /**
     31  * Parses {@link LogMessage} strings.
     32  */
     33 final class LogMessageParser implements Parser<LogMessage> {
     34   @Inject LogMessageParser() {}
     35 
     36   private static final Pattern GC_PATTERN =
     37       Pattern.compile(".*\\[(?:(Full) )?GC.*(\\d+\\.\\d+) secs\\]");
     38   private static final Pattern JIT_PATTERN =
     39       Pattern.compile(".*::.*( \\(((\\d+ bytes)|(static))\\))?");
     40   private static final Pattern VM_OPTION_PATTERN =
     41       Pattern.compile("\\s*(\\w+)\\s+(\\w+)\\s+:?=\\s+([^\\s]*)\\s+\\{([^}]*)\\}\\s*");
     42 
     43   @Override public LogMessage parse(CharSequence text) {
     44     // TODO(gak): do this stuff in terms of CharSequence instead of String
     45     String string = text.toString();
     46     Matcher gcMatcher = GC_PATTERN.matcher(string);
     47     if (gcMatcher.matches()) {
     48       return new GcLogMessage(
     49           "Full".equals(gcMatcher.group(1))
     50               ? GcLogMessage.Type.FULL
     51               : GcLogMessage.Type.INCREMENTAL,
     52           ShortDuration.of(BigDecimal.valueOf(Double.parseDouble(gcMatcher.group(2))), SECONDS));
     53     }
     54     Matcher jitMatcher = JIT_PATTERN.matcher(string);
     55     if (jitMatcher.matches()) {
     56       return new HotspotLogMessage();
     57     }
     58     Matcher vmOptionMatcher = VM_OPTION_PATTERN.matcher(string);
     59     if (vmOptionMatcher.matches()) {
     60       return new VmOptionLogMessage(vmOptionMatcher.group(2), vmOptionMatcher.group(3));
     61     }
     62     return null;
     63   }
     64 }
     65