Source Code Cross Referenced for BenchmarkingClients.java in  » Web-Server » xsocket » org » xsocket » connection » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » xsocket » org.xsocket.connection 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: DisconnectTest.java 1156 2007-04-15 08:10:39Z grro $
002:        /*
003:         *  Copyright (c) xsocket.org, 2006. All rights reserved.
004:         *
005:         *  This library is free software; you can redistribute it and/or
006:         *  modify it under the terms of the GNU Lesser General Public
007:         *  License as published by the Free Software Foundation; either
008:         *  version 2.1 of the License, or (at your option) any later version.
009:         *
010:         *  This library is distributed in the hope that it will be useful,
011:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         *  Lesser General Public License for more details.
014:         *
015:         *  You should have received a copy of the GNU Lesser General Public
016:         *  License along with this library; if not, write to the Free Software
017:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         *
019:         * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020:         * The latest copy of this software may be found on http://www.xsocket.org/
021:         */
022:        package org.xsocket.connection;
023:
024:        import java.io.IOException;
025:        import java.io.InputStreamReader;
026:        import java.io.LineNumberReader;
027:        import java.io.OutputStreamWriter;
028:        import java.io.PrintWriter;
029:        import java.net.Socket;
030:        import java.nio.BufferUnderflowException;
031:
032:        import org.xsocket.MaxReadSizeExceededException;
033:        import org.xsocket.connection.BlockingConnection;
034:        import org.xsocket.connection.IBlockingConnection;
035:
036:        /**
037:         *
038:         * @author grro@xsocket.org
039:         */
040:        public final class BenchmarkingClients {
041:
042:            private static final String DELIMITER = "\r\n";
043:
044:            public static final void main(String... args) throws Exception {
045:                if (args.length != 2) {
046:                    System.out
047:                            .println("usage java org.xsocket.stream.BenchmarkClient <host> <port>");
048:                    System.exit(-1);
049:                }
050:
051:                new BenchmarkingClients().launch(args[0], Integer
052:                        .parseInt(args[1]));
053:            }
054:
055:            public void launch(String host, int port) throws IOException {
056:                compare(host, port, 10, 100, 1);
057:
058:                System.out
059:                        .println("\n\n\nloops, dataSize, packages, direct,  blocking,  nonblocking");
060:                compare(host, port, 10, 1000, 1);
061:                compare(host, port, 10, 1000, 10);
062:                compare(host, port, 1000, 1000, 1);
063:                compare(host, port, 1000, 1000, 10);
064:                compare(host, port, 50000, 1000, 2);
065:                compare(host, port, 1000000, 50, 1);
066:            }
067:
068:            private void compare(String host, int port, int size, int loops,
069:                    int packages) throws IOException {
070:                long timeDirect = callDirect(host, port, size, loops, packages);
071:                long timeBlocking = callBlockingConnection(host, port, size,
072:                        loops, packages);
073:                long timeNonBlocking = callNonBlockingConnection(host, port,
074:                        size, loops, packages);
075:                System.out.println(loops + ",  " + size + ",  " + packages
076:                        + ",  " + timeDirect + ",  " + timeBlocking + ",  "
077:                        + timeNonBlocking);
078:            }
079:
080:            static long callDirect(String host, int port, int dataSize,
081:                    int loops, int packages) throws IOException {
082:                String data = new String(QAUtil.generateByteArray(dataSize));
083:
084:                long start = System.currentTimeMillis();
085:
086:                for (int i = 0; i < loops; i++) {
087:                    Socket socket = new Socket(host, port);
088:                    PrintWriter pw = new PrintWriter(new OutputStreamWriter(
089:                            socket.getOutputStream()));
090:                    LineNumberReader lnr = new LineNumberReader(
091:                            new InputStreamReader(socket.getInputStream()));
092:
093:                    for (int j = 0; j < packages; j++) {
094:                        pw.println(data);
095:                        pw.flush();
096:
097:                        String result = lnr.readLine();
098:
099:                        if (!data.equals(result)) {
100:                            System.out.println("error!");
101:                        }
102:                    }
103:
104:                    pw.close();
105:                    lnr.close();
106:                    socket.close();
107:                }
108:
109:                long end = System.currentTimeMillis();
110:                return (end - start);
111:            }
112:
113:            static long callBlockingConnection(String host, int port,
114:                    int dataSize, int loops, int packages) throws IOException {
115:                String data = new String(QAUtil.generateByteArray(dataSize));
116:
117:                long start = System.currentTimeMillis();
118:
119:                for (int i = 0; i < loops; i++) {
120:                    IBlockingConnection connection = new BlockingConnection(
121:                            host, port);
122:                    connection.setAutoflush(true);
123:
124:                    for (int j = 0; j < packages; j++) {
125:                        connection.write(data + DELIMITER);
126:                        String result = connection
127:                                .readStringByDelimiter(DELIMITER);
128:
129:                        if (!data.equals(result)) {
130:                            System.out.println("error!");
131:                        }
132:                    }
133:
134:                    connection.close();
135:                }
136:
137:                long end = System.currentTimeMillis();
138:                return (end - start);
139:            }
140:
141:            static long callNonBlockingConnection(String host, int port,
142:                    int dataSize, int loops, int packages) throws IOException {
143:                String data = new String(QAUtil.generateByteArray(dataSize));
144:
145:                long start = System.currentTimeMillis();
146:
147:                for (int i = 0; i < loops; i++) {
148:                    ClientHandler hdl = new ClientHandler();
149:                    INonBlockingConnection connection = new NonBlockingConnection(
150:                            host, port, hdl);
151:                    connection.setAutoflush(true);
152:
153:                    for (int j = 0; j < packages; j++) {
154:                        synchronized (hdl) {
155:                            connection.write(data + DELIMITER);
156:                            try {
157:                                hdl.wait();
158:                            } catch (InterruptedException ignore) {
159:                            }
160:                        }
161:
162:                        if (!data.equals(hdl.result)) {
163:                            System.out.println("error!");
164:                        }
165:                    }
166:
167:                    connection.close();
168:                }
169:
170:                long end = System.currentTimeMillis();
171:                return (end - start);
172:            }
173:
174:            private static final class ClientHandler implements  IDataHandler {
175:
176:                private String result = null;
177:
178:                public boolean onData(INonBlockingConnection connection)
179:                        throws IOException, BufferUnderflowException,
180:                        MaxReadSizeExceededException {
181:                    result = connection.readStringByDelimiter(DELIMITER);
182:
183:                    synchronized (this ) {
184:                        this .notify();
185:                    }
186:
187:                    return true;
188:                }
189:            }
190:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.