Search in sources :

Example 16 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ChanelHandlerTest method testClient.

@Test
public void testClient() throws Throwable {
    // read server info from property
    if (PerformanceUtils.getProperty("server", null) == null) {
        logger.warn("Please set -Dserver=127.0.0.1:9911");
        return;
    }
    final String server = System.getProperty("server", "127.0.0.1:9911");
    final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
    final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
    final int timeout = PerformanceUtils.getIntProperty(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    int sleep = PerformanceUtils.getIntProperty("sleep", 60 * 1000 * 60);
    final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + "&timeout=" + timeout;
    ExchangeClient exchangeClient = initClient(url);
    Thread.sleep(sleep);
    closeClient(exchangeClient);
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) Test(org.junit.Test)

Example 17 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ChanelHandlerTest method initClient.

public static ExchangeClient initClient(String url) {
    // Create client and build connection
    ExchangeClient exchangeClient = null;
    PeformanceTestHandler handler = new PeformanceTestHandler(url);
    boolean run = true;
    while (run) {
        try {
            exchangeClient = Exchangers.connect(url, handler);
        } catch (Throwable t) {
            if (t != null && t.getCause() != null && t.getCause().getClass() != null && (t.getCause().getClass() == java.net.ConnectException.class || t.getCause().getClass() == java.net.ConnectException.class)) {
            } else {
                t.printStackTrace();
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (exchangeClient != null) {
            run = false;
        }
    }
    return exchangeClient;
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient)

Example 18 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class PerformanceClientTest method testClient.

@Test
@SuppressWarnings("unchecked")
public void testClient() throws Throwable {
    // read server info from property
    if (PerformanceUtils.getProperty("server", null) == null) {
        logger.warn("Please set -Dserver=127.0.0.1:9911");
        return;
    }
    final String server = System.getProperty("server", "127.0.0.1:9911");
    final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
    final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
    final int timeout = PerformanceUtils.getIntProperty(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    final int length = PerformanceUtils.getIntProperty("length", 1024);
    final int connections = PerformanceUtils.getIntProperty(Constants.CONNECTIONS_KEY, 1);
    final int concurrent = PerformanceUtils.getIntProperty("concurrent", 100);
    int r = PerformanceUtils.getIntProperty("runs", 10000);
    final int runs = r > 0 ? r : Integer.MAX_VALUE;
    final String onerror = PerformanceUtils.getProperty("onerror", "continue");
    final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + "&timeout=" + timeout;
    // Create clients and build connections
    final ExchangeClient[] exchangeClients = new ExchangeClient[connections];
    for (int i = 0; i < connections; i++) {
        // exchangeClients[i] = Exchangers.connect(url,handler);
        exchangeClients[i] = Exchangers.connect(url);
    }
    List<String> serverEnvironment = (List<String>) exchangeClients[0].request("environment").get();
    List<String> serverScene = (List<String>) exchangeClients[0].request("scene").get();
    // Create some data for test
    StringBuilder buf = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        buf.append("A");
    }
    final String data = buf.toString();
    // counters
    final AtomicLong count = new AtomicLong();
    final AtomicLong error = new AtomicLong();
    final AtomicLong time = new AtomicLong();
    final AtomicLong all = new AtomicLong();
    // Start multiple threads
    final CountDownLatch latch = new CountDownLatch(concurrent);
    for (int i = 0; i < concurrent; i++) {
        new Thread(new Runnable() {

            public void run() {
                try {
                    AtomicInteger index = new AtomicInteger();
                    long init = System.currentTimeMillis();
                    for (int i = 0; i < runs; i++) {
                        try {
                            count.incrementAndGet();
                            ExchangeClient client = exchangeClients[index.getAndIncrement() % connections];
                            long start = System.currentTimeMillis();
                            String result = (String) client.request(data).get();
                            long end = System.currentTimeMillis();
                            if (!data.equals(result)) {
                                throw new IllegalStateException("Invalid result " + result);
                            }
                            time.addAndGet(end - start);
                        } catch (Exception e) {
                            error.incrementAndGet();
                            e.printStackTrace();
                            if ("exit".equals(onerror)) {
                                System.exit(-1);
                            } else if ("break".equals(onerror)) {
                                break;
                            } else if ("sleep".equals(onerror)) {
                                try {
                                    Thread.sleep(30000);
                                } catch (InterruptedException e1) {
                                }
                            }
                        }
                    }
                    all.addAndGet(System.currentTimeMillis() - init);
                } finally {
                    latch.countDown();
                }
            }
        }).start();
    }
    // Output, tps is not for accuracy, but it reflects the situation to a certain extent.
    new Thread(new Runnable() {

        public void run() {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                long lastCount = count.get();
                long sleepTime = 2000;
                long elapsd = sleepTime / 1000;
                boolean bfirst = true;
                while (latch.getCount() > 0) {
                    long c = count.get() - lastCount;
                    if (// The first time is inaccurate.
                    !bfirst)
                        System.out.println("[" + dateFormat.format(new Date()) + "] count: " + count.get() + ", error: " + error.get() + ",tps:" + (c / elapsd));
                    bfirst = false;
                    lastCount = count.get();
                    Thread.sleep(sleepTime);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    latch.await();
    for (ExchangeClient client : exchangeClients) {
        if (client.isConnected()) {
            client.close();
        }
    }
    long total = count.get();
    long failed = error.get();
    long succeeded = total - failed;
    long elapsed = time.get();
    long allElapsed = all.get();
    long clientElapsed = allElapsed - elapsed;
    long art = 0;
    long qps = 0;
    long throughput = 0;
    if (elapsed > 0) {
        art = elapsed / succeeded;
        qps = concurrent * succeeded * 1000 / elapsed;
        throughput = concurrent * succeeded * length * 2 * 1000 / elapsed;
    }
    PerformanceUtils.printBorder();
    PerformanceUtils.printHeader("Dubbo Remoting Performance Test Report");
    PerformanceUtils.printBorder();
    PerformanceUtils.printHeader("Test Environment");
    PerformanceUtils.printSeparator();
    for (String item : serverEnvironment) {
        PerformanceUtils.printBody("Server " + item);
    }
    PerformanceUtils.printSeparator();
    List<String> clientEnvironment = PerformanceUtils.getEnvironment();
    for (String item : clientEnvironment) {
        PerformanceUtils.printBody("Client " + item);
    }
    PerformanceUtils.printSeparator();
    PerformanceUtils.printHeader("Test Scene");
    PerformanceUtils.printSeparator();
    for (String item : serverScene) {
        PerformanceUtils.printBody("Server " + item);
    }
    PerformanceUtils.printBody("Client Transporter: " + transporter);
    PerformanceUtils.printBody("Serialization: " + serialization);
    PerformanceUtils.printBody("Response Timeout: " + timeout + " ms");
    PerformanceUtils.printBody("Data Length: " + length + " bytes");
    PerformanceUtils.printBody("Client Shared Connections: " + connections);
    PerformanceUtils.printBody("Client Concurrent Threads: " + concurrent);
    PerformanceUtils.printBody("Run Times Per Thread: " + runs);
    PerformanceUtils.printSeparator();
    PerformanceUtils.printHeader("Test Result");
    PerformanceUtils.printSeparator();
    PerformanceUtils.printBody("Succeeded Requests: " + DecimalFormat.getIntegerInstance().format(succeeded));
    PerformanceUtils.printBody("Failed Requests: " + failed);
    PerformanceUtils.printBody("Client Elapsed Time: " + clientElapsed + " ms");
    PerformanceUtils.printBody("Average Response Time: " + art + " ms");
    PerformanceUtils.printBody("Requests Per Second: " + qps + "/s");
    PerformanceUtils.printBody("Throughput Per Second: " + DecimalFormat.getIntegerInstance().format(throughput) + " bytes/s");
    PerformanceUtils.printBorder();
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 19 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ExchangeClientFactory method get.

public ExchangeClient get(final String targetIP, final int targetPort, final int connectTimeout, final int clientNums) throws Exception {
    String key = targetIP + ":" + targetPort;
    if (clients.containsKey(key)) {
        if (clientNums == 1) {
            return clients.get(key).get().get(0);
        } else {
            Random random = new Random();
            return clients.get(key).get().get(random.nextInt(clientNums));
        }
    } else {
        FutureTask<List<ExchangeClient>> task = new FutureTask<List<ExchangeClient>>(new Callable<List<ExchangeClient>>() {

            public List<ExchangeClient> call() throws Exception {
                List<ExchangeClient> clients = new ArrayList<ExchangeClient>(clientNums);
                for (int i = 0; i < clientNums; i++) {
                    clients.add(createClient(targetIP, targetPort, connectTimeout));
                }
                return clients;
            }
        });
        FutureTask<List<ExchangeClient>> currentTask = clients.putIfAbsent(key, task);
        if (currentTask == null) {
            task.run();
        } else {
            task = currentTask;
        }
        if (clientNums == 1)
            return task.get().get(0);
        else {
            Random random = new Random();
            return task.get().get(random.nextInt(clientNums));
        }
    }
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) Random(java.util.Random) FutureTask(java.util.concurrent.FutureTask) List(java.util.List) ArrayList(java.util.ArrayList)

Example 20 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class HeartbeatClient method main.

public static void main(String[] args) throws Exception {
    final HeartBeatExchangeHandler serverHandler = new HeartBeatExchangeHandler(handler);
    Thread serverThread = new Thread(new Runnable() {

        public void run() {
            try {
                exchangeServer = new HeaderExchangeServer(Transporters.bind(serverUrl, serverHandler));
                serverStarted = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    serverThread.setDaemon(true);
    serverThread.start();
    while (!serverStarted) {
        Thread.sleep(1000);
    }
    URL url = serverUrl.addParameter(Constants.HEARTBEAT_KEY, 1000);
    HeartBeatExchangeHandler clientHandler = new HeartBeatExchangeHandler(handler);
    ExchangeClient exchangeClient = new HeaderExchangeClient(Transporters.connect(url, clientHandler), true);
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        System.out.print(".");
    }
    System.out.println();
    if (serverHandler.getHeartBeatCount() > 0) {
        System.out.printf("Server receives %d heartbeats", serverHandler.getHeartBeatCount());
    } else {
        throw new Exception("Client heartbeat does not work.");
    }
    exchangeClient.close();
    exchangeServer.close();
}
Also used : HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) HeaderExchangeServer(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer) URL(com.alibaba.dubbo.common.URL)

Aggregations

ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)21 Test (org.junit.Test)6 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 RpcException (com.alibaba.dubbo.rpc.RpcException)4 URL (com.alibaba.dubbo.common.URL)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)2 HeaderExchangeClient (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient)2 HeaderExchangeServer (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 List (java.util.List)2 Random (java.util.Random)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Transporter (com.alibaba.dubbo.remoting.Transporter)1 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)1 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)1 ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1