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); } }