| career | drupal | java | mac | mysql | perl | scala | uml | unix  

JMeter example source code file (

This example JMeter source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - JMeter tags/keywords

csv, deprecated, io, may, override, override, overrides, sampleresult, sampleresult, serializable, statisticalsampleresult, statisticalsampleresult, string, stringbuilder, stringbuilder

The JMeter source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.apache.jmeter.samplers;


 * Aggregates sample results for use by the Statistical remote batch mode.
 * Samples are aggregated by the key defined by getKey().
 * TODO: merge error count into parent class?
public class StatisticalSampleResult extends SampleResult implements
        Serializable {

    private static final long serialVersionUID = 240L;

    private int errorCount;

    // Need to maintain our own elapsed timer to ensure more accurate aggregation
    private long elapsed;

    public StatisticalSampleResult(){// May be called by XStream

     * Allow OldSaveService to generate a suitable result when sample/error counts have been saved.
     * @deprecated Needs to be replaced when multiple sample results are sorted out
     * @param stamp
     * @param elapsed
    public StatisticalSampleResult(long stamp, long elapsed) {
        super(stamp, elapsed);

     * Create a statistical sample result from an ordinary sample result.
     * @param res the sample result 
     * @param keyOnThreadName true if key includes threadName, false if threadGroup
    public StatisticalSampleResult(SampleResult res, boolean keyOnThreadName) {
        // Copy data that is shared between samples (i.e. the key items):
        if (keyOnThreadName) {

        setSuccessful(true); // Assume result is OK
        setSampleCount(0); // because we add the sample count in later
        elapsed = 0;

    public void add(SampleResult res) {
        // Add Sample Counter
        setSampleCount(getSampleCount() + res.getSampleCount());

        setBytes(getBytes() + res.getBytes());

        // Add Error Counter
        if (!res.isSuccessful()) {

        // Set start/end times
        if (getStartTime()==0){ // Bug 40954 - ensure start time gets started!
        } else {
            this.setStartTime(Math.min(getStartTime(), res.getStartTime()));
        this.setEndTime(Math.max(getEndTime(), res.getEndTime()));

        setLatency(getLatency()+ res.getLatency());

        elapsed += res.getTime();

    public long getTime() {
        return elapsed;

    public long getTimeStamp() {
        return getEndTime();

    public int getErrorCount() {// Overrides SampleResult
        return errorCount;

    public void setErrorCount(int e) {// for reading CSV files
        errorCount = e;

     * Generates the key to be used for aggregating samples as follows:<br/>
     * <code>sampleLabel "-" [threadName|threadGroup]
     * <p>
     * N.B. the key should agree with the fixed items that are saved in the sample.
     * @param event sample event whose key is to be calculated
     * @param keyOnThreadName true if key should use thread name, otherwise use thread group
     * @return the key to use for aggregating samples
    public static String getKey(SampleEvent event, boolean keyOnThreadName) {
        StringBuilder sb = new StringBuilder(80);
        if (keyOnThreadName){
        } else {
        return sb.toString();

Other JMeter examples (source code examples)

Here is a short list of links related to this JMeter source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.