Skip to content

Commit

Permalink
Split out mangled method name parts.
Browse files Browse the repository at this point in the history
Split out the parts of a mangled method name on construction so they
can be provided as parameters for generating the sampler name.
  • Loading branch information
jmibanez committed Sep 15, 2017
1 parent 07147a6 commit 0aa6b3c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
36 changes: 25 additions & 11 deletions src/com/jmibanez/tools/jmeter/MethodCallRecord.java
Expand Up @@ -24,6 +24,8 @@ public class MethodCallRecord
private Class<?>[] argTypes;
private Object[] args;
private byte[] argsPacked;
private String mangledMethodName;
private String mangledArgs;
private Object returnValue;
private Throwable returnException;
private boolean isException = false;
Expand All @@ -39,7 +41,10 @@ public MethodCallRecord(final int index, final String target, final Method m,
this.index = index;
this.target = target;
this.argTypes = m.getParameterTypes();
this.method = constructMethodName(m.getName(), this.argTypes);
this.method = m.getName();
String[] builtNames = constructMethodName(m.getName(), this.argTypes);
this.mangledMethodName = builtNames[0];
this.mangledArgs = builtNames[1];
this.args = args;
this.argsPacked = packArgs(this.args);
}
Expand All @@ -56,6 +61,14 @@ public String getMethod() {
return method;
}

public String getMangledMethodName() {
return mangledMethodName;
}

public String getMangledArguments() {
return mangledArgs;
}

public Object[] recreateArguments() {
this.args = unpackArgs(this.argsPacked);
return args;
Expand Down Expand Up @@ -111,24 +124,25 @@ public Map<String, String> getRemotePathsInReturn() {
return remotePathsInReturn;
}

public static String constructMethodName(String methodName, Class<?>[] argTypes) {
StringBuilder sb = new StringBuilder();
sb.append(methodName);
public static String[] constructMethodName(String methodName, Class<?>[] argTypes) {
StringBuilder full = new StringBuilder();
StringBuilder args = new StringBuilder();

if(argTypes == null || argTypes.length == 0) {
sb.append(":");
return sb.toString();
return new String[] { methodName + ":", "" };
}

sb.append(":");
full.append(methodName);
full.append(":");
for(Class<?> c : argTypes) {
sb.append(c.getName());
sb.append(",");
args.append(c.getName());
args.append(",");
}

sb.deleteCharAt(sb.length() - 1);
args.deleteCharAt(args.length() - 1);

return sb.toString();
full.append(args);
return new String[] { full.toString(), args.toString() };
}

private void writeObject(ObjectOutputStream out)
Expand Down
4 changes: 3 additions & 1 deletion src/com/jmibanez/tools/jmeter/RMISampleResult.java
Expand Up @@ -142,7 +142,9 @@ private void writeObject(ObjectOutputStream out)
if(method != null) {
out.writeUTF(method.getClass().getCanonicalName());
out.writeUTF("\nM:");
out.writeUTF(MethodCallRecord.constructMethodName(method.getName(), method.getParameterTypes()));
String[] builtNames = MethodCallRecord.constructMethodName(method.getName(),
method.getParameterTypes());
out.writeUTF(builtNames[0]);
}
else {
out.writeUTF(classOwnerName);
Expand Down
Expand Up @@ -302,10 +302,10 @@ private void init() {
JLabel portLabel = new JLabel("Proxy Port");
portLabel.setLabelFor(proxyPort);

samplerNameFormat = new JTextField("[%1$s] %2$d - %3$s", 100);
samplerNameFormat = new JTextField("[%1$s] %2$d - %3$s:%4$s", 100);
samplerNameFormat.setName(SAMPLERNAMEFORMAT_FIELD);

JLabel samplerNameFormatLabel = new JLabel("Sampler Name Format");
JLabel samplerNameFormatLabel = new JLabel("Sampler Name Format [1: Target; 2: Call Index; 3: Method; 4: Args]");
samplerNameFormatLabel.setLabelFor(samplerNameFormat);

JPanel configBox = new HorizontalPanel();
Expand Down
Expand Up @@ -36,7 +36,7 @@
public class RmiSamplerGeneratorMethodRecorder
implements MethodRecorder {

public static final String DEFAULT_SAMPLER_NAME_FORMAT = "[%1$s] %2$d - %3$s";
public static final String DEFAULT_SAMPLER_NAME_FORMAT = "[%1$s] %2$d - %3$s:%4$s";

private static Log log = LogFactory.getLog(RmiSamplerGeneratorMethodRecorder.class);

Expand Down Expand Up @@ -161,28 +161,31 @@ public void recordCall(final MethodCallRecord r)
BeanShellPostProcessor retValProc = null;
sampler.setProperty(TestElement.TEST_CLASS, RMISampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, RMISamplerGUI.class.getName());
sampler.setTargetName(r.getTarget());
sampler.setMethodName(r.getMangledMethodName());

String instanceName = r.getTarget();
sampler.setTargetName(instanceName);
if (instanceName == null) {
instanceName = "";
}

int index = r.getIndex();
String method = r.getMethod();
sampler.setMethodName(method);
String mangledArgs = r.getMangledArguments();

String samplerName = "";

try {
samplerName = String.format(samplerNameFormat,
instanceName, index,
method);
method, mangledArgs);
}
catch(Exception e) {
// use default format
log.warn("Invalid sampler name format", e);
samplerName = String.format(DEFAULT_SAMPLER_NAME_FORMAT,
instanceName, index,
method);
method, mangledArgs);
}
sampler.setName(samplerName);

Expand Down
28 changes: 28 additions & 0 deletions test/com/jmibanez/tools/jmeter/MethodCallRecordTest.java
@@ -0,0 +1,28 @@
package com.jmibanez.tools.jmeter;

import java.util.List;
import java.util.Map;
import junit.framework.TestCase;

public class MethodCallRecordTest extends TestCase {

public void testConstructSimpleMethodName() {
String[] builtNames = MethodCallRecord.constructMethodName("foo", null);
assertEquals("foo:", builtNames[0]);
assertEquals("", builtNames[1]);
}

public void testConstructMethodNameWithOneArg() {
String[] builtNames = MethodCallRecord
.constructMethodName("foo", new Class<?>[] { String.class });
assertEquals("foo:java.lang.String", builtNames[0]);
assertEquals("java.lang.String", builtNames[1]);
}

public void testConstructMethodNameWithSeveralArgs() {
String[] builtNames = MethodCallRecord
.constructMethodName("foo", new Class<?>[] { String.class, Class.class });
assertEquals("foo:java.lang.String,java.lang.Class", builtNames[0]);
assertEquals("java.lang.String,java.lang.Class", builtNames[1]);
}
}

0 comments on commit 0aa6b3c

Please sign in to comment.