hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r910324 - in /httpcomponents/httpcore/trunk/httpcore-benchmark: ./ src/main/java/org/apache/http/benchmark/ src/main/java/org/apache/http/benchmark/httpcore/ src/main/java/org/apache/http/benchmark/jetty/
Date Mon, 15 Feb 2010 20:06:25 GMT
Author: olegk
Date: Mon Feb 15 20:06:25 2010
New Revision: 910324

URL: http://svn.apache.org/viewvc?rev=910324&view=rev
Log:
HTTPCORE-178: NIO benchmarks

Added:
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-benchmark/pom.xml
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/Benchmark.java
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreServer.java
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
    httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/RandomDataHandler.java

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/pom.xml?rev=910324&r1=910323&r2=910324&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/pom.xml (original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/pom.xml Mon Feb 15 20:06:25 2010
@@ -57,11 +57,23 @@
     </dependency>
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpcore-nio</artifactId>
+      <version>${pom.version}</version>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpcore-ab</artifactId>
       <version>${pom.version}</version>
       <scope>compile</scope>
     </dependency>
     <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+      <version>1.2</version>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-server</artifactId>
       <version>7.0.1.v20091125</version>

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/Benchmark.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/Benchmark.java?rev=910324&r1=910323&r2=910324&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/Benchmark.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/Benchmark.java
Mon Feb 15 20:06:25 2010
@@ -33,11 +33,13 @@
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
+import org.apache.http.benchmark.httpcore.HttpCoreNIOServer;
 import org.apache.http.benchmark.httpcore.HttpCoreServer;
+import org.apache.http.benchmark.jetty.JettyNIOServer;
 import org.apache.http.benchmark.jetty.JettyServer;
-import org.apache.http.contrib.benchmark.CommandLineUtils;
-import org.apache.http.contrib.benchmark.Config;
-import org.apache.http.contrib.benchmark.HttpBenchmark;
+import org.apache.http.benchmark.CommandLineUtils;
+import org.apache.http.benchmark.Config;
+import org.apache.http.benchmark.HttpBenchmark;
 
 public class Benchmark {
 
@@ -68,6 +70,8 @@
         Benchmark benchmark = new Benchmark();
         benchmark.run(new JettyServer(PORT), config);
         benchmark.run(new HttpCoreServer(PORT), config);
+        benchmark.run(new JettyNIOServer(PORT), config);
+        benchmark.run(new HttpCoreNIOServer(PORT), config);
     }
 
     public Benchmark() {

Added: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java?rev=910324&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
Mon Feb 15 20:06:25 2010
@@ -0,0 +1,140 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.benchmark.httpcore;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.benchmark.HttpServer;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
+import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
+import org.apache.http.nio.protocol.AsyncNHttpServiceHandler;
+import org.apache.http.nio.protocol.NHttpRequestHandlerRegistry;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.ListeningIOReactor;
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.SyncBasicHttpParams;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.ImmutableHttpProcessor;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+import org.apache.http.util.VersionInfo;
+
+public class HttpCoreNIOServer implements HttpServer {
+
+    private final NHttpListener listener;
+    
+    public HttpCoreNIOServer(int port) throws IOException {
+        if (port <= 0) {
+            throw new IllegalArgumentException("Server port may not be negative or null");
+        }
+        
+        HttpParams params = new SyncBasicHttpParams();
+        params
+            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000)
+            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 12 * 1024)
+            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
+            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpCore-NIO-Test/1.1");
+
+        HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[]
{
+                new ResponseDate(),
+                new ResponseServer(),
+                new ResponseContent(),
+                new ResponseConnControl()
+        });
+        
+        AsyncNHttpServiceHandler handler = new AsyncNHttpServiceHandler(
+                httpproc,
+                new DefaultHttpResponseFactory(),
+                new DefaultConnectionReuseStrategy(),
+                params);
+
+        NHttpRequestHandlerRegistry reqistry = new NHttpRequestHandlerRegistry();
+        reqistry.register("/rnd", new NRandomDataHandler());
+        handler.setHandlerResolver(reqistry);
+
+        ListeningIOReactor ioreactor = new DefaultListeningIOReactor(2, params);
+        ioreactor.listen(new InetSocketAddress(port));
+        
+        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);
+        this.listener = new NHttpListener(ioreactor, ioEventDispatch);
+    }
+
+    public String getName() {
+        return "HttpCore (NIO)";
+    }
+
+    public String getVersion() {
+        VersionInfo vinfo = VersionInfo.loadVersionInfo("org.apache.http", 
+                Thread.currentThread().getContextClassLoader());
+        return vinfo.getRelease();
+    }
+
+    public void start() throws Exception {
+        this.listener.start();
+    }
+    
+    public void shutdown() {
+        this.listener.terminate();
+        try {
+            this.listener.awaitTermination(1000);
+        } catch (InterruptedException ex) {
+        }
+        Exception ex = this.listener.getException();
+        if (ex != null) {
+            System.out.println("Error: " + ex.getMessage());
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        if (args.length != 1) {
+            System.out.println("Usage: <port>");
+            System.exit(1);
+        }
+        int port = Integer.parseInt(args[0]);
+        final HttpCoreNIOServer server = new HttpCoreNIOServer(port);
+        System.out.println("Listening on port: " + port);
+        server.start();
+        
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+
+            @Override
+            public void run() {
+                server.shutdown();
+            }
+            
+        });
+    }
+    
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreNIOServer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreServer.java?rev=910324&r1=910323&r2=910324&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/HttpCoreServer.java
Mon Feb 15 20:06:25 2010
@@ -112,12 +112,20 @@
             this.listener.awaitTermination(1000);
         } catch (InterruptedException ex) {
         }
