Nov 24, 2020

Performance Test Liquibase Update

When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems.

If you need to measure the time to apply each sql statement we can use mockito to spy liquibase’s JDBCExecutor and print out each SQL and it’s time to complete.

One gotcha is that you have to place your test in the liquibase.executor.jvm package so that you can have access to the StatementCallback interface.

package liquibase.executor.jvm;
 
import liquibase.Liquibase;
import liquibase.change.Change;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.statement.SqlStatement;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
import static org.mockito.ArgumentMatchers.any;
 
public class LiquibaseSqlPerformanceTest {
 
    static final String TEST_CONTEXT = null;
 
    @Test
    public void test() throws Exception {
        //Setup database
        Class.forName("org.h2.Driver");
        Connection connection = DriverManager.getConnection("jdbc:h2/~test", "", "");
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
 
        Liquibase liquibase = new Liquibase("",  new ClassLoaderResourceAccessor(), database);
 
 
        JdbcExecutor executor = Mockito.spy(JdbcExecutor.class);
 
        Answer answer = invocation -> {
 
            long startTime = System.currentTimeMillis();
 
            Object returnValue = invocation.callRealMethod();
 
            long endTime = System.currentTimeMillis();
 
            System.out.println(invocation.getArgument(0).toString());
            System.out.println(endTime - startTime + "ms");
 
            return returnValue;
        };
 
        Mockito.doAnswer(answer).when(executor).execute((Change) any(), any());
        Mockito.doAnswer(answer).when(executor).execute((SqlStatement) any(), any());
        Mockito.doAnswer(answer).when(executor).execute((StatementCallback) any(), any());
 
 
        liquibase.update(TEST_CONTEXT);
 
    }
}

About the Author

Scott Bock profile.

Scott Bock

Principal Technologist

Scott is a Senior Software Engineer with over 12 years of experience using Java, and 5 years experience in technical leadership positions. His strengths include troubleshooting and problem solving abilities, excellent repertoire with customers and management, and verbal and written communication. He develops code across the entire technology stack including database, application, and user interface.

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]