Skip to content

Commit 3699ca6

Browse files
committed
enhanced ServerStressTest.java ( TooTallNate#79 / TooTallNate#75 )
1 parent 56148a3 commit 3699ca6

1 file changed

Lines changed: 78 additions & 20 deletions

File tree

‎example/ServerStressTest.java‎

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.URI;
66
import java.net.URISyntaxException;
77
import java.nio.channels.NotYetConnectedException;
8+
import java.util.Collections;
89
import java.util.LinkedList;
910
import java.util.List;
1011
import java.util.Timer;
@@ -25,19 +26,24 @@
2526
public class ServerStressTest extends JFrame {
2627
private JSlider clients;
2728
private JSlider interval;
29+
private JSlider joinrate;
2830
private JButton start, stop, reset;
31+
private JLabel joinratelabel = new JLabel();
2932
private JLabel clientslabel = new JLabel();
3033
private JLabel intervallabel = new JLabel();
3134
private JTextField uriinput = new JTextField( "ws://localhost:8887" );
3235
private JTextArea text = new JTextArea( "payload" );
3336
private Timer timer = new Timer( true );
37+
private Thread adjustthread;
38+
39+
private int notyetconnected = 0;
3440

3541
public ServerStressTest() {
3642
setTitle( "ServerStressTest" );
3743
setDefaultCloseOperation( EXIT_ON_CLOSE );
3844
start = new JButton( "Start" );
3945
start.addActionListener( new ActionListener() {
40-
46+
4147
@Override
4248
public void actionPerformed( ActionEvent e ) {
4349
start.setEnabled( false );
@@ -46,7 +52,19 @@ public void actionPerformed( ActionEvent e ) {
4652
interval.setEnabled( false );
4753
clients.setEnabled( false );
4854

49-
adjust();
55+
stopAdjust();
56+
adjustthread = new Thread( new Runnable() {
57+
@Override
58+
public void run() {
59+
try {
60+
adjust();
61+
} catch ( InterruptedException e ) {
62+
System.out.println( "adjust chanced" );
63+
}
64+
}
65+
} );
66+
adjustthread.start();
67+
5068
}
5169
} );
5270
stop = new JButton( "Stop" );
@@ -56,9 +74,11 @@ public void actionPerformed( ActionEvent e ) {
5674
@Override
5775
public void actionPerformed( ActionEvent e ) {
5876
timer.cancel();
77+
stopAdjust();
5978
start.setEnabled( true );
6079
stop.setEnabled( false );
6180
reset.setEnabled( true );
81+
joinrate.setEnabled( true );
6282
interval.setEnabled( true );
6383
clients.setEnabled( true );
6484
}
@@ -69,18 +89,26 @@ public void actionPerformed( ActionEvent e ) {
6989

7090
@Override
7191
public void actionPerformed( ActionEvent e ) {
72-
while( !websockets.isEmpty())
92+
while ( !websockets.isEmpty() )
7393
websockets.remove( 0 ).close();
7494

95+
7596
}
7697
} );
77-
clients = new JSlider( 0, 500 );
98+
joinrate = new JSlider( 0, 5000 );
99+
joinrate.addChangeListener( new ChangeListener() {
100+
@Override
101+
public void stateChanged( ChangeEvent e ) {
102+
joinratelabel.setText( "Joinrate: " + joinrate.getValue() + " ms " );
103+
}
104+
} );
105+
clients = new JSlider( 0, 10000 );
78106
clients.addChangeListener( new ChangeListener() {
79-
107+
80108
@Override
81109
public void stateChanged( ChangeEvent e ) {
82110
clientslabel.setText( "Clients: " + clients.getValue() );
83-
111+
84112
}
85113
} );
86114
interval = new JSlider( 0, 5000 );
@@ -94,66 +122,96 @@ public void stateChanged( ChangeEvent e ) {
94122
} );
95123

96124
setSize( 300, 400 );
97-
setLayout( new GridLayout( 8, 1, 10, 10 ) );
125+
setLayout( new GridLayout( 10, 1, 10, 10 ) );
98126
add( new JLabel( "URI" ) );
99127
add( uriinput );
128+
add( joinratelabel );
129+
add( joinrate );
100130
add( clientslabel );
101131
add( clients );
102132
add( intervallabel );
103133
add( interval );
104134
JPanel south = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
105135
add( text );
106136
add( south );
107-
137+
108138
south.add( start );
109139
south.add( stop );
110140
south.add( reset );
111141

142+
joinrate.setValue( 200 );
112143
interval.setValue( 1000 );
113144
clients.setValue( 1 );
114145

115146
}
116147

117-
List<WebSocketClient> websockets = new LinkedList<WebSocketClient>();
148+
List<WebSocketClient> websockets = Collections.synchronizedList( new LinkedList<WebSocketClient>() );
118149
URI uri;
119-
public void adjust() {
150+
public void adjust() throws InterruptedException {
120151
System.out.println( "Adjust" );
121152
try {
122153
uri = new URI( uriinput.getText() );
123154
} catch ( URISyntaxException e ) {
124155
e.printStackTrace();
125156
}
126-
while ( websockets.size() < clients.getValue() ) {
127-
WebSocketClient cl = new EmptyClient( uri );
157+
int totalclients = clients.getValue();
158+
while ( websockets.size() < totalclients ) {
159+
WebSocketClient cl = new EmptyClient( uri ) {
160+
@Override
161+
public void onClose( int code, String reason, boolean remote ) {
162+
System.out.println( "Closed duo " + code + " " + reason );
163+
websockets.remove( this );
164+
}
165+
};
166+
128167
cl.connect();
168+
clients.setValue( websockets.size() );
129169
websockets.add( cl );
170+
Thread.sleep( joinrate.getValue() );
130171
}
131172
while ( websockets.size() > clients.getValue() ) {
132173
websockets.remove( 0 ).close();
133174
}
134175
timer = new Timer( true );
135176
timer.scheduleAtFixedRate( new TimerTask() {
136-
177+
137178
@Override
138179
public void run() {
139-
System.out.println( "send " + System.currentTimeMillis() );
140180
send();
141-
181+
142182
}
143183
}, 0, interval.getValue() );
144184

145185
}
146-
public void send() {
147-
for( WebSocketClient cl : websockets ) {
186+
187+
public void stopAdjust() {
188+
if( adjustthread != null ) {
189+
adjustthread.interrupt();
148190
try {
149-
cl.send( text.getText() );
150-
} catch ( NotYetConnectedException e ) {
151-
e.printStackTrace();
191+
adjustthread.join();
152192
} catch ( InterruptedException e ) {
153193
e.printStackTrace();
154194
}
155195
}
156196
}
197+
public void send() {
198+
notyetconnected = 0;
199+
String payload = text.getText();
200+
long time1 = System.currentTimeMillis();
201+
synchronized ( websockets ) {
202+
for( WebSocketClient cl : websockets ) {
203+
try {
204+
cl.send( payload );
205+
cl.flush();
206+
} catch ( NotYetConnectedException e )
207+
notyetconnected++;
208+
} catch ( InterruptedException e ) {
209+
e.printStackTrace();
210+
}
211+
}
212+
}
213+
System.out.println( websockets.size() + "/" + notyetconnected + " clients sent \"" + payload + "\"" + ( System.currentTimeMillis() - time1 ) );
214+
}
157215
/**
158216
* @param args
159217
*/

0 commit comments

Comments
 (0)