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
Why we started using JSON Schema in our internal Helm charts
Helm 3 supports validating the provided chart values against a JSON Schema. While it may be quicker to get started in your chart development without a schema, we found it valuable for a number of […]
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]