From: <wo...@us...> - 2003-02-27 21:28:54
|
Update of /cvsroot/jmule/jMule/src/wolf/nio/channels/shaped/tests In directory sc8-pr-cvs1:/tmp/cvs-serv29882/src/wolf/nio/channels/shaped/tests Added Files: ShapedSelectorProviderTest.java Log Message: --- NEW FILE: ShapedSelectorProviderTest.java --- package wolf.nio.channels.shaped.tests; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.channels.spi.SelectorProvider; import java.util.Iterator; import wolf.nio.channels.shaped.ShapedSelectorProvider; import wolf.nio.channels.shaped.ShapedSocketChannel; import wolf.nio.channels.shaped.ShapedSocketChannelImpl; import wolf.nio.channels.shaped.Shaper; import junit.framework.TestCase; /** * @author carlo * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class ShapedSelectorProviderTest extends TestCase { static class Data { ByteBuffer buf = ByteBuffer.allocate(16 * 1024); ShapedSocketChannel channel; int total = 0; } /** * Constructor for ShapedSelectorProviderTest. * @param arg0 */ public ShapedSelectorProviderTest(String arg0) { super(arg0); } public static void main(String[] args) { junit.textui.TestRunner.run(ShapedSelectorProviderTest.class); } /** * Test blocking io */ public void _test1() throws IOException { SelectorProvider provider = new ShapedSelectorProvider(); //SelectorProvider provider = SelectorProvider.provider(); SocketChannel s = provider.openSocketChannel(); s.connect(new InetSocketAddress("wolf.xs4all.nl", 9)); ByteBuffer src = ByteBuffer.allocate(1024 * 16); for(int i = 0; i < src.capacity(); i++) { src.put((byte) 0x00); } src.flip(); long total = 0; long start = System.currentTimeMillis(); long lastReport = 0; for(int i = 0; i < 10; i++) { int l = s.write(src); assert l == src.capacity() : l; total += l; src.flip(); long now = System.currentTimeMillis(); if(now > (lastReport + 5000)) { long delta = Math.max(now - start, 1); System.out.println("rate = " + ((total * 1000) / delta) + "bps"); lastReport = now; } } long now = System.currentTimeMillis(); long delta = now - start; long rate = (total * 1000) / delta; System.out.println("rate = " + rate + "bps"); assertTrue("to fast", rate < 10500); s.close(); } /** * Test a blocking server socket */ public void _test2() throws IOException { SelectorProvider provider = new ShapedSelectorProvider(); //Selector sel = provider.openSelector(); ServerSocketChannel server = provider.openServerSocketChannel(); server.socket().bind(new InetSocketAddress(9999)); //server.configureBlocking(false); //server.register(sel, SelectionKey.OP_ACCEPT, server); System.out.println("Accepting"); ShapedSocketChannel s = (ShapedSocketChannel) server.accept(); s.setIncomingShaper(new Shaper()); //s.configureBlocking(true); ByteBuffer dst = ByteBuffer.allocate(1024 * 16); long total = 0; long start = System.currentTimeMillis(); long lastReport = 0; for(int i = 0; i < 10; i++) { int l = s.read(dst); assert l == dst.capacity() : l; total += l; dst.flip(); long now = System.currentTimeMillis(); if(now > (lastReport + 5000)) { long delta = Math.max(now - start, 1); System.out.println("rate = " + ((total * 1000) / delta) + "bps"); lastReport = now; } } long now = System.currentTimeMillis(); long delta = now - start; long rate = (total * 1000) / delta; System.out.println("rate = " + rate + "bps"); assertTrue("to fast", rate < 10500); s.close(); } /** * Test a non-blocking server socket */ public void test3() throws IOException { // use a single shaper Shaper shaper = new Shaper(); SelectorProvider provider = new ShapedSelectorProvider(); Selector sel = provider.openSelector(); ServerSocketChannel server = provider.openServerSocketChannel(); server.socket().bind(new InetSocketAddress(9999)); server.configureBlocking(false); server.register(sel, SelectionKey.OP_ACCEPT); ByteBuffer dst = ByteBuffer.allocate(1024 * 16); long total = 0; long start = System.currentTimeMillis(); long lastReport = 0; while(sel.keys().size() > 0) { int n = sel.select(1000); Iterator i = sel.selectedKeys().iterator(); while(i.hasNext()) { SelectionKey key = (SelectionKey) i.next(); i.remove(); if(key.isAcceptable()) { Data data = new Data(); ShapedSocketChannel s = (ShapedSocketChannel) server.accept(); s.configureBlocking(false); s.setIncomingShaper(shaper); s.register(sel, SelectionKey.OP_READ, data); data.channel = s; } if(key.isReadable()) { SocketChannel s = (SocketChannel) key.channel(); Data data = (Data) key.attachment(); int l = s.read(data.buf); if(data.buf.remaining() <= 0) data.buf.rewind(); if(l < 0) s.close(); else { //System.out.println("s = " + s + ", l = " + l); data.total += l; total += l; } } } long now = System.currentTimeMillis(); if(now > (lastReport + 1000)) { long delta = Math.max(now - start, 1); System.out.println("rate = " + ((total * 1000) / delta) + "bps"); lastReport = now; Iterator it = sel.keys().iterator(); while(it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if(key.attachment() != null) { Data data = (Data) key.attachment(); System.out.println(data.channel + ": rate = " + ((data.total * 1000) / delta) + "bps"); data.total = 0; } } total = 0; start = now; } } /* System.out.println("Accepting"); ShapedSocketChannel s = (ShapedSocketChannel) server.accept(); s.setIncomingShaper(new Shaper()); //s.configureBlocking(true); long total = 0; long start = System.currentTimeMillis(); long lastReport = 0; for(int i = 0; i < 10; i++) { int l = s.read(dst); assert l == dst.capacity() : l; total += l; dst.flip(); long now = System.currentTimeMillis(); if(now > (lastReport + 5000)) { long delta = Math.max(now - start, 1); System.out.println("rate = " + ((total * 1000) / delta) + "bps"); lastReport = now; } } long now = System.currentTimeMillis(); long delta = now - start; long rate = (total * 1000) / delta; System.out.println("rate = " + rate + "bps"); assertTrue("to fast", rate < 10500); s.close(); */ } } |