1 18 19 package org.apache.jmeter.modifiers; 20 21 import java.io.Serializable ; 22 23 import org.apache.jmeter.engine.event.LoopIterationEvent; 24 import org.apache.jmeter.engine.event.LoopIterationListener; 25 import org.apache.jmeter.engine.util.NoThreadClone; 26 import org.apache.jmeter.testelement.AbstractTestElement; 27 import org.apache.jmeter.testelement.property.BooleanProperty; 28 import org.apache.jmeter.testelement.property.IntegerProperty; 29 import org.apache.jmeter.threads.JMeterContextService; 30 import org.apache.jmeter.threads.JMeterVariables; 31 import org.apache.jorphan.logging.LoggingManager; 32 import org.apache.log.Logger; 33 34 37 public class CounterConfig 38 extends AbstractTestElement 39 implements Serializable , LoopIterationListener, NoThreadClone 40 { 41 private static Logger log = LoggingManager.getLoggerForClass(); 42 public final static String START = "CounterConfig.start"; 43 public final static String END = "CounterConfig.end"; 44 public final static String INCREMENT = "CounterConfig.incr"; 45 public final static String PER_USER = "CounterConfig.per_user"; 46 public final static String VAR_NAME = "CounterConfig.name"; 47 48 private int globalCounter = -1; 49 50 53 public synchronized void iterationStart(LoopIterationEvent event) 54 { 55 JMeterVariables variables = 57 JMeterContextService.getContext().getVariables(); 58 int start = getStart(), end = getEnd(), increment = getIncrement(); 59 if (!isPerUser()) 60 { 61 if (globalCounter == -1 || globalCounter > end) 62 { 63 globalCounter = start; 64 } 65 variables.put(getVarName(), Integer.toString(globalCounter)); 66 globalCounter += increment; 67 } 68 else 69 { 70 String value = variables.get(getVarName()); 71 if (value == null) 72 { 73 variables.put(getVarName(), Integer.toString(start)); 74 } 75 else 76 { 77 try 78 { 79 int current = Integer.parseInt(value); 80 current += increment; 81 if (current > end) 82 { 83 current = start; 84 } 85 variables.put(getVarName(), Integer.toString(current)); 86 } 87 catch (NumberFormatException e) 88 { 89 log.info("Bad number in Counter config", e); 90 } 91 } 92 } 93 } 94 95 public void setStart(int start) 96 { 97 setProperty(new IntegerProperty(START, start)); 98 } 99 100 public void setStart(String start) 101 { 102 setProperty(START, start); 103 } 104 105 public int getStart() 106 { 107 return getPropertyAsInt(START); 108 } 109 110 public void setEnd(int end) 111 { 112 setProperty(new IntegerProperty(END, end)); 113 } 114 115 public void setEnd(String end) 116 { 117 setProperty(END, end); 118 } 119 120 public int getEnd() 121 { 122 return getPropertyAsInt(END); 123 } 124 125 public void setIncrement(int inc) 126 { 127 setProperty(new IntegerProperty(INCREMENT, inc)); 128 } 129 130 public void setIncrement(String incr) 131 { 132 setProperty(INCREMENT, incr); 133 } 134 135 public int getIncrement() 136 { 137 return getPropertyAsInt(INCREMENT); 138 } 139 140 public void setIsPerUser(boolean isPer) 141 { 142 setProperty(new BooleanProperty(PER_USER, isPer)); 143 } 144 145 public boolean isPerUser() 146 { 147 return getPropertyAsBoolean(PER_USER); 148 } 149 150 public void setVarName(String name) 151 { 152 setProperty(VAR_NAME, name); 153 } 154 155 public String getVarName() 156 { 157 return getPropertyAsString(VAR_NAME); 158 } 159 } 160 | Popular Tags |