You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/11/18 00:35:43 UTC

svn commit: r345364 - in /jakarta/jmeter/branches/rel-2-1/src: core/org/apache/jmeter/util/SlowSocket.java protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java

Author: sebb
Date: Thu Nov 17 15:35:33 2005
New Revision: 345364

URL: http://svn.apache.org/viewcvs?rev=345364&view=rev
Log:
Classes to support slow sockets (modem emulation)

Added:
    jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java   (with props)
    jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java   (with props)

Added: jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java
URL: http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java?rev=345364&view=auto
==============================================================================
--- jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java (added)
+++ jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java Thu Nov 17 15:35:33 2005
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.util;
+
+import java.io.FilterInputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/**
+ * "Slow" socket implementation to emulate dial-up modems etc
+ */
+public class SlowSocket extends Socket {
+
+    private final int CPS; // Characters per second to emulate
+    
+    /**
+     * 
+     * @param cps characters per second
+     * @param host hostname
+     * @param port port
+     * @param localAddr local address
+     * @param localPort local port
+     * 
+     * @throws IOException
+     * @throws IllegalArgumentException if cps <=0
+     */
+    public SlowSocket(int cps, String host, int port, InetAddress localAddr, int localPort) throws IOException {
+        super(host, port, localAddr, localPort);
+        if (cps <=0) {
+            throw new IllegalArgumentException("Speed (cps) <= 0");
+        }
+        CPS=cps;
+    }
+
+    /**
+     * 
+     * @param cps characters per second
+     * @param host hostname
+     * @param port port
+     * 
+     * @throws UnknownHostException
+     * @throws IOException
+     * @throws IllegalArgumentException if cps <=0
+     */
+    public SlowSocket(int cps, String host, int port) throws UnknownHostException, IOException {
+        super(host, port);
+        if (cps <=0) {
+            throw new IllegalArgumentException("Speed (cps) <= 0");
+        }
+        CPS=cps;
+    }
+
+    // Override so we can intercept the stream
+    public OutputStream getOutputStream() throws IOException {
+        return new SlowOutputStream(super.getOutputStream());
+    }
+    
+    // Override so we can intercept the stream
+    public InputStream getInputStream() throws IOException {
+        return new SlowInputStream(super.getInputStream());
+    }
+
+    private void pause(int bytes){
+        try {
+            Thread.sleep(bytes*1000/CPS);
+        } catch (InterruptedException ignored) {
+        }
+    }
+    
+    class SlowInputStream extends FilterInputStream {
+
+        public SlowInputStream(InputStream in) {
+            super(in);
+        }
+
+        public int read() throws IOException {
+            pause(1);
+            return in.read();
+        }
+
+        public int read(byte[] b, int off, int len) throws IOException {
+            pause(len);
+            return in.read(b, off, len);
+        }
+
+    }
+    
+    class SlowOutputStream extends FilterOutputStream {
+
+        public SlowOutputStream(OutputStream out) {
+            super(out);
+        }
+
+        public void write(byte[] b, int off, int len) throws IOException {
+            pause(len);
+            out.write(b, off, len);
+        }
+
+        public void write(int b) throws IOException {
+            pause(1);
+            out.write(b);
+        }
+    }
+}
\ No newline at end of file

Propchange: jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/jmeter/branches/rel-2-1/src/core/org/apache/jmeter/util/SlowSocket.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java?rev=345364&view=auto
==============================================================================
--- jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java (added)
+++ jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java Thu Nov 17 15:35:33 2005
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.http.util;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
+import org.apache.jmeter.util.SlowSocket;
+
+/**
+ * HttpClient protocol factory to generate "slow" sockets for emulating dial-up modems 
+ */
+
+public class SlowHttpClientSocketFactory implements ProtocolSocketFactory {
+
+    private final int CPS; // Characters per second to emulate
+
+    /**
+     * 
+     * @param cps - characters per second
+     */
+    public SlowHttpClientSocketFactory(final int cps) {
+        super();
+        CPS = cps;
+    }
+
+    public Socket createSocket(String host, int port, InetAddress clientHost,
+            int clientPort) throws IOException, UnknownHostException {
+        return new SlowSocket(CPS,host,port,clientHost,clientPort);
+    }
+
+    public Socket createSocket(String host, int port) throws IOException,
+            UnknownHostException {
+        return new SlowSocket(CPS,host,port);
+    }
+}

Propchange: jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHttpClientSocketFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org