Benchmarking YSQL Connection Time in YugabyteDB with the PostgreSQL JDBC Driver

In a previous post, we explored how to benchmark connection times with the YugabyteDB JDBC Smart Driver. The Smart Driver provides features like load balancing and retries across tablet servers, which makes it a great choice for production workloads.

But sometimes you don’t need the Smart Driver. For example, if you just want to measure the raw connection time to a single YSQL endpoint, the standard PostgreSQL JDBC driver works perfectly fine. In this post, we’ll walk through a simple Maven-based Java program (yb-pgjdbc-demo) that does exactly that.

Why Measure Connection Time?

Database connection establishment isn’t free. In distributed databases like YugabyteDB, there’s often a TLS handshake, authentication, and a bit of metadata exchange before the connection is ready. Measuring this helps:

  • • Compare environments (local vs. cloud, different regions, etc.)

  • • Baseline connection latency

  • • Spot anomalies if connection time suddenly spikes

Setting Up the Demo

We’ll create a fresh Maven project and add the PostgreSQL JDBC dependency.

pom.xml
				
					<dependencies>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.7</version>
  </dependency>
</dependencies>

				
			

This pulls in the latest PostgreSQL JDBC driver, which works seamlessly with YugabyteDB’s YSQL API.

The Program

The Java program is dead simple:

  • • Accepts a JDBC URL as a command-line argument (including username, password, options).

  • • Records a timestamp before and after the connection.

  • • Prints the elapsed time in milliseconds.

Main.java
				
					package com.example.ybpg;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Main <JDBC_URL>");
            System.exit(1);
        }

        String jdbcUrl = args[0];
        long startTime = System.nanoTime();

        try (Connection conn = DriverManager.getConnection(jdbcUrl)) {
            long elapsedNs = System.nanoTime() - startTime;
            System.out.printf("Connection established in %.3f ms%n", elapsedNs / 1e6);
        } catch (SQLException e) {
            long elapsedNs = System.nanoTime() - startTime;
            System.err.printf("Connection failed after %.3f ms – error: %s%n",
                    elapsedNs / 1e6, e.getMessage());
        }
    }
}
				
			
Running the Test

Compile the project:

				
					mvn -q clean package
				
			

Then run with your JDBC URL:

				
					java -cp target/yb-pgjdbc-demo-1.0.0.jar:~/.m2/repository/org/postgresql/postgresql/42.7.7/postgresql-42.7.7.jar \
  com.example.ybpg.Main \
  "jdbc:postgresql://127.0.0.1:5433/yugabyte?user=yugabyte&password=yugabyte&connectTimeout=10"
				
			

Example output:

				
					Connection established in 42.187 ms
				
			
Comparing to the Smart Driver

Unlike the YugabyteDB Smart Driver:

  • • No retries, load balancing, or multiple-attempt timing.

  • • Just a straight measurement of how long it takes to connect to a given node.

That simplicity makes it ideal if you just want a baseline measurement.

Have Fun!

If you look closely, the baby blue spruce, in our backyard, is holding its own babies... a nest full of the tiniest little birds. 🐦💙