001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes.test.transport;
018:
019: import java.io.OutputStream;
020: import java.net.Socket;
021: import java.text.DecimalFormat;
022: import org.apache.catalina.tribes.transport.nio.NioSender;
023: import org.apache.catalina.tribes.membership.MemberImpl;
024: import java.nio.channels.Selector;
025: import org.apache.catalina.tribes.io.XByteBuffer;
026: import org.apache.catalina.tribes.Member;
027: import java.nio.channels.SelectionKey;
028: import java.util.Iterator;
029: import org.apache.catalina.tribes.Channel;
030: import org.apache.catalina.tribes.io.ChannelData;
031: import java.math.BigDecimal;
032:
033: public class SocketNioSend {
034:
035: public static void main(String[] args) throws Exception {
036: Selector selector = Selector.open();
037: Member mbr = new MemberImpl("localhost", 9999, 0);
038: ChannelData data = new ChannelData();
039: data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
040: data.setAddress(mbr);
041: byte[] buf = new byte[8192 * 4];
042: data.setMessage(new XByteBuffer(buf, false));
043: buf = XByteBuffer.createDataPackage(data);
044: int len = buf.length;
045: BigDecimal total = new BigDecimal((double) 0);
046: BigDecimal bytes = new BigDecimal((double) len);
047: NioSender sender = new NioSender();
048: sender.setDestination(mbr);
049: sender.setDirectBuffer(true);
050: sender.setSelector(selector);
051: sender.setTxBufSize(1024 * 1024);
052: sender.connect();
053: sender.setMessage(buf);
054: System.out.println("Writing to 9999");
055: long start = 0;
056: double mb = 0;
057: boolean first = true;
058: int count = 0;
059: DecimalFormat df = new DecimalFormat("##.00");
060: while (count < 100000) {
061: if (first) {
062: first = false;
063: start = System.currentTimeMillis();
064: }
065: sender.setMessage(buf);
066: int selectedKeys = 0;
067: try {
068: selectedKeys = selector.select(0);
069: } catch (Exception e) {
070: e.printStackTrace();
071: continue;
072: }
073:
074: if (selectedKeys == 0) {
075: continue;
076: }
077:
078: Iterator it = selector.selectedKeys().iterator();
079: while (it.hasNext()) {
080: SelectionKey sk = (SelectionKey) it.next();
081: it.remove();
082: try {
083: int readyOps = sk.readyOps();
084: sk.interestOps(sk.interestOps() & ~readyOps);
085: if (sender.process(sk, false)) {
086: total = total.add(bytes);
087: sender.reset();
088: sender.setMessage(buf);
089: mb += ((double) len) / 1024 / 1024;
090: if (((++count) % 10000) == 0) {
091: long time = System.currentTimeMillis();
092: double seconds = ((double) (time - start)) / 1000;
093: System.out
094: .println("Throughput "
095: + df.format(mb / seconds)
096: + " MB/seconds, total "
097: + mb + " MB, total "
098: + total + " bytes.");
099: }
100: }
101:
102: } catch (Throwable t) {
103: t.printStackTrace();
104: return;
105: }
106: }
107: selector.selectedKeys().clear();
108: }
109: System.out.println("Complete, sleeping 15 seconds");
110: Thread.sleep(15000);
111: }
112: }
|