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. Required fields are marked *

Related Blog Posts
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]