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
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]
Creating Mocks For Unit Testing in Go
Unit testing is an important part of any project, and Go built its framework with a testing package; making unit testing part of the language. This testing framework is good for most scenarios, but you […]