Created
January 9, 2017 15:28
-
-
Save pluto-atom-4/04da6a648dada63713d40f055cbd8701 to your computer and use it in GitHub Desktop.
study Jmeter TLS sampler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package internal.example.net.ssl; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.Charset; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
/** | |
* Created by pluto-atom-4 on 1/7/17. | |
*/ | |
public class ByteBufferOperation { | |
private static int HEADER_LENGTH = 4; | |
private static final String EPP_HELLO ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" | |
+"<epp xmlns=\"urn:ietf:params:xml:ns:epp-1.0\"><hello/></epp>"; | |
private static final Charset DEFAULT_CHARSET = UTF_8; | |
public static void main(String[] args) { | |
int messageLength = EPP_HELLO.getBytes(DEFAULT_CHARSET).length; | |
int frameLength = HEADER_LENGTH + messageLength; | |
ByteBuffer byteBuffer = ByteBuffer.allocate(frameLength); | |
byteBuffer.putInt(messageLength); | |
System.out.println(byteBuffer.toString()); | |
byteBuffer.put(EPP_HELLO.getBytes(DEFAULT_CHARSET)); | |
System.out.println(byteBuffer.toString()); | |
byte[] bytes = new byte[byteBuffer.capacity()]; | |
byteBuffer.rewind(); | |
byteBuffer.get(bytes); | |
System.out.println(byteToString(bytes)); | |
} | |
private static String byteToString(byte[] b) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < b.length; i++) { | |
sb.append("0x"); | |
sb.append(Integer.toHexString((int) b[i]).toUpperCase()); | |
sb.append(" "); | |
} | |
return sb.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.jmeter.protocol.tsl; | |
import org.apache.jmeter.samplers.AbstractSampler; | |
import org.apache.jmeter.samplers.Entry; | |
import org.apache.jmeter.samplers.SampleResult; | |
import org.apache.jmeter.testbeans.TestBean; | |
import org.apache.jorphan.logging.LoggingManager; | |
import org.apache.log.Logger; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by pluto-atom-4 on 1/7/17. | |
*/ | |
public class TLSSampler extends AbstractSampler implements TestBean { | |
private static final long serialVersionUID = 240L; | |
private static Logger log = LoggingManager.getLoggerForClass(); | |
private Boolean reuse; | |
private int port; | |
private String host; | |
private String username; | |
private String password; | |
private String body; | |
private static final String TCPKEY = "TCP"; //$NON-NLS-1$ key for HashMap | |
private static final String ERRKEY = "ERR"; //$NON-NLS-1$ key for HashMap | |
private String getError() { | |
Map<String, Object> cp = tp.get(); | |
return (String) cp.get(ERRKEY); | |
} | |
private String getCachedKey(String id) { | |
Map<String, Object> cp = tp.get(); | |
String cachedKey = null; | |
cachedKey = (String) cp.get(id); | |
if (cachedKey != null) { | |
log.info("found object for" + id); | |
} else { | |
log.info("creating object based for " + id ); | |
cachedKey = getSocketKey() + "#" + id; | |
cp.put(id, cachedKey); | |
} | |
return cachedKey; | |
} | |
/** | |
* @return String socket key in cache Map | |
*/ | |
private String getSocketKey() { | |
return TCPKEY+"#"+getHost()+"#"+getPort()+"#"+getUsername()+"#"+getPassword(); | |
} | |
/** | |
* the cache of TCP Connections | |
* KEY = TCPKEY or ERRKEY, Entry= Socket or String | |
*/ | |
private static final ThreadLocal<Map<String, Object>> tp = | |
new ThreadLocal<Map<String, Object>>() { | |
@Override | |
protected Map<String, Object> initialValue() { | |
return new HashMap<>(); | |
} | |
}; | |
public SampleResult sample(Entry entry) { | |
SampleResult result = new SampleResult(); | |
result.setSampleLabel(getName()); | |
result.setSamplerData(body); | |
String cachedKey = getCachedKey( getThreadContext().getThreadGroup().getName().replace(" ", "-") | |
+ "-" + String.valueOf(getThreadContext().getThreadNum())); | |
result.sampleStart(); | |
// Do something ... | |
result.setResponseMessage(cachedKey); | |
result.sampleEnd(); | |
result.setSuccessful(true); | |
return result; | |
} | |
// A TestBean is a Java Bean. Just define some properites and tye will | |
// automatically show up in the GUI. | |
// A String property: | |
public Boolean getReuse() { | |
return reuse; | |
} | |
public void setReuse(Boolean reuse) { | |
this.reuse = reuse; | |
} | |
public int getPort() { | |
return port; | |
} | |
public void setPort(int port) { | |
this.port = port; | |
} | |
public String getHost() { | |
return host; | |
} | |
public void setHost(String host) { | |
this.host = host; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public String getBody() { | |
return body; | |
} | |
public void setBody(String body) { | |
this.body = body; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment