Skip to content

Commit d4f50d1

Browse files
committed
Add --version option
Displays PerfTest version, git hash, build timestamp, and other information as well (AMQP Java Client in use, JVM version, OS, etc). [#153987842] Fixes #60
1 parent 56d121f commit d4f50d1

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
<maven.install.plugin.version>2.5.2</maven.install.plugin.version>
7575
<checksum.maven.plugin.version>1.6</checksum.maven.plugin.version>
7676
<maven.jar.plugin.version>3.0.2</maven.jar.plugin.version>
77+
<buildnumber.plugin.version>1.4</buildnumber.plugin.version>
78+
79+
<!-- because of https://issues.apache.org/jira/browse/MRESOURCES-99 -->
80+
<build.timestamp>${maven.build.timestamp}</build.timestamp>
81+
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
7782
</properties>
7883

7984
<dependencies>
@@ -111,6 +116,7 @@
111116
<resources>
112117
<resource>
113118
<directory>src/main/resources</directory>
119+
<filtering>true</filtering>
114120
</resource>
115121
<resource>
116122
<directory>html</directory>
@@ -173,8 +179,13 @@
173179
<version>${maven.jar.plugin.version}</version>
174180
<configuration>
175181
<archive>
182+
<manifest>
183+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
184+
</manifest>
176185
<manifestEntries>
177186
<Automatic-Module-Name>com.rabbitmq.perf</Automatic-Module-Name>
187+
<Implementation-Build>${buildNumber}</Implementation-Build>
188+
<Implementation-Build-Timestamp>${maven.build.timestamp}</Implementation-Build-Timestamp>
178189
</manifestEntries>
179190
</archive>
180191
</configuration>
@@ -189,6 +200,25 @@
189200
</configuration>
190201
</plugin>
191202

203+
<plugin>
204+
<groupId>org.codehaus.mojo</groupId>
205+
<artifactId>buildnumber-maven-plugin</artifactId>
206+
<version>${buildnumber.plugin.version}</version>
207+
<executions>
208+
<execution>
209+
<phase>validate</phase>
210+
<goals>
211+
<goal>create</goal>
212+
</goals>
213+
</execution>
214+
</executions>
215+
<configuration>
216+
<doCheck>false</doCheck>
217+
<doUpdate>false</doUpdate>
218+
<revisionOnScmFailure>unknown</revisionOnScmFailure>
219+
</configuration>
220+
</plugin>
221+
192222
</plugins>
193223
</build>
194224

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
package com.rabbitmq.perf;
1717

1818
import java.io.*;
19+
import java.nio.charset.Charset;
1920
import java.security.NoSuchAlgorithmException;
2021
import java.text.SimpleDateFormat;
2122
import java.util.Calendar;
2223
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
26+
import java.util.Locale;
2527
import java.util.Map;
2628
import java.util.Properties;
2729

30+
import com.rabbitmq.client.impl.ClientVersion;
2831
import org.apache.commons.cli.CommandLine;
2932
import org.apache.commons.cli.CommandLineParser;
3033
import org.apache.commons.cli.GnuParser;
@@ -55,6 +58,12 @@ public static void main(String[] args) {
5558
usage(options);
5659
System.exit(0);
5760
}
61+
62+
if (cmd.hasOption('v')) {
63+
versionInformation();
64+
System.exit(0);
65+
}
66+
5867
String testID = new SimpleDateFormat("HHmmss-SSS").format(Calendar.
5968
getInstance().getTime());
6069
testID = strArg(cmd, 'd', "test-"+testID);
@@ -271,6 +280,8 @@ private static Options getOptions() {
271280
options.addOption(new Option("qa", "queue-args", true, "queue arguments as key/pair values, separated by commas"));
272281
options.addOption(new Option("L", "consumer-latency", true, "consumer latency in microseconds"));
273282
options.addOption(new Option("udsc", "use-default-ssl-context", false,"use JVM default SSL context"));
283+
284+
options.addOption(new Option("v", "version", false,"print version information"));
274285
return options;
275286
}
276287

@@ -471,4 +482,27 @@ private static String rate(long count, long elapsed, boolean display) {
471482
}
472483
}
473484
}
485+
486+
private static void versionInformation() {
487+
String lineSeparator = System.getProperty("line.separator");
488+
String version = String.format(
489+
"RabbitMQ Perf Test %s (%s; %s)",
490+
Version.VERSION, Version.BUILD, Version.BUILD_TIMESTAMP
491+
);
492+
String info = String.format(
493+
"RabbitMQ AMQP Client version: %s" + lineSeparator +
494+
"Java version: %s, vendor: %s" + lineSeparator +
495+
"Java home: %s" + lineSeparator +
496+
"Default locale: %s, platform encoding: %s" + lineSeparator +
497+
"OS name: %s, version: %s, arch: %s",
498+
ClientVersion.VERSION,
499+
System.getProperty("java.version"), System.getProperty("java.vendor"),
500+
System.getProperty("java.home"),
501+
Locale.getDefault().toString(), Charset.defaultCharset().toString(),
502+
System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch")
503+
);
504+
System.out.println("\u001B[1m" + version);
505+
System.out.println("\u001B[0m" + info);
506+
}
507+
474508
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright (c) 2018-Present Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.perf;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.io.InputStream;
22+
import java.util.Properties;
23+
24+
/**
25+
* Current version of the tool.
26+
* Tries to get version information from a specific property file
27+
* and falls back to manifest information if the file cannot be loaded.
28+
*/
29+
public class Version {
30+
31+
public static final String VERSION, BUILD, BUILD_TIMESTAMP;
32+
33+
private static final Logger LOGGER = LoggerFactory.getLogger(Version.class);
34+
35+
static {
36+
VERSION = getVersion();
37+
BUILD = getBuild();
38+
BUILD_TIMESTAMP = getBuildTimestamp();
39+
}
40+
41+
private static final String getVersion() {
42+
String version;
43+
try {
44+
version = getValueFromPropertyFile("com.rabbitmq.perf.version");
45+
} catch (Exception e1) {
46+
LOGGER.warn("Couldn't get version from property file", e1);
47+
try {
48+
version = getVersionFromPackage();
49+
} catch (Exception e2) {
50+
LOGGER.warn("Couldn't get version with Package#getImplementationVersion", e1);
51+
version = getDefaultVersion();
52+
}
53+
}
54+
return version;
55+
}
56+
57+
private static final String getBuild() {
58+
String build;
59+
try {
60+
build = getValueFromPropertyFile("com.rabbitmq.perf.build");
61+
} catch (Exception e) {
62+
LOGGER.warn("Couldn't get build from property file", e);
63+
build = getDefaultBuild();
64+
}
65+
return build;
66+
}
67+
68+
private static final String getBuildTimestamp() {
69+
String build;
70+
try {
71+
build = getValueFromPropertyFile("com.rabbitmq.perf.build.timestamp");
72+
} catch (Exception e) {
73+
LOGGER.warn("Couldn't get build timestamp from property file", e);
74+
build = getDefaultBuildTimestamp();
75+
}
76+
return build;
77+
}
78+
79+
private static final String getValueFromPropertyFile(String key) throws Exception {
80+
InputStream inputStream = Version.class.getClassLoader().getResourceAsStream("rabbitmq-perf-test.properties");
81+
Properties version = new Properties();
82+
try {
83+
version.load(inputStream);
84+
} finally {
85+
if (inputStream != null) {
86+
inputStream.close();
87+
}
88+
}
89+
if (version.getProperty(key) == null) {
90+
throw new IllegalStateException("Coulnd't find " + key + " property in property file");
91+
}
92+
return version.getProperty(key);
93+
}
94+
95+
private static final String getVersionFromPackage() {
96+
if (Version.class.getPackage().getImplementationVersion() == null) {
97+
throw new IllegalStateException("Couldn't get version with Package#getImplementationVersion");
98+
}
99+
return Version.class.getPackage().getImplementationVersion();
100+
}
101+
102+
private static final String getDefaultVersion() {
103+
return "0.0.0";
104+
}
105+
106+
private static final String getDefaultBuild() {
107+
return "unknown";
108+
}
109+
110+
private static final String getDefaultBuildTimestamp() {
111+
return "unknown";
112+
}
113+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
com.rabbitmq.perf.version = ${project.version}
2+
com.rabbitmq.perf.build = ${buildNumber}
3+
com.rabbitmq.perf.build.timestamp = ${build.timestamp}

0 commit comments

Comments
 (0)