Skip to content

Commit

Permalink
OF-2611: Optimize S2S unit test for CPU usage
Browse files Browse the repository at this point in the history
Generating certificates is expensive. For performance, it's best to generate each set once, and then reuse those during the execution of the tests.

This removes about 70% of the CPU usage during test execution. Locally, the duration of test execution dropped to about 60% of the original duration.
  • Loading branch information
guusdk committed Jul 12, 2023
1 parent 3f931b7 commit d080633
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,31 @@
public class AbstractRemoteServerDummy
{
public static final String XMPP_DOMAIN = "remote-dummy.example.org";

private final static KeystoreTestUtils.ResultHolder SELF_SIGNED_CERTIFICATE;
private final static KeystoreTestUtils.ResultHolder EXPIRED_SELF_SIGNED_CERTIFICATE;
private final static KeystoreTestUtils.ResultHolder VALID_CERTIFICATE_CHAIN;
private final static KeystoreTestUtils.ResultHolder EXPIRED_CERTIFICATE_CHAIN;

public static final Duration SO_TIMEOUT = Duration.ofMillis(50);
protected boolean useExpiredEndEntityCertificate;
protected boolean useSelfSignedCertificate;
protected boolean disableDialback;
protected Connection.TLSPolicy encryptionPolicy = Connection.TLSPolicy.optional;
protected KeystoreTestUtils.ResultHolder generatedPKIX;

static {
// Generating certificates is expensive. For performance, it's best to generate each set once, and then reuse those during the execution of the tests.
try {
SELF_SIGNED_CERTIFICATE = KeystoreTestUtils.generateSelfSignedCertificate(XMPP_DOMAIN);
EXPIRED_SELF_SIGNED_CERTIFICATE = KeystoreTestUtils.generateExpiredSelfSignedCertificate(XMPP_DOMAIN);
VALID_CERTIFICATE_CHAIN = KeystoreTestUtils.generateValidCertificateChain(XMPP_DOMAIN);
EXPIRED_CERTIFICATE_CHAIN = KeystoreTestUtils.generateCertificateChainWithExpiredEndEntityCert(XMPP_DOMAIN);
} catch (Throwable t) {
throw new IllegalStateException("Unable to setup certificates used by the test implementation.", t);
}
}

/**
* Updates the TLS encryption policy that's observed by this server.
*/
Expand Down Expand Up @@ -91,11 +109,10 @@ public void preparePKIX() throws Exception
throw new IllegalStateException("PKIX already prepared.");
}

final String commonName = XMPP_DOMAIN;
if (useSelfSignedCertificate) {
generatedPKIX = useExpiredEndEntityCertificate ? KeystoreTestUtils.generateExpiredSelfSignedCertificate(commonName) : KeystoreTestUtils.generateSelfSignedCertificate(commonName);
generatedPKIX = useExpiredEndEntityCertificate ? EXPIRED_SELF_SIGNED_CERTIFICATE : SELF_SIGNED_CERTIFICATE;
} else {
generatedPKIX = useExpiredEndEntityCertificate ? KeystoreTestUtils.generateCertificateChainWithExpiredEndEntityCert(commonName) : KeystoreTestUtils.generateValidCertificateChain(commonName);
generatedPKIX = useExpiredEndEntityCertificate ? EXPIRED_CERTIFICATE_CHAIN : VALID_CERTIFICATE_CHAIN;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.util.JiveGlobals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down

0 comments on commit d080633

Please sign in to comment.