This is an automated email from the ASF dual-hosted git repository.

zhaijia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new b43e83c  Enable pulsar-perf to load WebSocket service URL from conf 
file (#9000)
b43e83c is described below

commit b43e83c87b7cb7a9e622a7001f33e75347df1c65
Author: Masahiro Sakamoto <[email protected]>
AuthorDate: Sun Dec 20 22:25:50 2020 +0900

    Enable pulsar-perf to load WebSocket service URL from conf file (#9000)
    
    ### Motivation
    
    Currently, the command line option `--proxy-url` is required when running 
the subcommand `websocket-producer` of `pulsar-perf`.
    
    ```sh
    $ ./bin/pulsar-perf websocket-producer
    
    The following option is required: -u, --proxy-url
    Usage: pulsar-perf websocket-producer [options] 
persistent://tenant/ns/my-topic
      Options:
        ...
      * -u, --proxy-url
           Pulsar Proxy URL, e.g., "ws://localhost:8080/"
    ```
    
    I think it would be useful to be able to load the WebSocket service URL 
from the config file (such as `client.conf`) as well as other parameters.
    
    ### Modifications
    
    `pulsar-perf` loads the WebSocket service URL from the config file if 
`--proxy-url` is not specified. The priorities are as follows:
    
    1. `--proxy-url` option
    2. `webSocketServiceUrl` in the config file
    3. `webServiceUrl` in the config file
    4. `serviceUrl` in the config file
---
 .../proxy/socket/client/PerformanceClient.java     | 31 ++++++++-
 .../proxy/socket/client/PerformanceClientTest.java | 75 ++++++++++++++++++++++
 .../src/test/resources/websocket_client1.conf      | 22 +++++++
 .../src/test/resources/websocket_client2.conf      | 21 ++++++
 .../src/test/resources/websocket_client3.conf      | 20 ++++++
 5 files changed, 166 insertions(+), 3 deletions(-)

diff --git 
a/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
 
b/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
index 5253d63..9722677 100644
--- 
a/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
+++ 
b/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
@@ -18,6 +18,9 @@
  */
 package org.apache.pulsar.proxy.socket.client;
 
+import static org.apache.commons.lang3.StringUtils.isBlank;
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -72,7 +75,7 @@ public class PerformanceClient {
         @Parameter(names = { "--conf-file" }, description = "Configuration 
file")
         public String confFile;
 
-        @Parameter(names = { "-u", "--proxy-url" }, description = "Pulsar 
Proxy URL, e.g., \"ws://localhost:8080/\"", required = true)
+        @Parameter(names = { "-u", "--proxy-url" }, description = "Pulsar 
Proxy URL, e.g., \"ws://localhost:8080/\"")
         public String proxyURL;
 
         @Parameter(description = "persistent://tenant/ns/my-topic", required = 
true)
@@ -144,8 +147,22 @@ public class PerformanceClient {
                 System.exit(1);
             }
 
-            if (arguments.proxyURL == null) {
-                arguments.proxyURL = prop.getProperty("serviceUrl", 
"http://localhost:8080/";);
+            if (isBlank(arguments.proxyURL)) {
+                String webSocketServiceUrl = 
prop.getProperty("webSocketServiceUrl");
+                if (isNotBlank(webSocketServiceUrl)) {
+                    arguments.proxyURL = webSocketServiceUrl;
+                } else {
+                    String webServiceUrl = 
isNotBlank(prop.getProperty("webServiceUrl"))
+                            ? prop.getProperty("webServiceUrl")
+                            : prop.getProperty("serviceUrl");
+                    if (isNotBlank(webServiceUrl)) {
+                        if (webServiceUrl.startsWith("ws://") || 
webServiceUrl.startsWith("wss://")) {
+                            arguments.proxyURL = webServiceUrl;
+                        } else if (webServiceUrl.startsWith("http://";) || 
webServiceUrl.startsWith("https://";)) {
+                            arguments.proxyURL = 
webServiceUrl.replaceFirst("^http", "ws");
+                        }
+                    }
+                }
             }
 
             if (arguments.authPluginClassName == null) {
@@ -157,6 +174,14 @@ public class PerformanceClient {
             }
         }
 
+        if (isBlank(arguments.proxyURL)) {
+            arguments.proxyURL = "ws://localhost:8080/";
+        }
+
+        if (!arguments.proxyURL.endsWith("/")) {
+            arguments.proxyURL += "/";
+        }
+
         arguments.testTime = TimeUnit.SECONDS.toMillis(arguments.testTime);
 
         return arguments;
diff --git 
a/pulsar-testclient/src/test/java/org/apache/pulsar/proxy/socket/client/PerformanceClientTest.java
 
b/pulsar-testclient/src/test/java/org/apache/pulsar/proxy/socket/client/PerformanceClientTest.java
new file mode 100644
index 0000000..135bb7b
--- /dev/null
+++ 
b/pulsar-testclient/src/test/java/org/apache/pulsar/proxy/socket/client/PerformanceClientTest.java
@@ -0,0 +1,75 @@
+/**
+ * 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.pulsar.proxy.socket.client;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PerformanceClientTest {
+    @Test(timeOut = 5000)
+    public void testLoadArguments() throws Exception {
+        PerformanceClient client = new PerformanceClient();
+
+        // "--proxy-url" has the highest priority
+        PerformanceClient.Arguments arguments = client.loadArguments(
+                getArgs("ws://broker0.pulsar.apache.org:8080/", 
"./src/test/resources/websocket_client1.conf"));
+        assertEquals(arguments.proxyURL, 
"ws://broker0.pulsar.apache.org:8080/");
+
+        // "webSocketServiceUrl" written in the conf file has the second 
priority
+        arguments = client.loadArguments(getArgs(null, 
"./src/test/resources/websocket_client1.conf"));
+        assertEquals(arguments.proxyURL, 
"ws://broker1.pulsar.apache.org:8080/");
+
+        // "webServiceUrl" written in the conf file has the third priority
+        arguments = client.loadArguments(getArgs(null, 
"./src/test/resources/websocket_client2.conf"));
+        assertEquals(arguments.proxyURL, 
"ws://broker2.pulsar.apache.org:8080/");
+
+        // "serviceUrl" written in the conf file has the fourth priority
+        arguments = client.loadArguments(getArgs(null, 
"./src/test/resources/websocket_client3.conf"));
+        assertEquals(arguments.proxyURL, 
"wss://broker3.pulsar.apache.org:8443/");
+
+        // The default value is "ws://localhost:8080/"
+        arguments = client.loadArguments(getArgs(null, null));
+        assertEquals(arguments.proxyURL, "ws://localhost:8080/");
+
+        // If the URL does not end with "/", it will be added
+        arguments = 
client.loadArguments(getArgs("ws://broker0.pulsar.apache.org:8080", null));
+        assertEquals(arguments.proxyURL, 
"ws://broker0.pulsar.apache.org:8080/");
+    }
+
+    private String[] getArgs(String proxyUrl, String confFile) {
+        List<String> args = new ArrayList<>();
+
+        if (proxyUrl != null) {
+            args.add("--proxy-url");
+            args.add(proxyUrl);
+        }
+
+        if (confFile != null) {
+            args.add("--conf-file");
+            args.add(confFile);
+        }
+
+        args.add("persistent://public/default/dummy");
+        return args.toArray(new String[args.size()]);
+    }
+}
diff --git a/pulsar-testclient/src/test/resources/websocket_client1.conf 
b/pulsar-testclient/src/test/resources/websocket_client1.conf
new file mode 100644
index 0000000..75e1c67
--- /dev/null
+++ b/pulsar-testclient/src/test/resources/websocket_client1.conf
@@ -0,0 +1,22 @@
+#
+# 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.
+#
+
+webSocketServiceUrl=ws://broker1.pulsar.apache.org:8080/
+webServiceUrl=http://broker2.pulsar.apache.org:8080/
+serviceUrl=https://broker3.pulsar.apache.org:8443/
diff --git a/pulsar-testclient/src/test/resources/websocket_client2.conf 
b/pulsar-testclient/src/test/resources/websocket_client2.conf
new file mode 100644
index 0000000..469e783
--- /dev/null
+++ b/pulsar-testclient/src/test/resources/websocket_client2.conf
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+
+webServiceUrl=http://broker2.pulsar.apache.org:8080/
+serviceUrl=https://broker3.pulsar.apache.org:8443/
diff --git a/pulsar-testclient/src/test/resources/websocket_client3.conf 
b/pulsar-testclient/src/test/resources/websocket_client3.conf
new file mode 100644
index 0000000..31f69b5
--- /dev/null
+++ b/pulsar-testclient/src/test/resources/websocket_client3.conf
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+serviceUrl=https://broker3.pulsar.apache.org:8443/

Reply via email to