Java Code Examples for org.apache.jmeter.threads.JMeterVariables#get()

The following examples show how to use org.apache.jmeter.threads.JMeterVariables#get() . 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: 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 2
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 3
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 4
Source File: AbstractUpdater.java    From jmeter-prometheus-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract the label values from the Sample Event. Values
 * depend on how the Updater was configured. 
 * 
 * @param event
 * @return the label values.
 */
protected String[] labelValues(SampleEvent event) {
	String[] labels = config.getLabels();
	String[] values =  new String[labels.length];
	JMeterVariables vars = JMeterContextService.getContext().getVariables();
	
	for(int i = 0; i < labels.length; i++) {
		String name = labels[i];
		String value = null;
		
		// reserved keyword for the sampler's label (the name)
		if(name.equalsIgnoreCase("label")) { 
			value = event.getResult().getSampleLabel();
	
		} else if(name.equalsIgnoreCase("code")) {	// code also reserved
			value = event.getResult().getResponseCode();
			
		// try to find it as a plain'ol variable.
		} else if (this.varIndexLookup.get(name) != null){
			int idx = this.varIndexLookup.get(name);
			value = event.getVarValue(idx);
		
		// lastly look in sample_variables
		}else if (vars != null){
			value = vars.get(name);
		}
		
		values[i] = (value == null || value.isEmpty()) ? NULL : value;
	}
	
	return values;
}
 
Example 5
Source File: AbstractUpdater.java    From jmeter-prometheus-plugin with Apache License 2.0 5 votes vote down vote up
protected String[] labelValues(AssertionContext ctx) {
	String[] labels = config.getLabels();
	String[] values =  new String[labels.length];
	JMeterVariables vars = JMeterContextService.getContext().getVariables();
	
	for(int i = 0; i < labels.length; i++) {
		String name = labels[i];
		String value = null;
		
		if(name.equalsIgnoreCase("label")) {
			value = ctx.assertion.getName();
			
		// try to find it as a plain'ol variable.
		} else if (this.varIndexLookup.get(name) != null){
			int idx = this.varIndexLookup.get(name);
			value = ctx.event.getVarValue(idx);
			
		
		// lastly look in sample_variables
		}else if (vars != null){
			value = vars.get(name);
		}
					
		values[i] = (value == null || value.isEmpty()) ? NULL : value;
	}
	
	return values;
}
 
Example 6
Source File: BaseBotSampler.java    From BotServiceStressToolkit with MIT License 4 votes vote down vote up
protected Activity send(Activity activity) throws UnirestException, HttpResponseException, AuthenticationException {
	log.info(String.format(
			"Sending activity of type %s with activityId=%s, conversationId=%s with callbackUrl as %s",
			activity.getType(), activity.getId(), activity.getConversation().getId(), activity.getServiceUrl()));

	Jsonb jsonb = JsonbBuilder.create();
	String jsonPayload = jsonb.toJson(activity);

	log.debug("Sending payload: " + jsonPayload);

	RequestBodyEntity request = Unirest.post(getBotUrl()).body(jsonPayload);
	Map<String, List<String>> headers = request.getHttpRequest().getHeaders();
	headers.put(CONTENT_TYPE_HEADER_KEY, Collections.singletonList(CONTENT_TYPE_APPLICATION_JSON));

	if (hasAuthProperties()) {
		JMeterVariables vars = getThreadContext().getVariables();
		log.debug("Has security data");

		if (vars.get(TOKEN) == null) {
			log.debug("Token is null, authenticating");
			TokenResponse tokenResponse = AuthHelper.getToken(getPropertyAsString(BotServiceSecurityConfig.APP_ID),
					getPropertyAsString(BotServiceSecurityConfig.CLIENT_SECRET));

			vars.put(TOKEN, tokenResponse.getAccessToken());

		} else {
			log.debug("Token is not null, will reuse it");
		}

		String token = vars.get(TOKEN);
		headers.put(AUTHORIZATION_HEADER, Collections.singletonList("Bearer " + token));

	} else {
		log.debug("No security properties available. Will proceed without authentication.");
	}

	HttpResponse<InputStream> postResponse = request.asBinary();
	ensureResponseSuccess(postResponse);

	return activity;
}