diff options
Diffstat (limited to 'socket_test')
-rw-r--r-- | socket_test/OWNERS | 1 | ||||
-rw-r--r-- | socket_test/pom.xml | 47 | ||||
-rw-r--r-- | socket_test/src/main/java/com/yahoo/socket/test/SocketTestApp.java | 109 |
3 files changed, 157 insertions, 0 deletions
diff --git a/socket_test/OWNERS b/socket_test/OWNERS new file mode 100644 index 00000000000..3b2ba1ede81 --- /dev/null +++ b/socket_test/OWNERS @@ -0,0 +1 @@ +gjoranv diff --git a/socket_test/pom.xml b/socket_test/pom.xml new file mode 100644 index 00000000000..57e1d7ec980 --- /dev/null +++ b/socket_test/pom.xml @@ -0,0 +1,47 @@ +<?xml version="1.0"?> +<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.yahoo.vespa</groupId> + <artifactId>socket_test</artifactId> + <version>6-SNAPSHOT</version> + <packaging>jar</packaging> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <version>2.4</version> + <configuration> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + <archive> + <manifest> + <mainClass>com.yahoo.socket.test.SocketTestApp</mainClass> + </manifest> + </archive> + </configuration> + <executions> + <execution> + <id>make-assembly</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> diff --git a/socket_test/src/main/java/com/yahoo/socket/test/SocketTestApp.java b/socket_test/src/main/java/com/yahoo/socket/test/SocketTestApp.java new file mode 100644 index 00000000000..dfee51ce52d --- /dev/null +++ b/socket_test/src/main/java/com/yahoo/socket/test/SocketTestApp.java @@ -0,0 +1,109 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.socket.test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a> + */ +public class SocketTestApp { + + private final static byte[] REQUEST = "foo".getBytes(StandardCharsets.UTF_8); + private final static byte[] RESPONSE = "bar".getBytes(StandardCharsets.UTF_8); + private static volatile int numRequests = 0; + + public static void main(String[] args) throws IOException { + final boolean soLingerOn = Boolean.valueOf(args[0]); + final int soLingerTime = Integer.valueOf(args[1]); + final int numClients = Integer.valueOf(args[2]); + final int sleepMillis = Integer.valueOf(args[3]); + + System.out.println("soLingerOn = " + soLingerOn); + System.out.println("soLingerTime = " + soLingerTime); + System.out.println("numClients = " + numClients); + System.out.println("sleepMillis = " + sleepMillis); + + ServerSocket serverSocket = new ServerSocket(0); + for (int i = 0; i < numClients; ++i) { + new Client(serverSocket.getLocalPort()).start(); + } + + Executor workers = Executors.newFixedThreadPool(numClients); + long prev = System.currentTimeMillis(); + while (true) { + long next = System.currentTimeMillis(); + if (next > prev + 1000) { + System.err.println((numRequests * 1000) / (next - prev)); + numRequests = 0; + prev = next; + } + final Socket clientSocket = serverSocket.accept(); + clientSocket.setSoLinger(soLingerOn, soLingerTime); + workers.execute(new Runnable() { + + @Override + public void run() { + try { + InputStream in = clientSocket.getInputStream(); + for (byte expected : REQUEST) { + int actual = in.read(); + if (actual != expected) { + throw new AssertionError("Expected '" + expected + "', got '" + actual + "'."); + } + } + Thread.sleep(sleepMillis); + OutputStream out = clientSocket.getOutputStream(); + out.write(RESPONSE); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + try { + clientSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }); + } + } + + private static class Client extends Thread { + + final int port; + + Client(int port) throws IOException { + this.port = port; + } + + @Override + public void run() { + try { + while (!isInterrupted()) { + Socket socket = new Socket("localhost", port); + OutputStream out = socket.getOutputStream(); + out.write(REQUEST); + + InputStream in = socket.getInputStream(); + for (byte expected : RESPONSE) { + int actual = in.read(); + if (actual != expected) { + throw new AssertionError("Expected '" + expected + "', got '" + actual + "'."); + } + } + socket.close(); + ++numRequests; + } + } catch (Throwable t) { + t.printStackTrace(); + } + } + } +} |