<commit_before>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.groundhog.jmeter;

import io.groundhog.base.URIScheme;
import io.groundhog.replay.AbstractReplayResultListener;
import io.groundhog.replay.UserAgent;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.net.HostAndPort;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.jmeter.samplers.SampleResult;
import org.jsoup.nodes.Document;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Queue;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author <NAME>
 * @since 1.0
 */
public class HarReplayResultListener extends AbstractReplayResultListener {
  private final Queue<SampleResult> resultQueue;
  private final URIScheme scheme;
  private final HostAndPort hostAndPort;

  public HarReplayResultListener(Queue<SampleResult> resultQueue, URIScheme scheme, HostAndPort hostAndPort) {
    this.resultQueue = checkNotNull(resultQueue);
    this.scheme = checkNotNull(scheme);
    this.hostAndPort = checkNotNull(hostAndPort);
  }

  @Override
  public void success(HttpRequest request, HttpResponse response, UserAgent userAgent,
                      int bytesRead, long start, long end, Optional<Document> document) {
    queueResult(request, response, userAgent, bytesRead, start, end, document, Optional.<String>absent());
  }

  @Override
  public void failure(String failureReason, HttpRequest request, HttpResponse response, UserAgent userAgent,
                      int bytesRead, long start, long end, Optional<Document> document) {
    queueResult(request, response, userAgent, bytesRead, start, end, document, Optional.of(failureReason));
  }

  @Override
  public void failure(HttpRequest request, Optional<UserAgent> userAgent, Optional<Throwable> cause) {
    checkNotNull(userAgent);
    SampleResult result = SampleResult.createTestSample(0);
    result.setSuccessful(false);
    result.setSampleLabel(getLabel(request));
    result.setResponseCode("A failure occurred");
    if (cause.isPresent()) {
      Optional<String> knownErrorMessage = getMessageForKnownException(cause.get());
      if (knownErrorMessage.isPresent()) {
        result.setResponseMessage(knownErrorMessage.get());
      } else {
        StringWriter stackTrace = new StringWriter();
        //noinspection ThrowableResultOfMethodCallIgnored
        cause.get().printStackTrace(new PrintWriter(stackTrace));
        result.setResponseMessage(stackTrace.toString());
      }<commit_msg>Add details to sampler label when no response is received<commit_after>/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.groundhog.jmeter;

import io.groundhog.base.URIScheme;
import io.groundhog.replay.AbstractReplayResultListener;
import io.groundhog.replay.UserAgent;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.net.HostAndPort;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.jmeter.samplers.SampleResult;
import org.jsoup.nodes.Document;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Queue;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author <NAME>
 * @since 1.0
 */
public class HarReplayResultListener extends AbstractReplayResultListener {
  private final Queue<SampleResult> resultQueue;
  private final URIScheme scheme;
  private final HostAndPort hostAndPort;

  public HarReplayResultListener(Queue<SampleResult> resultQueue, URIScheme scheme, HostAndPort hostAndPort) {
    this.resultQueue = checkNotNull(resultQueue);
    this.scheme = checkNotNull(scheme);
    this.hostAndPort = checkNotNull(hostAndPort);
  }

  @Override
  public void success(HttpRequest request, HttpResponse response, UserAgent userAgent,
                      int bytesRead, long start, long end, Optional<Document> document) {
    queueResult(request, response, userAgent, bytesRead, start, end, document, Optional.<String>absent());
  }

  @Override
  public void failure(String failureReason, HttpRequest request, HttpResponse response, UserAgent userAgent,
                      int bytesRead, long start, long end, Optional<Document> document) {
    queueResult(request, response, userAgent, bytesRead, start, end, document, Optional.of(failureReason));
  }

  @Override
  public void failure(HttpRequest request, Optional<UserAgent> userAgent, Optional<Throwable> cause) {
    checkNotNull(userAgent);
    SampleResult result = SampleResult.createTestSample(0);
    result.setSuccessful(false);
    result.setSampleLabel(getLabel(request) + " (Error, no response)");
    result.setResponseCode("A failure occurred");
    if (cause.isPresent()) {
      Optional<String> knownErrorMessage = getMessageForKnownException(cause.get());
      if (knownErrorMessage.isPresent()) {
        result.setResponseMessage(knownErrorMessage.get());
      } else {
        StringWriter stackTrace = new StringWriter();
        //noinspection ThrowableResultOfMethodCallIgnored
        cause.get().printStackTrace(new PrintWriter(stackTrace));
        result.setResponseMessage(stackTrace.toString());
      }
{'fraction_non_alphanumeric': 0.09385964912280702, 'fraction_numerical': 0.0043859649122807015, 'mean_word_length': 4.621199671322925, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '533955', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1844, 'n_words': 510}