Slow Connection in Mongo SSL/TLS using Java Driver

Hi, apparently we notice there is a significant slowness in connecting to MongoDB SSL/TLS. We have tested in both SSL in version 4.0.16 and TLS in version 4.2.7 using mongodb-driver-sync version 4.0.4 (or version 3.9.x above). It takes around 9 seconds.

The issue does not seem to appear when we are using mongodb-driver version 3.8.2. It only takes around 400 milliseconds. We are using docker-based MongoDB.

This is our sample testing code. Your help would be appreciated. Thanks!

@RunWith(JUnit4.class)
public class TestWithoutSpring {

    private SSLContext sslContext;

    @Before
    public void before() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chain, authType) -> true)
                .build();
    }

    @Test
    public void testMongoClientNew() {
        ConnectionString connectionString = new ConnectionString("mongodb://192.168.4.9:27017/mydb?ssl=true");
        MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connectionString)
                .applyToSslSettings(builder -> builder.context(sslContext).invalidHostNameAllowed(true))
                .build();
        com.mongodb.client.MongoClient client = MongoClients.create(settings);
        String database = connectionString.getDatabase();
        Assert.notNull(database);
        long start = System.currentTimeMillis();
        client.getDatabase(database).runCommand(new Document("ping", 1));
        long end = System.currentTimeMillis();
        System.err.println(end - start);
        client.close();
    }
}