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 import java.net.InetAddress; 23 import java.net.UnknownHostException; 24 25 import org.apache.jmeter.threads.JMeterVariables; 26 import org.apache.jmeter.util.JMeterUtils; 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 30 /** 31 * Packages information regarding the target of a sample event, such as the 32 * result from that event and the thread group it ran in. 33 */ 34 public class SampleEvent implements Serializable { 35 private static final Logger log = LoggingManager.getLoggerForClass(); 36 37 private static final long serialVersionUID = 232L; 38 39 public static final String SAMPLE_VARIABLES = "sample_variables"; // $NON-NLS-1$ 40 41 public static final String HOSTNAME; 42 43 // List of variable names to be saved in JTL files 44 private static final String[] variableNames; 45 // Number of variable names 46 private static final int varCount; 47 48 // The values. Entries be null, but there will be the correct number. 49 private final String[] values; 50 51 static { 52 String hn=""; 53 try { 54 hn = InetAddress.getLocalHost().getHostName(); 55 } catch (UnknownHostException e) { 56 log.error("Cannot obtain local host name "+e); 57 } 58 HOSTNAME=hn; 59 60 String vars = JMeterUtils.getProperty(SAMPLE_VARIABLES); 61 variableNames=vars != null ? vars.split(",") : new String[0]; 62 varCount=variableNames.length; 63 if (varCount>0){ 64 log.info(varCount + " sample_variables have been declared: "+vars); 65 } 66 } 67 68 69 private final SampleResult result; 70 71 private final String threadGroup; // TODO appears to duplicate the threadName field in SampleResult 72 73 private final String hostname; 74 75 76 /* 77 * Only for Unit tests 78 */ 79 public SampleEvent() { 80 this(null, null); 81 } 82 83 /** 84 * Creates SampleEvent without saving any variables. 85 * 86 * Use by Proxy and StatisticalSampleSender. 87 * 88 * @param result SampleResult 89 * @param threadGroup name 90 */ 91 public SampleEvent(SampleResult result, String threadGroup) { 92 this.result = result; 93 this.threadGroup = threadGroup; 94 this.hostname = HOSTNAME; 95 values = new String[variableNames.length]; 96 } 97 98 /** 99 * Contructor used for normal samples, saves variable values if any are defined. 100 * 101 * @param result 102 * @param threadGroup name 103 * @param jmvars Jmeter variables 104 */ 105 public SampleEvent(SampleResult result, String threadGroup, JMeterVariables jmvars) { 106 this.result = result; 107 this.threadGroup = threadGroup; 108 this.hostname = HOSTNAME; 109 values = new String[variableNames.length]; 110 saveVars(jmvars); 111 } 112 113 /** 114 * Only intended for use when loading results from a file. 115 * 116 * @param result 117 * @param threadGroup 118 * @param hostname 119 */ 120 public SampleEvent(SampleResult result, String threadGroup, String hostname) { 121 this.result = result; 122 this.threadGroup = threadGroup; 123 this.hostname = hostname; 124 values = new String[variableNames.length]; 125 } 126 127 private void saveVars(JMeterVariables vars){ 128 for(int i = 0; i < variableNames.length; i++){ 129 values[i] = vars.get(variableNames[i]); 130 } 131 } 132 133 /** Return the number of variables defined */ 134 public static int getVarCount(){ 135 return varCount; 136 } 137 138 /** Get the nth variable name (zero-based) */ 139 public static String getVarName(int i){ 140 return variableNames[i]; 141 } 142 143 /** Get the nth variable value (zero-based) */ 144 public String getVarValue(int i){ 145 return values[i]; 146 } 147 148 public SampleResult getResult() { 149 return result; 150 } 151 152 public String getThreadGroup() { 153 return threadGroup; 154 } 155 156 public String getHostname() { 157 return hostname; 158 } 159 }