You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jmeter.apache.org by GitBox <gi...@apache.org> on 2020/12/14 07:29:05 UTC

[GitHub] [jmeter] pmouawad commented on a change in pull request #637: add: tcp record proxy

pmouawad commented on a change in pull request #637:
URL: https://github.com/apache/jmeter/pull/637#discussion_r542162475



##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
##########
@@ -348,8 +350,17 @@ private TCPClient getProtocol() {
         return tcpClient;
     }
 
+    public SampleResult sample(){
+        return sample(null, true);
+    }
+
     @Override
     public SampleResult sample(Entry e)// Entry tends to be ignored ...
+    {
+        return sample(e, firstSample);
+    }
+
+    public SampleResult sample(Entry e, boolean firstSampleFlag)// Entry tends to be ignored ...

Review comment:
       Could you add javadoc explaining firstSampleFlag

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPSamplerManager.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.GuiPackage;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.tcp.control.gui.TCPSamplerGui;
+import org.apache.jmeter.protocol.tcp.proxy.gui.TCPProxyDef;
+import org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl;
+import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.testelement.TestElement;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPSamplerManager extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPSamplerManager.class);
+
+    private final JMeterTreeNode targetNode;
+    private final TestElement tcpSamplerElement;
+    private boolean workStatus = false;
+
+    private final Queue<TCPSampler> tcpSamplerInfoQueue = new ArrayDeque<>();
+
+    public TCPSamplerManager(TestElement tcpSamplerElement, JMeterTreeNode targetNode) {
+        this.tcpSamplerElement = tcpSamplerElement;
+        this.targetNode = targetNode;
+    }
+
+    /**
+     * create a new TCPSampler and put into TCPSampler queue.
+     * manager will store all TCPSampler to target JmeterTreeNode.
+     *
+     * @param data tcp payload
+     * @return target server response payload
+     */
+    public byte[] newTCPSampler(String data) {
+        log.debug("new sampler , payload " + data);
+        TCPSampler tcpSampler = new TCPSampler();
+        TestElement element = (TestElement) tcpSamplerElement.clone();
+        element.removeProperty(TCPSampler.PROXY_SERVER_PORT);
+        if (useHexPayload(element)) {
+            data = JOrphanUtils.baToHexString(data.getBytes());
+        }
+        element.setProperty(TCPSampler.REQUEST, data);
+        element.setProperty(TestElement.GUI_CLASS, TCPSamplerGui.class.getName());
+        element.setName("TCP Sampler");
+        tcpSampler.addTestElement(element);
+        tcpSamplerInfoQueue.add(tcpSampler);
+        SampleResult tcpSampleResult = tcpSampler.sample();
+        if (useHexPayload(element)) {
+            return BinaryTCPClientImpl.hexStringToByteArray(new String(tcpSampleResult.getResponseData()));
+        }
+        return tcpSampleResult.getResponseData();
+    }
+
+    private boolean useHexPayload(TestElement element) {
+        String tcpSamplerClassPath = element.getPropertyAsString(TCPSampler.CLASSNAME);
+        TCPProxyDef samplerClass = TCPProxyDef.findByClassPath(tcpSamplerClassPath);
+        if (samplerClass != null) {
+            switch (samplerClass) {
+                case BinaryTCPClientImpl_class:
+                case LengthPrefixedBinaryTCPClientImpl_class:
+                    return true;
+                case TCPClientImpl_class:
+                default:
+                    return false;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void managerStart() {
+        log.debug("start");
+        workStatus = true;
+        start();
+    }
+
+    /**
+     * manager will stop after samplerQueue is empty
+     */
+    public void managerStop() {
+        workStatus = false;
+    }
+
+    @Override
+    public void run() {
+        while (workStatus) {
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                e.printStackTrace();

Review comment:
       prefer logging

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPProxyServer.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPProxyServer extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPProxyServer.class);
+
+    ServerSocket serverSocket;
+    public static final int DEFAULT_PORT = 8899;
+    int proxyPort = DEFAULT_PORT;
+    int bufferSize = 4096;
+    boolean runningFlag = true;
+
+    private TCPSamplerManager samplerManager;
+
+    public TCPProxyServer(int proxyPort, TCPSamplerManager samplerManager) {
+        this.proxyPort = proxyPort;
+        this.samplerManager = samplerManager;
+    }
+
+    public void serverStart() throws IOException {
+        log.debug("proxy start");
+        runningFlag = true;
+        serverSocket = new ServerSocket(proxyPort);
+        log.debug("proxy server socket open success");
+        this.samplerManager.managerStart();
+        start();
+        log.debug("proxy start success");
+    }
+
+    public void serverStop() throws IOException {
+        runningFlag = false;
+        serverSocket.close();
+        this.samplerManager.managerStop();
+    }
+
+    @Override
+    public void run() {
+        if (serverSocket == null || serverSocket.isClosed()) {
+            try {
+                serverSocket = new ServerSocket(proxyPort);
+            } catch (IOException ioException) {
+                log.error("start proxy fail " + ioException.getMessage());
+            }
+        }
+        try {
+            while (runningFlag) {
+                Socket socket = serverSocket.accept();
+                log.debug("new client connection " + socket.getInetAddress().getHostName() + ":" + socket.getPort());
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            byte[] buffer = new byte[bufferSize];
+                            int readFlag = socket.getInputStream().read(buffer);
+                            if (readFlag != -1) {
+                                byte[] dataRead = Arrays.copyOf(buffer, readFlag);
+                                byte[] responseData = samplerManager.newTCPSampler(new String(dataRead));
+                                socket.getOutputStream().write(responseData);
+                            }
+                        } catch (IOException e) {
+                            log.debug("proxy socket exception " + e.getMessage());
+                        } finally {
+                            try {
+                                socket.close();
+                            } catch (IOException ignore) {
+                            }
+                        }
+                    }
+                }).start();
+            }
+        } catch (SocketException socketException) {
+            log.debug("proxy closed ");
+        } catch (IOException ioException) {
+            ioException.printStackTrace();

Review comment:
       Use logging

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPProxyServer.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPProxyServer extends Thread {

Review comment:
       Javadocs missing

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPSamplerManager.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.GuiPackage;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.tcp.control.gui.TCPSamplerGui;
+import org.apache.jmeter.protocol.tcp.proxy.gui.TCPProxyDef;
+import org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl;
+import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.testelement.TestElement;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPSamplerManager extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPSamplerManager.class);
+
+    private final JMeterTreeNode targetNode;
+    private final TestElement tcpSamplerElement;
+    private boolean workStatus = false;
+
+    private final Queue<TCPSampler> tcpSamplerInfoQueue = new ArrayDeque<>();
+
+    public TCPSamplerManager(TestElement tcpSamplerElement, JMeterTreeNode targetNode) {
+        this.tcpSamplerElement = tcpSamplerElement;
+        this.targetNode = targetNode;
+    }
+
+    /**
+     * create a new TCPSampler and put into TCPSampler queue.
+     * manager will store all TCPSampler to target JmeterTreeNode.
+     *
+     * @param data tcp payload
+     * @return target server response payload
+     */
+    public byte[] newTCPSampler(String data) {
+        log.debug("new sampler , payload " + data);
+        TCPSampler tcpSampler = new TCPSampler();
+        TestElement element = (TestElement) tcpSamplerElement.clone();
+        element.removeProperty(TCPSampler.PROXY_SERVER_PORT);
+        if (useHexPayload(element)) {
+            data = JOrphanUtils.baToHexString(data.getBytes());
+        }
+        element.setProperty(TCPSampler.REQUEST, data);
+        element.setProperty(TestElement.GUI_CLASS, TCPSamplerGui.class.getName());
+        element.setName("TCP Sampler");
+        tcpSampler.addTestElement(element);
+        tcpSamplerInfoQueue.add(tcpSampler);
+        SampleResult tcpSampleResult = tcpSampler.sample();
+        if (useHexPayload(element)) {
+            return BinaryTCPClientImpl.hexStringToByteArray(new String(tcpSampleResult.getResponseData()));
+        }
+        return tcpSampleResult.getResponseData();
+    }
+
+    private boolean useHexPayload(TestElement element) {
+        String tcpSamplerClassPath = element.getPropertyAsString(TCPSampler.CLASSNAME);
+        TCPProxyDef samplerClass = TCPProxyDef.findByClassPath(tcpSamplerClassPath);
+        if (samplerClass != null) {
+            switch (samplerClass) {
+                case BinaryTCPClientImpl_class:
+                case LengthPrefixedBinaryTCPClientImpl_class:
+                    return true;
+                case TCPClientImpl_class:
+                default:
+                    return false;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void managerStart() {
+        log.debug("start");

Review comment:
       Maybe add some contextual (port ...) data to log message

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPSamplerManager.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.GuiPackage;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.tcp.control.gui.TCPSamplerGui;
+import org.apache.jmeter.protocol.tcp.proxy.gui.TCPProxyDef;
+import org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl;
+import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.testelement.TestElement;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPSamplerManager extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPSamplerManager.class);
+
+    private final JMeterTreeNode targetNode;
+    private final TestElement tcpSamplerElement;
+    private boolean workStatus = false;
+
+    private final Queue<TCPSampler> tcpSamplerInfoQueue = new ArrayDeque<>();
+
+    public TCPSamplerManager(TestElement tcpSamplerElement, JMeterTreeNode targetNode) {
+        this.tcpSamplerElement = tcpSamplerElement;
+        this.targetNode = targetNode;
+    }
+
+    /**
+     * create a new TCPSampler and put into TCPSampler queue.
+     * manager will store all TCPSampler to target JmeterTreeNode.
+     *
+     * @param data tcp payload
+     * @return target server response payload
+     */
+    public byte[] newTCPSampler(String data) {
+        log.debug("new sampler , payload " + data);

Review comment:
       use log.debug("New sampler with payload{}", data)

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPProxyServer.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPProxyServer extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPProxyServer.class);
+
+    ServerSocket serverSocket;
+    public static final int DEFAULT_PORT = 8899;
+    int proxyPort = DEFAULT_PORT;
+    int bufferSize = 4096;
+    boolean runningFlag = true;
+
+    private TCPSamplerManager samplerManager;
+
+    public TCPProxyServer(int proxyPort, TCPSamplerManager samplerManager) {
+        this.proxyPort = proxyPort;
+        this.samplerManager = samplerManager;
+    }
+
+    public void serverStart() throws IOException {
+        log.debug("proxy start");
+        runningFlag = true;
+        serverSocket = new ServerSocket(proxyPort);
+        log.debug("proxy server socket open success");
+        this.samplerManager.managerStart();
+        start();
+        log.debug("proxy start success");
+    }
+
+    public void serverStop() throws IOException {
+        runningFlag = false;
+        serverSocket.close();
+        this.samplerManager.managerStop();
+    }
+
+    @Override
+    public void run() {
+        if (serverSocket == null || serverSocket.isClosed()) {
+            try {
+                serverSocket = new ServerSocket(proxyPort);
+            } catch (IOException ioException) {
+                log.error("start proxy fail " + ioException.getMessage());
+            }
+        }
+        try {
+            while (runningFlag) {
+                Socket socket = serverSocket.accept();
+                log.debug("new client connection " + socket.getInetAddress().getHostName() + ":" + socket.getPort());
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            byte[] buffer = new byte[bufferSize];
+                            int readFlag = socket.getInputStream().read(buffer);
+                            if (readFlag != -1) {
+                                byte[] dataRead = Arrays.copyOf(buffer, readFlag);
+                                byte[] responseData = samplerManager.newTCPSampler(new String(dataRead));
+                                socket.getOutputStream().write(responseData);
+                            }
+                        } catch (IOException e) {
+                            log.debug("proxy socket exception " + e.getMessage());
+                        } finally {
+                            try {
+                                socket.close();
+                            } catch (IOException ignore) {
+                            }
+                        }
+                    }
+                }).start();
+            }
+        } catch (SocketException socketException) {
+            log.debug("proxy closed ");

Review comment:
       Add contextual data and exception
   log.debug("Proxy closed", socketException)

##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPSamplerManager.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.jmeter.protocol.tcp.proxy;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.GuiPackage;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.tcp.control.gui.TCPSamplerGui;
+import org.apache.jmeter.protocol.tcp.proxy.gui.TCPProxyDef;
+import org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl;
+import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.testelement.TestElement;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPSamplerManager extends Thread {

Review comment:
       Add some javadoc explaining class purpose




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org