Skip to content

Commit 66ac903

Browse files
committed
JSSE: add -profile option to example ClientJSSE/ServerJSSE/MultiThreadedSSLClient to allow easier analysis with VisualVM
1 parent 115e93a commit 66ac903

3 files changed

Lines changed: 111 additions & 8 deletions

File tree

examples/provider/ClientJSSE.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public void run(String[] args) throws Exception {
8787
boolean putEnabledProtocols = false; /* set enabled protocols */
8888
boolean sendGET = false; /* send HTTP GET */
8989

90+
/* Sleep 10 seconds before and after execution of main example,
91+
* to allow profilers like VisualVM to be attached. */
92+
boolean profileSleep = false;
93+
9094
boolean resumeSession = false; /* try one session resumption */
9195
byte[] firstSessionId = null; /* sess ID of first session */
9296
byte[] resumeSessionId = null; /* sess ID of resumed session */
@@ -180,6 +184,9 @@ public void run(String[] args) throws Exception {
180184
} else if (arg.equals("-r")) {
181185
resumeSession = true;
182186

187+
} else if (arg.equals("-profile")) {
188+
profileSleep = true;
189+
183190
} else {
184191
printUsage();
185192
}
@@ -197,6 +204,12 @@ public void run(String[] args) throws Exception {
197204
return;
198205
}
199206

207+
if (profileSleep) {
208+
System.out.println(
209+
"Sleeping 10 seconds to allow profiler to attach");
210+
Thread.sleep(10000);
211+
}
212+
200213
/* X509TrustManager that trusts all peer certificates. Used if peer
201214
* authentication (-d) has been passed in */
202215
TrustManager[] trustAllCerts = new TrustManager[] {
@@ -322,6 +335,25 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
322335
System.out.println("Server message : " + new String(back));
323336
sock.close();
324337
}
338+
339+
if (profileSleep) {
340+
/* Remove provider and set variables to null to help garbage
341+
* collector for profiling */
342+
Security.removeProvider("wolfJSSE");
343+
sock = null;
344+
sf = null;
345+
ctx = null;
346+
km = null;
347+
tm = null;
348+
349+
/* Try and kick start garbage collector before profiling
350+
* heap dump */
351+
System.gc();
352+
353+
System.out.println(
354+
"Sleeping 10 seconds to allow profiler to dump heap");
355+
Thread.sleep(10000);
356+
}
325357
}
326358

327359
private void showPeer(SSLSocket sock) {
@@ -385,6 +417,8 @@ private void printUsage() {
385417
System.out.println("-A <file>:<password>\tCertificate/key CA JKS file,\tdefault " +
386418
"../provider/ca-server.jks:wolfSSL test");
387419
System.out.println("-r Resume session");
420+
System.out.println("-profile\tSleep for 10 sec before/after running " +
421+
"to allow profilers to attach");
388422
System.exit(1);
389423
}
390424
}

examples/provider/MultiThreadedSSLClient.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class MultiThreadedSSLClient
7878
int successClientConnections = 0; /* successful client connections */
7979
int failedClientConnections = 0; /* failed client connections */
8080

81+
/* Sleep 10 seconds before and after execution of main example,
82+
* to allow profilers like VisualVM to be attached. */
83+
boolean profileSleep = false;
84+
8185
long totalConnectionTimeMs = 0; /* total handshake time, across clients */
8286
final Object timeLock = new Object();
8387

