1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.samplers; 20 21 import java.io.Serializable; 22 23 /** 24 * Aggregates sample results for use by the Statistical remote batch mode. 25 * Samples are aggregated by the key defined by getKey(). 26 * TODO: merge error count into parent class? 27 */ 28 public class StatisticalSampleResult extends SampleResult implements 29 Serializable { 30 31 private static final long serialVersionUID = 24L; 32 33 private int errorCount; 34 35 public StatisticalSampleResult(){// May be called by XStream 36 } 37 38 /** 39 * Allow OldSaveService to generate a suitable result when sample/error counts have been saved. 40 * 41 * @deprecated Needs to be replaced when multiple sample results are sorted out 42 * 43 * @param stamp 44 * @param elapsed 45 */ 46 public StatisticalSampleResult(long stamp, long elapsed) { 47 super(stamp, elapsed); 48 } 49 50 public StatisticalSampleResult(SampleResult res) { 51 // Copy data that is shared between samples (i.e. the key items): 52 setSampleLabel(res.getSampleLabel()); 53 // Nothing else can be saved, as the samples may come from any thread 54 55 setSuccessful(true); // Assume result is OK 56 setSampleCount(0); // because we add the sample count in later 57 } 58 59 public void add(SampleResult res) { 60 // Add Sample Counter 61 setSampleCount(getSampleCount() + res.getSampleCount()); 62 63 setBytes(getBytes() + res.getBytes()); 64 65 // Add Error Counter 66 if (!res.isSuccessful()) { 67 errorCount++; 68 this.setSuccessful(false); 69 } 70 71 // Set start/end times 72 if (getStartTime()==0){ // Bug 40954 - ensure start time gets started! 73 this.setStartTime(res.getStartTime()); 74 } else { 75 this.setStartTime(Math.min(getStartTime(), res.getStartTime())); 76 } 77 this.setEndTime(Math.max(getEndTime(), res.getEndTime())); 78 79 setLatency(getLatency()+ res.getLatency()); 80 81 } 82 83 public long getTime() { 84 return getEndTime() - getStartTime() - this.getIdleTime(); 85 } 86 87 public long getTimeStamp() { 88 return getEndTime(); 89 } 90 91 public int getErrorCount() {// Overrides SampleResult 92 return errorCount; 93 } 94 95 public void setErrorCount(int e) {// for reading CSV files 96 errorCount = e; 97 } 98 99 /** 100 * Generates the key to be used for aggregating samples as follows:<br/> 101 * <code>sampleLabel</code> "-" <code>threadGroup</code> 102 * 103 * N.B. the key should agree with the fixed items that are saved in the sample. 104 * 105 * @param event sample event whose key is to be calculated 106 * @return the key to use for aggregating samples 107 */ 108 public static String getKey(SampleEvent event) { 109 StringBuffer sb = new StringBuffer(80); 110 sb.append(event.getResult().getSampleLabel()).append("-").append(event.getThreadGroup()); 111 return sb.toString(); 112 } 113 }