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
Google Professional Machine Learning Engineer Exam 2021
Exam Description A Professional Machine Learning Engineer designs, builds, and productionizes ML models to solve business challenges using Google Cloud technologies and knowledge of proven ML models and techniques. The ML Engineer is proficient in all aspects […]
Designing Kubernetes Controllers
There has been some excellent online discussion lately around Kubernetes controllers, highlighted by an excellent Speakerdeck presentation assembled by Tim Hockin. What I’d like to do in this post is explore some of the implications […]
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Jolt custom java transform
Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just […]