@@ -156,10 +160,6 @@ public MultiThreadedSSLClient(String[] args) {
156160
String jkspass = "wolfSSL test";
157161
char[] passArr = jkspass.toCharArray();
158162

159-
if (args.length != 2) {
160-
printUsage();
161-
}
162-
163163
/* pull in command line options from user */
164164
for (int i = 0; i < args.length; i++)
165165
{
@@ -170,12 +170,22 @@ public MultiThreadedSSLClient(String[] args) {
170170
printUsage();
171171
numClientConnections = Integer.parseInt(args[++i]);
172172

173+
} else if (arg.equals("-profile")) {
174+
profileSleep = true;
175+
173176
} else {
174177
printUsage();
175178
}
176179
}
177180

178181
try {
182+
183+
if (profileSleep) {
184+
System.out.println(
185+
"Sleeping 10 seconds to allow profiler to attach");
186+
Thread.sleep(10000);
187+
}
188+
179189
List<ClientThread> clientList = new ArrayList<ClientThread>();
180190
CountDownLatch latch = new CountDownLatch(numClientConnections);
181191

@@ -209,12 +219,22 @@ public MultiThreadedSSLClient(String[] args) {
209219
latch.await();
210220
executor.shutdown();
211221

222+
Security.removeProvider("wolfJSSE");
223+
224+
if (profileSleep) {
225+
/* Try and kick start garbage collector before profiling
226+
* heap dump */
227+
System.gc();
228+
229+
System.out.println(
230+
"Sleeping 10 seconds to allow profiler to dump heap");
231+
Thread.sleep(10000);
232+
}
233+
212234
} catch (Exception e) {
213235
e.printStackTrace();
214236
}
215237

216-
Security.removeProvider("wolfJSSE");
217-
218238
System.out.println("================================================");
219239
System.out.println("All Client Connections Finished");
220240
System.out.println("Successful = " + successClientConnections);
@@ -234,6 +254,8 @@ public static void main(String[] args) {
234254
private void printUsage() {
235255
System.out.println("Java wolfJSSE example threaded client usage:");
236256
System.out.println("-n <num>\tNumber of client connections");
257+
System.out.println("-profile\tSleep for 10 sec before/after running " +
258+
"to allow profilers to attach");
237259
System.exit(1);
238260
}
239261
}

examples/provider/ServerJSSE.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ public void run(String[] args) {
5757
boolean verifyPeer = true; /* verify peer by default */
5858
boolean useEnvVar = false; /* load cert/key from enviornment variable */
5959
boolean listSuites = false; /* list all supported cipher suites */
60-
boolean listEnabledProtocols = false; /* show enabled protocols */
61-
boolean putEnabledProtocols = false; /* set enabled protocols */
60+
boolean listEnabledProtocols = false; /* show enabled protocols */
61+
boolean putEnabledProtocols = false; /* set enabled protocols */
62+
63+
/* Sleep 10 seconds before and after execution of main example,
64+
* to allow profilers like VisualVM to be attached. */
65+
boolean profileSleep = false;
6266

6367
/* cert info */
6468
String serverJKS = "../provider/server.jks";
@@ -86,6 +90,12 @@ public void run(String[] args) {
8690

8791
/* load WolfSSLprovider */
8892
Security.addProvider(new WolfSSLProvider());
93+
if (Security.getProvider("wolfJSSE") == null) {
94+
System.out.println("Can't find wolfJSSE provider");
95+
}
96+
else {
97+
System.out.println("Registered wolfJSSE provider");
98+
}
8999

90100
/* pull in command line options from user */
91101
for (int i = 0; i < args.length; i++)
@@ -151,6 +161,9 @@ public void run(String[] args) {
151161
protocols = args[++i].split(" ");
152162
sslVersion = -1;
153163

164+
} else if (arg.equals("-profile")) {
165+
profileSleep = true;
166+
154167
} else {
155168
printUsage();
156169
}
@@ -181,6 +194,12 @@ public void run(String[] args) {
181194
System.exit(1);
182195
}
183196

197+
if (profileSleep) {
198+
System.out.println(
199+
"Sleeping 10 seconds to allow profiler to attach");
200+
Thread.sleep(10000);
201+
}
202+
184203
/* set up keystore and truststore */
185204
KeyStore keystore = KeyStore.getInstance("JKS");
186205
keystore.load(new FileInputStream(serverJKS),
@@ -258,6 +277,32 @@ public void run(String[] args) {
258277
sock.getOutputStream().write(msg.getBytes());
259278

260279
sock.close();
280+
281+
if (profileSleep) {
282+
/* If profiling, only loop once */
283+
sock = null;
284+
break;
285+
}
286+
}
287+
288+
ss.close();
289+
290+
if (profileSleep) {
291+
/* Remove provider and set variables to null to help
292+
* garbage collector for profiling */
293+
Security.removeProvider("wolfJSSE");
294+
ss = null;
295+
ctx = null;
296+
km = null;
297+
tm = null;
298+
299+
/* Try and kick start garbage collector before profiling
300+
* heap dump */
301+
System.gc();
302+
303+
System.out.println(
304+
"Sleeping 10 seconds to allow profiler to dump heap");
305+
Thread.sleep(10000);
261306
}
262307

263308
} catch (Exception e) {
@@ -298,6 +343,8 @@ private void printUsage() {
298343
"../provider/server.jks:\"wolfSSL test\"");
299344
System.out.println("-A <file>:<password>\tCertificate/key CA JKS file,\tdefault " +
300345
"../provider/ca-client.jks:\"wolfSSL test\"");
346+
System.out.println("-profile\tSleep for 10 sec before/after running " +
347+
"to allow profilers to attach");
301348
System.exit(1);
302349
}
303350

0 commit comments

Comments
 (0)