+        Exception ex = this.listener.getException();
+        if (ex != null) {
+            System.out.println("Error: " + ex.getMessage());
+        }
         while (!this.workers.isEmpty()) {
             HttpWorker worker = this.workers.remove();
             worker.terminate();
             try {
                 worker.awaitTermination(1000);
-            } catch (InterruptedException ex) {
+            } catch (InterruptedException iex) {
+            }
+            ex = worker.getException();
+            if (ex != null) {
+                System.out.println("Error: " + ex.getMessage());
             }
         }
     }

Added: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java?rev=910324&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
Mon Feb 15 20:06:25 2010
@@ -0,0 +1,72 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.benchmark.httpcore;
+
+import java.io.IOException;
+
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.IOReactor;
+
+public class NHttpListener extends Thread {
+
+    private final IOReactor ioreactor;
+    private final IOEventDispatch ioEventDispatch;
+
+    private volatile Exception exception;
+    
+    public NHttpListener(
+            final IOReactor ioreactor, final IOEventDispatch ioEventDispatch) throws IOException
{
+        super();
+        this.ioreactor = ioreactor;
+        this.ioEventDispatch = ioEventDispatch;
+    }
+
+    @Override
+    public void run() {
+        try {
+            this.ioreactor.execute(this.ioEventDispatch);
+        } catch (Exception ex) {
+            this.exception = ex;
+        }
+    }
+
+    public void terminate() {
+        try {
+            this.ioreactor.shutdown();
+        } catch (IOException ex) {
+        }
+    }
+    
+    public Exception getException() {
+        return this.exception;
+    }
+    
+    public void awaitTermination(long millis) throws InterruptedException {
+        this.join(millis);
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NHttpListener.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java?rev=910324&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
Mon Feb 15 20:06:25 2010
@@ -0,0 +1,203 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.benchmark.httpcore;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Locale;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.MethodNotSupportedException;
+import org.apache.http.entity.AbstractHttpEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.entity.BufferingNHttpEntity;
+import org.apache.http.nio.entity.ConsumingNHttpEntity;
+import org.apache.http.nio.entity.ProducingNHttpEntity;
+import org.apache.http.nio.protocol.NHttpRequestHandler;
+import org.apache.http.nio.protocol.NHttpResponseTrigger;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+import org.apache.http.protocol.HttpContext;
+
+class NRandomDataHandler implements NHttpRequestHandler  {
+    
+    public NRandomDataHandler() {
+        super();
+    }
+    
+    public ConsumingNHttpEntity entityRequest(
+            final HttpEntityEnclosingRequest request,
+            final HttpContext context) throws HttpException, IOException {
+        // Use buffering entity for simplicity
+        return new BufferingNHttpEntity(request.getEntity(), new HeapByteBufferAllocator());
+    }
+
+    public void handle(
+            final HttpRequest request, 
+            final HttpResponse response, 
+            final NHttpResponseTrigger trigger,
+            final HttpContext context) throws HttpException, IOException {
+        String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
+        if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST"))
{
+            throw new MethodNotSupportedException(method + " method not supported"); 
+        }
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+            entity.consumeContent();
+        }
+        String target = request.getRequestLine().getUri();
+
+        int count = 100;
+        
+        int idx = target.indexOf('?');
+        if (idx != -1) {
+            String s = target.substring(idx + 1);
+            if (s.startsWith("c=")) {
+                s = s.substring(2);
+                try {
+                    count = Integer.parseInt(s);
+                } catch (NumberFormatException ex) {
+                    response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
+                    response.setEntity(new StringEntity("Invalid query format: " + s, 
+                            "text/plain", "ASCII"));
+                    return;
+                }
+            }
+        }
+        response.setStatusCode(HttpStatus.SC_OK);
+        RandomEntity body = new RandomEntity(count);
+        response.setEntity(body);
+        trigger.submitResponse(response);
+    }
+
+
+
+    public void handle(
+            final HttpRequest request, 
+            final HttpResponse response,
+            final HttpContext context) throws HttpException, IOException {
+        String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
+        if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST"))
{
+            throw new MethodNotSupportedException(method + " method not supported"); 
+        }
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+            entity.consumeContent();
+        }
+        String target = request.getRequestLine().getUri();
+
+        int count = 100;
+        
+        int idx = target.indexOf('?');
+        if (idx != -1) {
+            String s = target.substring(idx + 1);
+            if (s.startsWith("c=")) {
+                s = s.substring(2);
+                try {
+                    count = Integer.parseInt(s);
+                } catch (NumberFormatException ex) {
+                    response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
+                    response.setEntity(new StringEntity("Invalid query format: " + s, 
+                            "text/plain", "ASCII"));
+                    return;
+                }
+            }
+        }
+        response.setStatusCode(HttpStatus.SC_OK);
+        RandomEntity body = new RandomEntity(count);
+        response.setEntity(body);
+    }
+ 
+    static class RandomEntity extends AbstractHttpEntity implements ProducingNHttpEntity
{
+
+        private final int count;
+        private final ByteBuffer buf;
+        
+        private int remaining;
+        
+        public RandomEntity(int count) {
+            super();
+            this.count = count;
+            this.remaining = count;
+            this.buf = ByteBuffer.allocate(1024);
+            setContentType("text/plain");
+        }
+        
+        public InputStream getContent() throws IOException, IllegalStateException {
+            throw new IllegalStateException("Method not supported");
+        }
+
+        public long getContentLength() {
+            return this.count;
+        }
+
+        public boolean isRepeatable() {
+            return true;
+        }
+
+        public boolean isStreaming() {
+            return false;
+        }
+
+        public void writeTo(final OutputStream outstream) throws IOException {
+            throw new IllegalStateException("Method not supported");
+        }
+
+        public void produceContent(
+                final ContentEncoder encoder, final IOControl ioctrl) throws IOException
{
+            int r = Math.abs(this.buf.hashCode());
+            int chunk = Math.min(this.buf.remaining(), this.remaining);
+            if (chunk > 0) {
+                for (int i = 0; i < chunk; i++) {
+                    byte b = (byte) ((r + i) % 96 + 32);
+                    this.buf.put(b);
+                }
+            }
+            this.buf.flip();
+            int bytesWritten = encoder.write(this.buf);
+            this.remaining -= bytesWritten;
+            if (this.remaining == 0 && this.buf.remaining() == 0) {
+                encoder.complete();
+            }
+            this.buf.compact();
+        }
+        
+        public void finish() throws IOException {
+            this.remaining = this.count;
+        }
+
+    }
+    
+}
\ No newline at end of file

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/NRandomDataHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java?rev=910324&r1=910323&r2=910324&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/httpcore/RandomDataHandler.java
Mon Feb 15 20:06:25 2010
@@ -30,7 +30,6 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Locale;
-import java.util.Random;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -89,10 +88,12 @@
     static class RandomEntity extends AbstractHttpEntity {
 
         private int count;
+        private final byte[] buf;
         
         public RandomEntity(int count) {
             super();
             this.count = count;
+            this.buf = new byte[1024];
             setContentType("text/plain");
         }
         
@@ -113,19 +114,14 @@
         }
 
         public void writeTo(final OutputStream outstream) throws IOException {
-            Random rnd = new Random(System.currentTimeMillis());
-            byte[] tmp = new byte[1024];
+            int r = Math.abs(this.buf.hashCode());
             int remaining = this.count;
             while (remaining > 0) {
-                int chunk = Math.min(tmp.length, remaining);
+                int chunk = Math.min(this.buf.length, remaining);
                 for (int i = 0; i < chunk; i++) {
-                    byte ch = (byte) (Math.abs(rnd.nextInt()) % 127);
-                    if (ch < 32) {
-                        ch = ' ';
-                    }
-                    tmp[i] = ch;
+                    this.buf[i] = (byte) ((r + i) % 96 + 32);
                 }
-                outstream.write(tmp, 0, chunk);
+                outstream.write(this.buf, 0, chunk);
                 remaining -= chunk;
             }
         }

