Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function returning the name of the current sampler #2401

Closed
asfimport opened this issue Sep 22, 2010 · 4 comments
Closed

New function returning the name of the current sampler #2401

asfimport opened this issue Sep 22, 2010 · 4 comments

Comments

@asfimport
Copy link
Collaborator

jens_0 (Bug 49975):
For tagging of load test requests in a monitoring tool I needed the possiility to add the name of the current sampler to the HTTP header. I implemented the following new function that can be used in the test's HTTP Header Manager.

Created attachment samplername.java: function sampler name

samplername.java
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.jmeter.functions;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

/**
 * Function to return the name of the current sampler.
 */
public class SamplerName extends AbstractFunction {

	private static final String KEY = "__samplerName"; //$NON-NLS-1$

	private static final List<String> desc = new LinkedList<String>();

	/** {@inheritDoc} */
	@Override
	public String execute(SampleResult previousResult, Sampler currentSampler)
			throws InvalidVariableException {
		// return JMeterContextService.getContext().getCurrentSampler().getName();
		return currentSampler.getName();
	}

	/** {@inheritDoc} */
	@Override
	public void setParameters(Collection<CompoundVariable> parameters)
			throws InvalidVariableException {
		checkParameterCount(parameters, 0, 0);
	}

	/** {@inheritDoc} */
	@Override
	public String getReferenceKey() {
		return KEY;
	}

	/** {@inheritDoc} */
	public List<String> getArgumentDesc() {
		return desc;
	}
}

OS: Windows XP

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Thanks.

I added an optional variable name.

Also, the code needs to check for null Sammpler object (which can happen e.g. if the function is used on the Test Plan - not that it makes sense there).

URL: http://svn.apache.org/viewvc?rev=1000632&view=rev
Log:
#2401 - New function returning the name of the current sampler

Added:
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/SamplerName.java (with props)
Modified:
jakarta/jmeter/trunk/xdocs/changes.xml
jakarta/jmeter/trunk/xdocs/usermanual/functions.xml

@asfimport
Copy link
Collaborator Author

Ralf Hauser (migrated from Bugzilla):
when will this be deployed?

http://jakarta.apache.org/jmeter/usermanual/functions.html

still doesn't show

<tr><td>Information</td><td> <a href="#__samplerName">samplerName</a></td><td>get the sampler name (label)</td></tr>

...

@asfimport
Copy link
Collaborator Author

Ralf Hauser (migrated from Bugzilla):
see also #2484

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
(In reply to comment 2)

when will this be deployed?

With the next JMeter release.

http://jakarta.apache.org/jmeter/usermanual/functions.html

still doesn't show

<tr><td>Information</td><td> <a
href="#__samplerName">samplerName</a></td><td>get the sampler name
(label)</td></tr>

...

Because that documentation is for the current release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant