JSSE: add -profile option to example ClientJSSE/ServerJSSE/MultiThreadedSSLClient to allow easier analysis with VisualVM

pull/180/head
Chris Conlon 2024-03-26 14:52:55 -06:00
parent 115e93aaa7
commit 66ac903297
3 changed files with 111 additions and 8 deletions

View File

@ -87,6 +87,10 @@ public class ClientJSSE {
boolean putEnabledProtocols = false; /* set enabled protocols */
boolean sendGET = false; /* send HTTP GET */
/* Sleep 10 seconds before and after execution of main example,
* to allow profilers like VisualVM to be attached. */
boolean profileSleep = false;
boolean resumeSession = false; /* try one session resumption */
byte[] firstSessionId = null; /* sess ID of first session */
byte[] resumeSessionId = null; /* sess ID of resumed session */
@ -180,6 +184,9 @@ public class ClientJSSE {
} else if (arg.equals("-r")) {
resumeSession = true;
} else if (arg.equals("-profile")) {
profileSleep = true;
} else {
printUsage();
}
@ -197,6 +204,12 @@ public class ClientJSSE {
return;
}
if (profileSleep) {
System.out.println(
"Sleeping 10 seconds to allow profiler to attach");
Thread.sleep(10000);
}
/* X509TrustManager that trusts all peer certificates. Used if peer
* authentication (-d) has been passed in */
TrustManager[] trustAllCerts = new TrustManager[] {
@ -322,6 +335,25 @@ public class ClientJSSE {
System.out.println("Server message : " + new String(back));
sock.close();
}
if (profileSleep) {
/* Remove provider and set variables to null to help garbage
* collector for profiling */
Security.removeProvider("wolfJSSE");
sock = null;
sf = null;
ctx = null;
km = null;
tm = null;
/* Try and kick start garbage collector before profiling
* heap dump */
System.gc();
System.out.println(
"Sleeping 10 seconds to allow profiler to dump heap");
Thread.sleep(10000);
}
}
private void showPeer(SSLSocket sock) {
@ -385,6 +417,8 @@ public class ClientJSSE {
System.out.println("-A <file>:<password>\tCertificate/key CA JKS file,\tdefault " +
"../provider/ca-server.jks:wolfSSL test");
System.out.println("-r Resume session");
System.out.println("-profile\tSleep for 10 sec before/after running " +
"to allow profilers to attach");
System.exit(1);
}
}

View File

@ -78,6 +78,10 @@ public class MultiThreadedSSLClient
int successClientConnections = 0; /* successful client connections */
int failedClientConnections = 0; /* failed client connections */
/* Sleep 10 seconds before and after execution of main example,
* to allow profilers like VisualVM to be attached. */
boolean profileSleep = false;
long totalConnectionTimeMs = 0; /* total handshake time, across clients */
final Object timeLock = new Object();
@ -156,10 +160,6 @@ public class MultiThreadedSSLClient
String jkspass = "wolfSSL test";
char[] passArr = jkspass.toCharArray();
if (args.length != 2) {
printUsage();
}
/* pull in command line options from user */
for (int i = 0; i < args.length; i++)
{
@ -170,12 +170,22 @@ public class MultiThreadedSSLClient
printUsage();
numClientConnections = Integer.parseInt(args[++i]);
} else if (arg.equals("-profile")) {
profileSleep = true;
} else {
printUsage();
}
}
try {
if (profileSleep) {
System.out.println(
"Sleeping 10 seconds to allow profiler to attach");
Thread.sleep(10000);
}
List<ClientThread> clientList = new ArrayList<ClientThread>();
CountDownLatch latch = new CountDownLatch(numClientConnections);
@ -209,12 +219,22 @@ public class MultiThreadedSSLClient
latch.await();
executor.shutdown();
Security.removeProvider("wolfJSSE");
if (profileSleep) {
/* Try and kick start garbage collector before profiling
* heap dump */
System.gc();
System.out.println(
"Sleeping 10 seconds to allow profiler to dump heap");
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}
Security.removeProvider("wolfJSSE");
System.out.println("================================================");
System.out.println("All Client Connections Finished");
System.out.println("Successful = " + successClientConnections);
@ -234,6 +254,8 @@ public class MultiThreadedSSLClient
private void printUsage() {
System.out.println("Java wolfJSSE example threaded client usage:");
System.out.println("-n <num>\tNumber of client connections");
System.out.println("-profile\tSleep for 10 sec before/after running " +
"to allow profilers to attach");
System.exit(1);
}
}

View File

@ -57,8 +57,12 @@ public class ServerJSSE {
boolean verifyPeer = true; /* verify peer by default */
boolean useEnvVar = false; /* load cert/key from enviornment variable */
boolean listSuites = false; /* list all supported cipher suites */
boolean listEnabledProtocols = false; /* show enabled protocols */
boolean putEnabledProtocols = false; /* set enabled protocols */
boolean listEnabledProtocols = false; /* show enabled protocols */
boolean putEnabledProtocols = false; /* set enabled protocols */
/* Sleep 10 seconds before and after execution of main example,
* to allow profilers like VisualVM to be attached. */
boolean profileSleep = false;
/* cert info */
String serverJKS = "../provider/server.jks";
@ -86,6 +90,12 @@ public class ServerJSSE {
/* load WolfSSLprovider */
Security.addProvider(new WolfSSLProvider());
if (Security.getProvider("wolfJSSE") == null) {
System.out.println("Can't find wolfJSSE provider");
}
else {
System.out.println("Registered wolfJSSE provider");
}
/* pull in command line options from user */
for (int i = 0; i < args.length; i++)
@ -151,6 +161,9 @@ public class ServerJSSE {
protocols = args[++i].split(" ");
sslVersion = -1;
} else if (arg.equals("-profile")) {
profileSleep = true;
} else {
printUsage();
}
@ -181,6 +194,12 @@ public class ServerJSSE {
System.exit(1);
}
if (profileSleep) {
System.out.println(
"Sleeping 10 seconds to allow profiler to attach");
Thread.sleep(10000);
}
/* set up keystore and truststore */
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new FileInputStream(serverJKS),
@ -258,6 +277,32 @@ public class ServerJSSE {
sock.getOutputStream().write(msg.getBytes());
sock.close();
if (profileSleep) {
/* If profiling, only loop once */
sock = null;
break;
}
}
ss.close();
if (profileSleep) {
/* Remove provider and set variables to null to help
* garbage collector for profiling */
Security.removeProvider("wolfJSSE");
ss = null;
ctx = null;
km = null;
tm = null;
/* Try and kick start garbage collector before profiling
* heap dump */
System.gc();
System.out.println(
"Sleeping 10 seconds to allow profiler to dump heap");
Thread.sleep(10000);
}
} catch (Exception e) {
@ -298,6 +343,8 @@ public class ServerJSSE {
"../provider/server.jks:\"wolfSSL test\"");
System.out.println("-A <file>:<password>\tCertificate/key CA JKS file,\tdefault " +
"../provider/ca-client.jks:\"wolfSSL test\"");
System.out.println("-profile\tSleep for 10 sec before/after running " +
"to allow profilers to attach");
System.exit(1);
}