org.apache.jmeter.threads.JMeterVariables Java Examples
The following examples show how to use
org.apache.jmeter.threads.JMeterVariables.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StrReplaceRegex.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String totalString = getParameter(0).replaceAll(getParameter(1), getParameter(2)); JMeterVariables vars = getVariables(); if (values.length > 3) { String varName = getParameter(3); if (vars != null && varName != null && varName.length() > 0) {// vars will be null on TestPlan vars.put(varName, totalString); } } return totalString; }
Example #2
Source File: RedisDataSetTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Before public void setUp() throws IOException { // Set up thread variables JMeterContext jmcx = JMeterContextService.getContext(); jmcx.setVariables(new JMeterVariables()); threadVars = jmcx.getVariables(); // Create new mock RedisServer server = RedisServer.newRedisServer(6370); server.start(); // Setup test data JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", 6370); Jedis jedis = pool.getResource(); jedis.rpush("testRecycleList", "12345678,1234","12345679,1235", "12345680,1236"); jedis.rpush("testConsumeList", "12345678,1234","12345679,1235", "12345680,1236"); jedis.sadd("testRecycleSet", "12345678,1234","12345679,1235", "12345680,1236"); jedis.sadd("testConsumeSet", "12345678,1234","12345679,1235", "12345680,1236"); jedis.shutdown(); }
Example #3
Source File: ParameterizedControllerTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testNext() throws InvalidVariableException { System.out.println("next"); Arguments args = new Arguments(); args.addArgument("var2", "${var1}"); args.addArgument("var3", "${var2}"); instance.setUserDefinedVariables(args); ValueReplacer replacer = new ValueReplacer(); replacer.replaceValues(instance); args.setRunningVersion(true); instance.next(); JMeterVariables vars = JMeterContextService.getContext().getVariables(); assertEquals("${var2}", vars.get("var3")); assertEquals("val1", vars.get("var2")); instance.next(); assertEquals("val1", vars.get("var3")); }
Example #4
Source File: If.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String actual = getParameter(0); String expected = getParameter(1); String result = null; if (actual.equals(expected)) { result = getParameter(2).toString(); } else { result = getParameter(3).toString(); } JMeterVariables vars = getVariables(); if (vars != null && values.length > 4) { String varName = getParameter(4).trim(); vars.put(varName, result); } return result; }
Example #5
Source File: RandomCSVDataSetConfig.java From jmeter-bzm-plugins with Apache License 2.0 | 6 votes |
private void readRandom() { final RandomCSVReader reader = getReader(); long lineAddr; synchronized (reader) { if (reader.hasNextRecord()) { lineAddr = reader.getNextLineAddr(); } else { // TODO: interrupt iteration if (randomCSVReader != null) { randomCSVReader.close(); } randomCSVReader = null; throw new JMeterStopThreadException("All records in the CSV file have been passed."); } } JMeterVariables variables = JMeterContextService.getContext().getVariables(); putVariables(variables, getDestinationVariableKeys(), reader.readLineWithSeek(lineAddr)); }
Example #6
Source File: ParallelSampler.java From jmeter-bzm-plugins with Apache License 2.0 | 6 votes |
private void changeVariablesMap() { try { JMeterContext context = this.getThreadContext(); if (context != null && context.getVariables() != null) { JMeterVariables jMeterVariables = context.getVariables(); Class<JMeterVariables> cls = JMeterVariables.class; Field variablesField = cls.getDeclaredField("variables"); variablesField.setAccessible(true); Object obj = variablesField.get(jMeterVariables); synchronized (obj) { if (obj instanceof Map) { Map variables = (Map) obj; if (!(variables instanceof ConcurrentHashMap)) { variablesField.set(jMeterVariables, new ConcurrentHashMap(variables)); } } else { log.warn("Unexpected variables map type " + obj.getClass().getName()); } } } } catch (Throwable ex) { log.warn("Cannot change variables map ", ex); } }
Example #7
Source File: SetVariablesAction.java From jmeter-plugins with Apache License 2.0 | 6 votes |
private void processVariables() { final Arguments args1 = (Arguments) this.getUserDefinedVariablesAsProperty().getObjectValue(); Arguments args = (Arguments) args1.clone(); final JMeterVariables vars = JMeterContextService.getContext().getVariables(); Iterator<Map.Entry<String, String>> it = args.getArgumentsAsMap().entrySet().iterator(); Map.Entry<String, String> var; while (it.hasNext()) { var = it.next(); if (log.isDebugEnabled()) { log.debug("Setting " + var.getKey() + "=" + var.getValue()); } vars.put(var.getKey(), var.getValue()); } }
Example #8
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testYamlExtractor3() throws IOException { File test = new File(getClass().getResource("/test.yml").getPath()); String response = FileUtils.readFileToString(test); JMeterContext context = JMeterContextService.getContext(); JMeterVariables vars = context.getVariables(); SampleResult samplerResult = new SampleResult(); samplerResult.setResponseData(response.getBytes()); context.setPreviousResult(samplerResult); JSONPathExtractor instance = new JSONPathExtractor(); instance.setInputFormat(JSONPathExtractor.INPUT_YAML); instance.setDefaultValue("DEFAULT"); instance.setVar("developers"); instance.setJsonPath("$.developers"); instance.process(); assertEquals("[\"rultor\",\"salikjan\",\"sherif\"]", vars.get("developers")); assertEquals("3", vars.get("developers_matchNr")); }
Example #9
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testYamlExtractor2() throws IOException { File test = new File(getClass().getResource("/test.yml").getPath()); String response = FileUtils.readFileToString(test); JMeterContext context = JMeterContextService.getContext(); JMeterVariables vars = context.getVariables(); SampleResult samplerResult = new SampleResult(); samplerResult.setResponseData(response.getBytes()); context.setPreviousResult(samplerResult); JSONPathExtractor instance = new JSONPathExtractor(); instance.setInputFormat(JSONPathExtractor.INPUT_YAML); instance.setDefaultValue("DEFAULT"); instance.setVar("architect"); instance.setJsonPath("$.architect"); instance.process(); assertEquals("mihai", vars.get("architect")); }
Example #10
Source File: PlainTextConfigElement.java From pepper-box with Apache License 2.0 | 6 votes |
/** * For every JMeter sample, iterationStart method gets invoked, it initializes load generator and for each iteration sets new message as JMeter variable * * @param loopIterationEvent */ @Override public void iterationStart(LoopIterationEvent loopIterationEvent) { //Check if load generator is instantiated if (generator == null) { try { //instantiate plaintext load generator generator = new PlaintTextLoadGenerator(getJsonSchema()); } catch (Exception e) { log.error("Failed to create PlaintTextLoadGenerator instance", e); } } //For ever iteration put message in jmeter variables JMeterVariables variables = JMeterContextService.getContext().getVariables(); variables.putObject(placeHolder, generator.nextMessage()); }
Example #11
Source File: PepperBoxSamplerTest.java From pepper-box with Apache License 2.0 | 6 votes |
@Before public void setup() throws IOException { zkServer = new EmbeddedZookeeper(); String zkConnect = ZKHOST + ":" + zkServer.port(); zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$); ZkUtils zkUtils = ZkUtils.apply(zkClient, false); Properties brokerProps = new Properties(); brokerProps.setProperty("zookeeper.connect", zkConnect); brokerProps.setProperty("broker.id", "0"); brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString()); brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT); KafkaConfig config = new KafkaConfig(brokerProps); Time mock = new MockTime(); kafkaServer = TestUtils.createServer(config, mock); //AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$); JMeterContext jmcx = JMeterContextService.getContext(); jmcx.setVariables(new JMeterVariables()); }
Example #12
Source File: PepperBoxLoadGenTest.java From pepper-box with Apache License 2.0 | 6 votes |
@Before public void setup() throws IOException { zkServer = new EmbeddedZookeeper(); String zkConnect = ZKHOST + ":" + zkServer.port(); zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$); ZkUtils zkUtils = ZkUtils.apply(zkClient, false); Properties brokerProps = new Properties(); brokerProps.setProperty("zookeeper.connect", zkConnect); brokerProps.setProperty("broker.id", "0"); brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString()); brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT); KafkaConfig config = new KafkaConfig(brokerProps); Time mock = new MockTime(); kafkaServer = TestUtils.createServer(config, mock); //AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$); JMeterContext jmcx = JMeterContextService.getContext(); jmcx.setVariables(new JMeterVariables()); }
Example #13
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testYamlExtractor1() throws IOException { File test = new File(getClass().getResource("/test1.yml").getPath()); String response = FileUtils.readFileToString(test); JMeterContext context = JMeterContextService.getContext(); JMeterVariables vars = context.getVariables(); SampleResult samplerResult = new SampleResult(); samplerResult.setResponseData(response.getBytes()); context.setPreviousResult(samplerResult); JSONPathExtractor instance = new JSONPathExtractor(); instance.setInputFormat(JSONPathExtractor.INPUT_YAML); instance.setDefaultValue("DEFAULT"); instance.setVar("concurrency"); instance.setJsonPath("$.execution[*].concurrency"); instance.process(); assertEquals("[100]", vars.get("concurrency")); instance.setJsonPath("execution[*].concurrency1"); instance.process(); assertEquals("DEFAULT", vars.get("concurrency")); }
Example #14
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testProcess_from_var_2() { System.out.println("process fromvar"); JMeterContext context = JMeterContextService.getContext(); JMeterVariables vars = context.getVariables(); SampleResult res = new SampleResult(); res.setResponseData("".getBytes()); context.setPreviousResult(res); vars.put("SVAR", json); JSONPathExtractor instance = new JSONPathExtractor(); instance.setDefaultValue("DEFAULT"); instance.setVar("test"); instance.setJsonPath("$.store.bicycle"); instance.setSubject(JSONPathExtractor.SUBJECT_VARIABLE); instance.setSrcVariableName("SVAR"); instance.process(); String test = vars.get("test"); boolean thiis = "{\"color\":\"red\",\"price\":19.95}".equals(test); boolean thaat = "{\"price\":19.95,\"color\":\"red\"}".equals(test); assertTrue(thiis || thaat); }
Example #15
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Ignore @Test // FIXME: we need to solve this one day public void testReported2() { System.out.println("process reported"); JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); res.setResponseData(json3.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setVar("var"); instance.setJsonPath("$.data[?(@.attr.value>0)][0].attr"); instance.setDefaultValue("NOTFOUND"); instance.process(); JMeterVariables vars = context.getVariables(); assertNotEquals("NOTFOUND", vars.get("var")); assertEquals("{value=1}", vars.get("var")); }
Example #16
Source File: SerializedConfigElement.java From pepper-box with Apache License 2.0 | 6 votes |
/** * For every JMeter sample, iterationStart method gets invoked, it initializes load generator and for each iteration sets new message as JMeter variable * * @param loopIterationEvent */ @Override public void iterationStart(LoopIterationEvent loopIterationEvent) { try { //Check if load generator is instantiated if (generator == null) { //instantiate serialized load generator generator = new SerializedLoadGenerator(className, objProperties); } //For ever iteration put message in jmeter variables JMeterVariables variables = JMeterContextService.getContext().getVariables(); variables.putObject(placeHolder, generator.nextMessage()); } catch (Exception e) { log.error("Failed to create PlaintTextLoadGenerator instance", e); } }
Example #17
Source File: AbstractUpdaterTest.java From jmeter-prometheus-plugin with Apache License 2.0 | 6 votes |
@Test public void testKeywords() { BaseCollectorConfig base = TestUtilities.simpleCounterCfg(); base.setLabels(new String[] {"label","code"}); ListenerCollectorConfig cfg = new ListenerCollectorConfig(base); TestUpdater u = new TestUpdater(cfg); SampleResult res = new SampleResult(); res.setSampleLabel("test_label"); res.setResponseCode("204"); SampleEvent event = new SampleEvent(res,"test_tg", new JMeterVariables()); String[] labels = u.labelValues(event); Assert.assertTrue(labels.length == 2); Assert.assertArrayEquals(new String[] {"test_label", "204"}, labels); }
Example #18
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testReported1_3() { System.out.println("process reported"); JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); res.setResponseData(json2.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setVar("GroupID"); instance.setJsonPath("$.data.groups[?(@.name==Avtovaz)].id"); instance.setDefaultValue("NOTFOUND"); instance.process(); JMeterVariables vars = context.getVariables(); assertEquals("NOTFOUND", vars.get("GroupID")); }
Example #19
Source File: AbstractUpdaterTest.java From jmeter-prometheus-plugin with Apache License 2.0 | 6 votes |
@Test public void testNulls() { BaseCollectorConfig base = TestUtilities.simpleCounterCfg(); base.setLabels(new String[] {"be_null_one", "be_null_two", "code"}); ListenerCollectorConfig cfg = new ListenerCollectorConfig(base); TestUpdater u = new TestUpdater(cfg); SampleResult res = new SampleResult(); res.setResponseCode("304"); SampleEvent event = new SampleEvent(res ,"tg1", new JMeterVariables()); String[] labels = u.labelValues(event); Assert.assertTrue(labels.length == 3); Assert.assertArrayEquals(new String[] {"null", "null", "304"}, labels); }
Example #20
Source File: Env.java From jmeter-plugins with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String propertyName = values[0].execute(); String propertyDefault = propertyName; if (values.length > 2) { // We have a 3rd parameter propertyDefault = values[2].execute(); } String propertyValue = JMeterPluginsUtils.getEnvDefault(propertyName, propertyDefault); if (values.length > 1) { String variableName = values[1].execute(); if (variableName.length() > 0) {// Allow for empty name final JMeterVariables variables = getVariables(); if (variables != null) { variables.put(variableName, propertyValue); } } } return propertyValue; }
Example #21
Source File: LoadGenerator.java From kafkameter with Apache License 2.0 | 6 votes |
/** * Helper for testing outside of JMeter */ public static void main(String[] args) { LoadGenerator generator = new LoadGenerator(); // Mock out JMeter environment JMeterVariables variables = new JMeterVariables(); JMeterContextService.getContext().setVariables(variables); generator.setFileName("config1.json"); generator.setVariableName("kafka_message"); generator.iterationStart(null); for (Map.Entry<String, Object> entry : variables.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } }
Example #22
Source File: FlexibleFileWriterTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testSampleOccurred_var() throws IOException { System.out.println("sampleOccurred-var"); SampleResult res = new SampleResult(); res.setResponseData("test".getBytes()); JMeterVariables vars = new JMeterVariables(); vars.put("TEST1", "TEST"); SampleEvent e = new SampleEvent(res, "Test", vars); FlexibleFileWriter instance = new FlexibleFileWriter(); instance.setFilename(File.createTempFile("ffw_test_", ".txt").getAbsolutePath()); System.out.println("prop: " + JMeterUtils.getProperty("sample_variables")); System.out.println("count: " + SampleEvent.getVarCount()); instance.setColumns("variable#0| |variable#| |variable#4t"); instance.testStarted(); for (int n = 0; n < 10; n++) { String exp = "TEST variable# variable#4t"; System.out.println(exp); instance.sampleOccurred(e); //ByteBuffer written = instance.fileEmul.getWrittenBytes(); //assertEquals(exp, JMeterPluginsUtils.byteBufferToString(written)); } instance.testEnded(); }
Example #23
Source File: FifoGet.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String fifoName = ((CompoundVariable) values[0]).execute(); Object valueObj = FifoMap.getInstance().get(fifoName); String value = null; if (valueObj != null) { value = valueObj.toString(); } JMeterVariables vars = getVariables(); if (vars != null && values.length > 1) { String varName = ((CompoundVariable) values[1]).execute().trim(); vars.put(varName, value); } return value; }
Example #24
Source File: FifoPop.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String fifoName = ((CompoundVariable) values[0]).execute(); String value = null; try { Object valueObj = FifoMap.getInstance().pop(fifoName, timeout); if (valueObj != null) { value = valueObj.toString(); } } catch (InterruptedException ex) { log.warn("Interrupted pop from queue " + fifoName); value = "INTERRUPTED"; } JMeterVariables vars = getVariables(); if (vars != null && values.length > 1) { String varName = ((CompoundVariable) values[1]).execute().trim(); vars.put(varName, value); } return value; }
Example #25
Source File: FifoPopPreProcessor.java From jmeter-plugins with Apache License 2.0 | 6 votes |
public void process() { String value = null; try { Object valueObj = FifoMap.getInstance().pop(getQueueName(), getTimeoutAsLong()); if (valueObj != null) { value = valueObj.toString(); } } catch (InterruptedException ex) { log.warn("Interrupted pop from queue " + getQueueName()); value = "INTERRUPTED"; } final JMeterVariables vars = JMeterContextService.getContext().getVariables(); if (vars != null) { vars.put(getVarName(), value); } }
Example #26
Source File: IsDefined.java From jmeter-plugins with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { JMeterVariables vars = getVariables(); String res = ((CompoundVariable) values[0]).execute().trim(); if (vars != null) { Object var = vars.getObject(res); if (var != null) { return "1"; } } return "0"; }
Example #27
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testProcess() { System.out.println("process"); JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); res.setResponseData(json.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setDefaultValue("DEFAULT"); instance.setVar("test"); instance.setJsonPath("$.store.book[*].author"); instance.process(); JMeterVariables vars = context.getVariables(); assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test")); }
Example #28
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testProcess_chinese() { JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); String chinese = "{\"carBrandName\":\"大众\"}"; res.setResponseData(chinese.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setDefaultValue("DEFAULT"); instance.setVar("test"); instance.setJsonPath("$.carBrandName"); instance.process(); JMeterVariables vars = context.getVariables(); // freaking "static final" DEFAULT_ENCODING field in SampleResult does not allow us to assert this // assertEquals("大众", vars.get("test")); }
Example #29
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testProcess_from_var() { System.out.println("process fromvar"); JMeterContext context = JMeterContextService.getContext(); JMeterVariables vars = context.getVariables(); SampleResult res = new SampleResult(); res.setResponseData("".getBytes()); context.setPreviousResult(res); vars.put("SVAR", json); JSONPathExtractor instance = new JSONPathExtractor(); instance.setDefaultValue("DEFAULT"); instance.setVar("test"); instance.setJsonPath("$.store.book[*].author"); instance.setSubject(JSONPathExtractor.SUBJECT_VARIABLE); instance.setSrcVariableName("SVAR"); instance.process(); assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test")); }
Example #30
Source File: JSONPathExtractorTest.java From jmeter-plugins with Apache License 2.0 | 6 votes |
@Test public void testReported1() { System.out.println("process reported"); JMeterContext context = JMeterContextService.getContext(); SampleResult res = new SampleResult(); res.setResponseData(json2.getBytes()); context.setPreviousResult(res); JSONPathExtractor instance = new JSONPathExtractor(); instance.setVar("GroupID"); instance.setJsonPath("$.data.groups[?(@.name=='Zaz')].id"); instance.setDefaultValue("NOTFOUND"); instance.process(); JMeterVariables vars = context.getVariables(); assertNotEquals("NOTFOUND", vars.get("GroupID")); assertEquals("378e9b20-99bb-4d1f-bf2c-6a4a6c69a8ed", vars.get("GroupID_1")); }