Java Code Examples for org.apache.jmeter.threads.JMeterContext#getVariables()

The following examples show how to use org.apache.jmeter.threads.JMeterContext#getVariables() . 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: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example 2
Source File: RedisDataSetTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 8
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 9
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testReported1_1() {
    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[*].id");
    instance.setDefaultValue("NOTFOUND");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertNotEquals("NOTFOUND", vars.get("GroupID"));
    assertEquals("e02991f4-a95d-43dd-8eb0-fbc44349e238", vars.get("GroupID_1"));
}
 
Example 10
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 11
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 12
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@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 13
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
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 14
Source File: HttpTestBean.java    From BotServiceStressToolkit with MIT License 6 votes vote down vote up
@Override
public SampleResult sample(Entry entry) {
	SampleResult res = new SampleResult();
	res.setSampleLabel(getName());
	res.sampleStart();
	res.setDataType(SampleResult.TEXT);

	try {
		JMeterContext context = getThreadContext();
		JMeterVariables vars = context.getVariables();

		String body = Unirest.get("http://www.uol.com.br").asString().getBody();

		res.setResponseData(body, null);
		res.sampleEnd();
		res.setSuccessful(true);

	} catch (UnirestException e) {
		res.setResponseData(e.toString(), null);
		res.sampleEnd();
		res.setSuccessful(false);
	} 

	return res;
}
 
Example 15
Source File: ConversationUpdateSampler.java    From BotServiceStressToolkit with MIT License 6 votes vote down vote up
private String ensureFromUser() {
	JMeterContext context = getThreadContext();
	JMeterVariables vars = context.getVariables();

	String fromUserId = "";
	if (getGenRandomUserIdPerThread()) {
		if (vars.get(Constants.USER) == null) {
			fromUserId = "user-" + RandomStringUtils.randomAlphabetic(3, 7);
			vars.put(Constants.USER, fromUserId);
		} else {
			fromUserId = vars.get(Constants.USER);
		}
	} else {
		fromUserId = getFromMemberId();
	}

	return fromUserId;
}
 
Example 16
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_list() {
    System.out.println("process list");
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData("{\"myval\": [{\"test\":1},{\"test\":{\"dict\":1}},{\"test\":null}]}".getBytes());
    context.setPreviousResult(res);

    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.setJsonPath("$.myval[*].test");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertEquals("[1,{\"dict\":1},null]", vars.get("test"));
    assertEquals("1", vars.get("test_1"));
    assertEquals("{\"dict\":1}", vars.get("test_2"));
    assertEquals("null", vars.get("test_3"));

    // test for cleaning prev vars
    res.setResponseData("{\"myval\": [{\"test\":1},{\"test\":2}]}".getBytes());
    instance.process();
    assertEquals("[1,2]", vars.get("test"));
    assertEquals("1", vars.get("test_1"));
    assertEquals("2", vars.get("test_2"));
    assertEquals(null, vars.get("test_3"));
}
 
Example 17
Source File: BaseBotSampler.java    From BotServiceStressToolkit with MIT License 5 votes vote down vote up
protected String getFromUser() {
	JMeterContext context = getThreadContext();
	JMeterVariables vars = context.getVariables();

	if (getGenRandomUserIdPerThread()) {
		return vars.get(Constants.USER);
	} else {
		return getFromMemberId();
	}
}
 
Example 18
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_default() {
    System.out.println("process def");
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    context.setPreviousResult(res);

    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertEquals("DEFAULT", vars.get("test"));
}
 
Example 19
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
private void injectVariables(JMeterThread jmThread, JMeterContext threadContext) {
    if (threadContext != null && threadContext.getVariables() != null) {
        try {
            Class<JMeterThread> cls = JMeterThread.class;
            Field vars = cls.getDeclaredField("threadVars");
            vars.setAccessible(true);
            vars.set(jmThread, threadContext.getVariables());
        } catch (Throwable ex) {
            log.warn("Cannot inject variables into parallel thread ", ex);
        }
    }
}
 
Example 20
Source File: RedisDataSet.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void iterationStart(LoopIterationEvent event) {
    Jedis connection = null;
    try {
        connection = pool.getResource();

        // Get data from list's head
        String line = getDataFromConnection(connection, redisKey);

        if(line == null) { // i.e. no more data (nil)
            throw new JMeterStopThreadException("End of redis data detected");
        }

        if (getRecycleDataOnUse()) {
            addDataToConnection(connection, redisKey, line);
        }

        final String names = variableNames;
        if (vars == null) {
            vars = JOrphanUtils.split(names, ","); 
        }
        
        final JMeterContext context = getThreadContext();
        JMeterVariables threadVars = context.getVariables();
        String[] values = JOrphanUtils.split(line, delimiter, false);
        for (int a = 0; a < vars.length && a < values.length; a++) {
            threadVars.put(vars[a], values[a]);
        }
        
    } finally {
        pool.returnResource(connection);
    }
}