Created
February 18, 2021 08:44
-
-
Save lukaseder/6e8f86b5cf91fcbdf561419e1af748ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jooq.testscripts; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.Statement; | |
import org.jooq.SQLDialect; | |
import org.jooq.test.setup.DatabaseSetup; | |
import org.jooq.test.setup.DatabaseSetup.Config; | |
import org.jooq.tools.StopWatch; | |
public class JDBCSpeedTest { | |
public static void main(String[] args) throws Exception { | |
SQLDialect dialect = SQLDialect.BIGQUERY; | |
DatabaseSetup dbSetup = new DatabaseSetup(new Config(dialect, "", false)); | |
try (Connection c = dbSetup.getConnection()) { | |
StopWatch watch; | |
watch = new StopWatch(); | |
int repetitions = 30; | |
for (int i = 0; i < repetitions; i++) { | |
try (PreparedStatement s = c.prepareStatement("insert into t (i) values (?)")) { | |
s.setInt(1, i); | |
s.executeUpdate(); | |
} | |
progress(watch, i); | |
} | |
watch.splitInfo("Finished prepared statements"); | |
watch = new StopWatch(); | |
try (PreparedStatement s = c.prepareStatement("insert into t (i) values (?)")) { | |
for (int i = 0; i < repetitions; i++) { | |
s.setInt(1, i); | |
s.executeUpdate(); | |
progress(watch, i); | |
} | |
} | |
watch.splitInfo("Finished prepared statements keeping them open"); | |
watch = new StopWatch(); | |
try (PreparedStatement s = c.prepareStatement("insert into t (i) values (?)")) { | |
for (int i = 0; i < repetitions; i++) { | |
s.setInt(1, i); | |
s.addBatch(); | |
progress(watch, i); | |
} | |
s.executeBatch(); | |
} | |
watch.splitInfo("Finished prepared statement batch"); | |
watch = new StopWatch(); | |
try (Statement s = c.createStatement()) { | |
for (int i = 0; i < repetitions; i++) { | |
s.executeUpdate("insert into t (i) values (" + i + ")"); | |
progress(watch, i); | |
} | |
} | |
watch.splitInfo("Finished static statements"); | |
watch = new StopWatch(); | |
try (Statement s = c.createStatement()) { | |
for (int i = 0; i < repetitions; i++) { | |
s.addBatch("insert into t (i) values (" + i + ")"); | |
progress(watch, i); | |
} | |
s.executeBatch(); | |
} | |
watch.splitInfo("Finished static statement batch"); | |
} | |
} | |
private static void progress(StopWatch watch, int i) { | |
if (i % 5 == 0) | |
watch.splitInfo("Insertions: " + i); | |
} | |
} | |
/* | |
09:33:18,214 INFO [org.jooq.tools.StopWatch ] - Insertions: 0 : Total: 1.206s | |
09:33:40,595 INFO [org.jooq.tools.StopWatch ] - Insertions: 5 : Total: 23.586s, +22.38s | |
09:34:09,560 INFO [org.jooq.tools.StopWatch ] - Insertions: 10 : Total: 52.552s, +28.965s | |
09:34:49,082 INFO [org.jooq.tools.StopWatch ] - Insertions: 15 : Total: 01:32, +39.521s | |
09:35:23,959 INFO [org.jooq.tools.StopWatch ] - Insertions: 20 : Total: 02:06, +34.877s | |
09:35:30,419 INFO [org.jooq.tools.StopWatch ] - Insertions: 25 : Total: 02:13, +6.46s | |
09:35:57,834 INFO [org.jooq.tools.StopWatch ] - Finished prepared statements: Total: 02:40, +27.415s | |
09:35:58,984 INFO [org.jooq.tools.StopWatch ] - Insertions: 0 : Total: 1.149s | |
09:36:38,774 INFO [org.jooq.tools.StopWatch ] - Insertions: 5 : Total: 40.939s, +39.789s | |
09:36:43,959 INFO [org.jooq.tools.StopWatch ] - Insertions: 10 : Total: 46.123s, +5.184s | |
09:37:18,241 INFO [org.jooq.tools.StopWatch ] - Insertions: 15 : Total: 01:20, +34.282s | |
09:37:23,789 INFO [org.jooq.tools.StopWatch ] - Insertions: 20 : Total: 01:25, +5.548s | |
09:37:49,033 INFO [org.jooq.tools.StopWatch ] - Insertions: 25 : Total: 01:51, +25.244s | |
09:37:53,471 INFO [org.jooq.tools.StopWatch ] - Finished prepared statements keeping them open: Total: 01:55, +4.438s | |
09:37:53,633 INFO [org.jooq.tools.StopWatch ] - Insertions: 0 : Total: 161.281ms | |
09:37:53,633 INFO [org.jooq.tools.StopWatch ] - Insertions: 5 : Total: 161.41ms, +0.129ms | |
09:37:53,634 INFO [org.jooq.tools.StopWatch ] - Insertions: 10 : Total: 161.476ms, +0.065ms | |
09:37:53,634 INFO [org.jooq.tools.StopWatch ] - Insertions: 15 : Total: 161.547ms, +0.07ms | |
09:37:53,634 INFO [org.jooq.tools.StopWatch ] - Insertions: 20 : Total: 161.675ms, +0.128ms | |
09:37:53,634 INFO [org.jooq.tools.StopWatch ] - Insertions: 25 : Total: 161.748ms, +0.073ms | |
09:40:20,556 INFO [org.jooq.tools.StopWatch ] - Finished prepared statement batch: Total: 02:27, +02:26 | |
09:40:21,518 INFO [org.jooq.tools.StopWatch ] - Insertions: 0 : Total: 962.172ms | |
09:40:28,501 INFO [org.jooq.tools.StopWatch ] - Insertions: 5 : Total: 7.944s, +6.982s | |
09:40:34,824 INFO [org.jooq.tools.StopWatch ] - Insertions: 10 : Total: 14.267s, +6.323s | |
09:40:41,208 INFO [org.jooq.tools.StopWatch ] - Insertions: 15 : Total: 20.652s, +6.384s | |
09:40:46,789 INFO [org.jooq.tools.StopWatch ] - Insertions: 20 : Total: 26.233s, +5.581s | |
09:40:52,153 INFO [org.jooq.tools.StopWatch ] - Insertions: 25 : Total: 31.596s, +5.363s | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Finished static statements: Total: 35.644s, +4.047s | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 0 : Total: 0.061ms | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 5 : Total: 0.142ms, +0.081ms | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 10 : Total: 0.189ms, +0.046ms | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 15 : Total: 0.239ms, +0.049ms | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 20 : Total: 0.283ms, +0.044ms | |
09:40:56,200 INFO [org.jooq.tools.StopWatch ] - Insertions: 25 : Total: 0.331ms, +0.047ms | |
09:43:13,014 INFO [org.jooq.tools.StopWatch ] - Finished static statement batch: Total: 02:16, +02:16 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another run, and proof that any experienced effect is merely due to random outliers, not systematic: