Source Code Cross Referenced for LoadClient.java in  » Web-Server » xsocket » org » xsocket » stream » 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.stream 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.xsocket.stream;
002:
003:        import java.io.IOException;
004:        import java.io.InputStreamReader;
005:        import java.io.LineNumberReader;
006:        import java.io.OutputStreamWriter;
007:        import java.io.PrintWriter;
008:        import java.net.Socket;
009:        import java.util.ArrayList;
010:        import java.util.List;
011:
012:        import org.xsocket.DataConverter;
013:
014:        public class LoadClient {
015:
016:            /*
017:             * http://www.minq.se/products/pureload/tuning/connections.html
018:             * 
019:             * win xp
020:             * http://support.microsoft.com/kb/314053/de
021:             * http://support.microsoft.com/kb/196271/de
022:             */
023:
024:            private String host = null;
025:            private int port = 0;
026:            private int countWorkers = 0;
027:            private int countConnections = 0;
028:            private int loops = 0;
029:            private boolean openNew = false;
030:            private int dataSize = 0;
031:            private int waitBetweenCalls = 0;
032:
033:            private Statistic statistic = null;
034:
035:            public static void main(String... args) throws Exception {
036:                if (args.length != 8) {
037:                    System.out
038:                            .println("usage org.xsocket.stream.LoadClient <host> <port> <workers> <cons> <openNew?> <loops> <dataSize> <waitTime>");
039:                    System.exit(-1);
040:                }
041:
042:                LoadClient loadClient = new LoadClient();
043:                loadClient.host = args[0];
044:                loadClient.port = Integer.parseInt(args[1]);
045:                loadClient.countWorkers = Integer.parseInt(args[2]);
046:                loadClient.countConnections = Integer.parseInt(args[3]);
047:                loadClient.openNew = Boolean.parseBoolean(args[4]);
048:                loadClient.loops = Integer.parseInt(args[5]);
049:                loadClient.dataSize = Integer.parseInt(args[6]);
050:                loadClient.waitBetweenCalls = Integer.parseInt(args[7]);
051:
052:                loadClient.testLoad();
053:            }
054:
055:            public void testLoad() throws Exception {
056:
057:                System.out.println("running test client with (server=" + host
058:                        + ":" + port + "; loops=" + loops + ")");
059:
060:                System.out
061:                        .println("\r\n---------------------------------------");
062:                System.out.println("Load-info:");
063:                System.out.println("data packet size                 "
064:                        + dataSize + " bytes");
065:                System.out.println("concurrent client threads        "
066:                        + countWorkers);
067:                System.out.println("concurrent connections           "
068:                        + countConnections * countWorkers);
069:                System.out.println("new connection for each request? "
070:                        + openNew);
071:                System.out.println("wait time between client calls   "
072:                        + DataConverter.toFormatedDuration(waitBetweenCalls));
073:                System.out
074:                        .println("---------------------------------------\r\n");
075:
076:                List<Worker> workers = new ArrayList<Worker>();
077:
078:                System.out.println("preparing workers (init connctions)...");
079:                for (int i = 0; i < countWorkers; i++) {
080:                    Worker worker = new Worker(host, port, countConnections,
081:                            openNew, loops, dataSize, waitBetweenCalls);
082:                    workers.add(worker);
083:                    worker.prepare();
084:                }
085:
086:                statistic = new Statistic();
087:
088:                System.out.println("run workers");
089:                for (Worker worker : workers) {
090:                    Thread t = new Thread(worker);
091:                    t.start();
092:                }
093:
094:                boolean allFinished;
095:                do {
096:                    allFinished = true;
097:                    TestUtil.sleep(1000);
098:
099:                    for (Worker worker : workers) {
100:                        allFinished = allFinished & worker.isRunning();
101:                    }
102:
103:                } while (!allFinished);
104:
105:            }
106:
107:            Socket createClientSocket(String host, int port) throws IOException {
108:                return new Socket(host, port);
109:            }
110:
111:            private final class Worker implements  Runnable {
112:
113:                private boolean isRunning = true;
114:
115:                private String host = null;
116:                private int port = 0;
117:                private int countConnections = 0;
118:                private boolean openNew = false;
119:                private int loops = 0;
120:                private int dataSize = 0;
121:                private int waitBetweenCalls = 0;
122:
123:                private ArrayList<ClientConnection> connections = new ArrayList<ClientConnection>();
124:
125:                Worker(String host, int port, int countConnections,
126:                        boolean openNew, int loops, int dataSize,
127:                        int waitBetweenCalls) throws IOException {
128:                    this .host = host;
129:                    this .port = port;
130:                    this .countConnections = countConnections;
131:                    this .openNew = openNew;
132:                    this .loops = loops;
133:                    this .dataSize = dataSize;
134:                    this .waitBetweenCalls = waitBetweenCalls;
135:                }
136:
137:                public void prepare() {
138:                    for (int i = 0; i < countConnections; i++) {
139:                        newConnection();
140:                    }
141:                }
142:
143:                @SuppressWarnings("unchecked")
144:                public void run() {
145:
146:                    String data = new String(TestUtil
147:                            .generatedByteArray(dataSize));
148:
149:                    for (int i = 0; i < loops; i++) {
150:                        ArrayList<ClientConnection> copy = (ArrayList<ClientConnection>) connections
151:                                .clone();
152:                        for (ClientConnection connection : copy) {
153:                            try {
154:                                String request = data + i;
155:
156:                                String response = null;
157:                                long elapsed = 0;
158:
159:                                if (openNew) {
160:                                    long start = System.currentTimeMillis();
161:                                    ClientConnection con = new ClientConnection(
162:                                            host, port);
163:                                    response = con.send(request);
164:                                    elapsed = System.currentTimeMillis()
165:                                            - start;
166:                                    con.close();
167:
168:                                } else {
169:                                    long start = System.currentTimeMillis();
170:                                    response = connection.send(request);
171:                                    elapsed = System.currentTimeMillis()
172:                                            - start;
173:                                }
174:
175:                                if (response == null) {
176:                                    throw new Exception("null response");
177:                                }
178:
179:                                if (request.equals(response)) {
180:                                    statistic.addValue((int) elapsed);
181:                                } else {
182:                                    throw new Exception("wrong response: "
183:                                            + response);
184:                                }
185:
186:                            } catch (Exception e) {
187:                                System.out.println("error " + e.toString()
188:                                        + ";");
189:                                connection.close();
190:                                connections.remove(connection);
191:                                newConnection();
192:                            }
193:
194:                            if (waitBetweenCalls > 0) {
195:                                TestUtil.sleep(waitBetweenCalls);
196:                            }
197:                        }
198:                    }
199:
200:                    isRunning = false;
201:                }
202:
203:                public boolean isRunning() {
204:                    return isRunning;
205:                }
206:
207:                private ClientConnection newConnection() {
208:                    ClientConnection connection = null;
209:                    do {
210:                        try {
211:                            connection = new ClientConnection(host, port);
212:                        } catch (Exception ex) {
213:                            try {
214:                                Thread.sleep(100);
215:                            } catch (InterruptedException ignore) {
216:                            }
217:                        }
218:                    } while (connection == null);
219:
220:                    connections.add(connection);
221:                    return connection;
222:                }
223:            }
224:
225:            private static final class ClientConnection {
226:                private Socket socket = null;
227:                private LineNumberReader lnr = null;
228:                private PrintWriter pw = null;
229:
230:                ClientConnection(String host, int port) throws IOException {
231:                    socket = new Socket(host, port);
232:                    socket.setReuseAddress(true);
233:                    lnr = new LineNumberReader(new InputStreamReader(socket
234:                            .getInputStream()));
235:                    pw = new PrintWriter(new OutputStreamWriter(socket
236:                            .getOutputStream()));
237:                }
238:
239:                public String send(String data) throws IOException {
240:                    pw.println(data);
241:                    pw.flush();
242:                    return lnr.readLine();
243:                }
244:
245:                public void close() {
246:                    try {
247:                        lnr.close();
248:                        pw.close();
249:                        socket.close();
250:                    } catch (Exception ignore) {
251:                    }
252:                }
253:            }
254:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.