Added: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java?rev=910324&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
Mon Feb 15 20:06:25 2010
@@ -0,0 +1,99 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.benchmark.jetty;
+
+import java.io.IOException;
+
+import org.apache.http.benchmark.HttpServer;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
+
+public class JettyNIOServer implements HttpServer {
+
+    private final Server server;
+    
+    public JettyNIOServer(int port) throws IOException {
+        super();
+        if (port <= 0) {
+            throw new IllegalArgumentException("Server port may not be negative or null");
+        }
+        
+        SelectChannelConnector connector = new SelectChannelConnector();
+        connector.setPort(port);
+        connector.setRequestBufferSize(12 * 1024);
+        connector.setResponseBufferSize(12 * 1024);
+        
+        this.server = new Server();
+        this.server.addConnector(connector);
+        this.server.setHandler(new RandomDataHandler());
+    }
+    
+    public String getName() {
+        return "Jetty (NIO)";
+    }
+
+    public String getVersion() {
+        return Server.getVersion();
+    }
+
+    public void start() throws Exception {
+        this.server.start();
+    }
+    
+    public void shutdown() {
+        try {
+            this.server.stop();
+        } catch (Exception ex) {
+        }
+        try {
+            this.server.join();
+        } catch (InterruptedException ex) {
+        }
+    }
+    
+    public static void main(String[] args) throws Exception {
+        if (args.length != 1) {
+            System.out.println("Usage: <port>");
+            System.exit(1);
+        }
+        int port = Integer.parseInt(args[0]);
+        final JettyNIOServer server = new JettyNIOServer(port);
+        System.out.println("Listening on port: " + port);
+        server.start();
+        
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+
+            @Override
+            public void run() {
+                server.shutdown();
+            }
+            
+        });
+    }
+    
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/JettyNIOServer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/RandomDataHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/RandomDataHandler.java?rev=910324&r1=910323&r2=910324&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/RandomDataHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-benchmark/src/main/java/org/apache/http/benchmark/jetty/RandomDataHandler.java
Mon Feb 15 20:06:25 2010
@@ -29,7 +29,6 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Writer;
-import java.util.Random;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -79,17 +78,13 @@
         response.setContentLength(count);
         
         OutputStream outstream = response.getOutputStream();
-        Random rnd = new Random(System.currentTimeMillis());
         byte[] tmp = new byte[1024];
+        int r = Math.abs(tmp.hashCode());
         int remaining = count;
         while (remaining > 0) {
             int chunk = Math.min(tmp.length, remaining);
             for (int i = 0; i < chunk; i++) {
-                byte ch = (byte) (Math.abs(rnd.nextInt()) % 127);
-                if (ch < 32) {
-                    ch = ' ';
-                }
-                tmp[i] = ch;
+                tmp[i] = (byte) ((r + i) % 96 + 32);
             }
             outstream.write(tmp, 0, chunk);
             remaining -= chunk;



Mime
View raw message