Skip to content

Commit

Permalink
feat: add timestamp to AMQP publisher (fixes jlavallee#37) (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliesbelik authored Sep 21, 2023
1 parent 55c54ed commit 18bb9b5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* App_id message property for AMQP Publisher (see [jlavallee#37](https://github.com/jlavallee/JMeter-Rabbit-AMQP/issues/37)).
* Timestamp message property for AMQP Publisher (see [jlavallee#37](https://github.com/jlavallee/JMeter-Rabbit-AMQP/issues/37)).

### Dependency Updates

Expand Down
8 changes: 4 additions & 4 deletions docs/examples/rabbitmq-amqp-test.jmx
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.2">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="AMQP Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">100</stringProp>
<boolProp name="LoopController.continue_forever">false</boolProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">10</stringProp>
<stringProp name="ThreadGroup.ramp_time">10</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
<boolProp name="ThreadGroup.delayedStart">false</boolProp>
</ThreadGroup>
<hashTree>
<RandomVariableConfig guiclass="TestBeanGUI" testclass="RandomVariableConfig" testname="Reply-To Queue Name" enabled="true">
Expand Down Expand Up @@ -82,6 +81,7 @@
<stringProp name="AMQPPublisher.ContentEncoding">utf-8</stringProp>
<stringProp name="AMQPPublisher.MessageId"></stringProp>
<stringProp name="AMQPPublisher.AppId"></stringProp>
<boolProp name="AMQPPublisher.Timestamp">true</boolProp>
<elementProp name="AMQPPublisher.Headers" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="cc" elementType="Argument">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,13 @@ private String formatHeaders(Delivery delivery) {
Map<String, Object> headers = delivery.getProperties().getHeaders();
StringBuilder sb = new StringBuilder();

sb.append(TIMESTAMP_PARAMETER)
.append(": ")
.append(delivery.getProperties().getTimestamp() != null ? delivery.getProperties().getTimestamp().getTime() : "")
.append("\n");
if (delivery.getProperties().getTimestamp() != null) {
sb.append(TIMESTAMP_PARAMETER)
.append(": ")
.append((delivery.getProperties().getTimestamp().getTime())/1000)
.append("\n");
}

sb.append(EXCHANGE_PARAMETER)
.append(": ")
.append(delivery.getEnvelope().getExchange())
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -51,9 +53,11 @@ public class AMQPPublisher extends AMQPSampler implements Interruptible {
private static final String PERSISTENT = "AMQPPublisher.Persistent";
private static final String USE_TX = "AMQPPublisher.UseTx";
private static final String APP_ID = "AMQPPublisher.AppId";
private static final String TIMESTAMP = "AMQPPublisher.Timestamp";

public static final boolean DEFAULT_PERSISTENT = false;
public static final boolean DEFAULT_USE_TX = false;
public static final boolean DEFAULT_TIMESTAMP = true;
public static final int DEFAULT_MESSAGE_PRIORITY = 0;
public static final String DEFAULT_RESPONSE_CODE = "500";
public static final String DEFAULT_CONTENT_TYPE = "text/plain";
Expand Down Expand Up @@ -273,6 +277,14 @@ public void setAppId(String appId) {
setProperty(APP_ID, appId);
}

public boolean getTimestamp() {
return getPropertyAsBoolean(TIMESTAMP, DEFAULT_TIMESTAMP);
}

public void setTimestamp(Boolean ts) {
setProperty(TIMESTAMP, ts);
}

@Override
public boolean interrupt() {
cleanup();
Expand Down Expand Up @@ -316,6 +328,10 @@ protected AMQP.BasicProperties getProperties() {
builder.appId(getAppId());
}

if (getTimestamp()) {
builder.timestamp(Date.from(Instant.now()));
}

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
Expand All @@ -24,6 +26,7 @@ public class AMQPPublisherGui extends AMQPSamplerGui {
private static final long serialVersionUID = 1L;

private final JLabeledTextArea message = new JLabeledTextArea("Message Content");

private final JLabeledTextField messageRoutingKey = new JLabeledTextField(" Routing Key");
private final JLabeledTextField messageType = new JLabeledTextField(" Message Type");
private final JLabeledTextField replyToQueue = new JLabeledTextField(" Reply-To Queue");
Expand All @@ -34,6 +37,7 @@ public class AMQPPublisherGui extends AMQPSamplerGui {
private final JLabeledTextField contentEncoding = new JLabeledTextField("Content Encoding");
private final JLabeledTextField appId = new JLabeledTextField(" Application ID");

private final JCheckBox timestamp = new JCheckBox("Timestamp", AMQPPublisher.DEFAULT_TIMESTAMP);
private final JCheckBox persistent = new JCheckBox("Persistent", AMQPPublisher.DEFAULT_PERSISTENT);
private final JCheckBox useTx = new JCheckBox("Use Transactions", AMQPPublisher.DEFAULT_USE_TX);

Expand Down Expand Up @@ -85,6 +89,7 @@ public void configure(TestElement element) {
messageId.setText(sampler.getMessageId());
message.setText(sampler.getMessage());
appId.setText(sampler.getAppId());
timestamp.setSelected(sampler.getTimestamp());

configureHeaders(sampler);
}
Expand Down Expand Up @@ -124,6 +129,7 @@ public void modifyTestElement(TestElement te) {
sampler.setContentEncoding(contentEncoding.getText());
sampler.setMessageId(messageId.getText());
sampler.setAppId(appId.getText());
sampler.setTimestamp(timestamp.isSelected());

sampler.setHeaders((Arguments) headers.createTestElement());
}
Expand Down Expand Up @@ -180,6 +186,10 @@ private JPanel initMessagePropertyPanel() {
JPanel propertyPanel = new JPanel(new GridBagLayout());
propertyPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), PROPS_SETTINGS_LABEL));

timestamp.setHorizontalTextPosition(SwingConstants.LEFT);
//timestamp.setHorizontalTextPosition(SwingConstants.LEADING);
timestamp.setIconTextGap(35);

propertyPanel.add(messageRoutingKey, constraints);
propertyPanel.add(replyToQueue, constraints);
propertyPanel.add(messageType, constraints);
Expand All @@ -189,6 +199,7 @@ private JPanel initMessagePropertyPanel() {
propertyPanel.add(appId, constraints);
propertyPanel.add(contentType, constraints);
propertyPanel.add(contentEncoding, constraints);
propertyPanel.add(timestamp, constraints);

return propertyPanel;
}
Expand All @@ -199,6 +210,7 @@ private JPanel initMessagePropertyPanel() {
@Override
public void clearGui() {
super.clearGui();

persistent.setSelected(AMQPPublisher.DEFAULT_PERSISTENT);
useTx.setSelected(AMQPPublisher.DEFAULT_USE_TX);
messageRoutingKey.setText("");
Expand All @@ -211,6 +223,8 @@ public void clearGui() {
messageId.setText("");
message.setText("");
appId.setText("");
timestamp.setSelected(AMQPPublisher.DEFAULT_TIMESTAMP);

headers.clearGui();
}

Expand Down

0 comments on commit 18bb9b5

Please sign in to comment.