|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.it.techempower; |
| 7 | + |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +import org.hibernate.SessionFactory; |
| 11 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 12 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.reactive.logging.impl.Log; |
| 15 | +import org.hibernate.reactive.mutiny.Mutiny; |
| 16 | +import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder; |
| 17 | +import org.hibernate.reactive.provider.Settings; |
| 18 | +import org.hibernate.reactive.vertx.VertxInstance; |
| 19 | + |
| 20 | + |
| 21 | +import io.vertx.core.DeploymentOptions; |
| 22 | +import io.vertx.core.Vertx; |
| 23 | +import io.vertx.core.VertxOptions; |
| 24 | +import org.testcontainers.containers.BindMode; |
| 25 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 26 | +import org.testcontainers.containers.output.OutputFrame; |
| 27 | + |
| 28 | +import static java.lang.invoke.MethodHandles.lookup; |
| 29 | +import static org.hibernate.reactive.logging.impl.LoggerFactory.make; |
| 30 | + |
| 31 | +/** |
| 32 | + * Make it easier to run benchmarks with external tools like "wrk" |
| 33 | + */ |
| 34 | +public class VertxServer { |
| 35 | + |
| 36 | + private static final Log LOG = make( Log.class, lookup() ); |
| 37 | + |
| 38 | + // These properties are in DatabaseConfiguration in core |
| 39 | + public static final boolean USE_DOCKER = isDockerEnabled(); |
| 40 | + |
| 41 | + // If not specify, default to enable docker |
| 42 | + private static boolean isDockerEnabled() { |
| 43 | + String enableDocker = System.getProperty( "docker" ); |
| 44 | + if ( enableDocker == null ) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + return Boolean.parseBoolean( enableDocker ); |
| 48 | + } |
| 49 | + |
| 50 | + public static final String IMAGE_NAME = "postgres:15-bullseye"; |
| 51 | + public static final String USERNAME = "benchmarkdbuser"; |
| 52 | + public static final String PASSWORD = "benchmarkdbpass"; |
| 53 | + public static final String DB_NAME = "hello_world"; |
| 54 | + |
| 55 | + public static final int VERTICLE_INSTANCES = 10; |
| 56 | + |
| 57 | + private static final String POSGRESQL_CONF_PATH = "/postgresql-min.conf"; |
| 58 | + |
| 59 | + public static final PostgreSQLContainer<?> postgresql = new PostgreSQLContainer<>( IMAGE_NAME ) |
| 60 | + .withUsername( USERNAME ) |
| 61 | + .withPassword( PASSWORD ) |
| 62 | + .withDatabaseName( DB_NAME ) |
| 63 | + .withCommand( "postgres", "-c", "config_file=/ssd/postgresql/postgresql.conf" ) |
| 64 | + .withEnv( "PG_DATA", "/ssd/postgresql" ) |
| 65 | + .withClasspathResourceMapping( |
| 66 | + POSGRESQL_CONF_PATH, |
| 67 | + "/ssd/postgresql/postgresql.conf", |
| 68 | + BindMode.READ_ONLY |
| 69 | + ) |
| 70 | + .withReuse( true ) |
| 71 | + .withLogConsumer( VertxServer::log ); |
| 72 | + |
| 73 | + private static void log(OutputFrame outputFrame) { |
| 74 | + LOG.info( outputFrame.getUtf8String() ); |
| 75 | + } |
| 76 | + |
| 77 | + private static Configuration constructConfiguration(boolean enableDocker) { |
| 78 | + Configuration configuration = new Configuration(); |
| 79 | + configuration.addAnnotatedClass( World.class ); |
| 80 | + |
| 81 | + configuration.setProperty( Settings.HBM2DDL_AUTO, "create" ); |
| 82 | + configuration.setProperty( Settings.URL, dbConnectionUrl( enableDocker ) ); |
| 83 | + configuration.setProperty( Settings.USER, USERNAME ); |
| 84 | + configuration.setProperty( Settings.PASS, PASSWORD ); |
| 85 | + |
| 86 | + //Use JAVA_TOOL_OPTIONS='-Dhibernate.show_sql=true' |
| 87 | + configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "false" ) ); |
| 88 | + configuration.setProperty( Settings.FORMAT_SQL, System.getProperty( Settings.FORMAT_SQL, "false" ) ); |
| 89 | + configuration.setProperty( Settings.HIGHLIGHT_SQL, System.getProperty( Settings.HIGHLIGHT_SQL, "true" ) ); |
| 90 | + return configuration; |
| 91 | + } |
| 92 | + |
| 93 | + private static String dbConnectionUrl(boolean enableDocker) { |
| 94 | + if ( enableDocker ) { |
| 95 | + // Calling start() will start the container (if not already started) |
| 96 | + // It is required to call start() before obtaining the JDBC URL because it will contain a randomized port |
| 97 | + postgresql.start(); |
| 98 | + return postgresql.getJdbcUrl(); |
| 99 | + } |
| 100 | + |
| 101 | + // When we don't use testcontainers we expect a database on the default url |
| 102 | + return "postgres://localhost:5432/" + DB_NAME; |
| 103 | + } |
| 104 | + |
| 105 | + public static SessionFactory createHibernateSessionFactory(boolean enableDocker, io.vertx.core.Vertx vertx) { |
| 106 | + final Configuration configuration = constructConfiguration( enableDocker ); |
| 107 | + StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder() |
| 108 | + .addService( VertxInstance.class, (VertxInstance) () -> vertx ) |
| 109 | + .applySettings( configuration.getProperties() ); |
| 110 | + StandardServiceRegistry registry = builder.build(); |
| 111 | + return configuration.buildSessionFactory( registry ); |
| 112 | + } |
| 113 | + |
| 114 | + public static VertxOptions vertxOptions() { |
| 115 | + VertxOptions vertxOptions = new VertxOptions(); |
| 116 | + vertxOptions.setBlockedThreadCheckInterval( 5 ); |
| 117 | + vertxOptions.setBlockedThreadCheckIntervalUnit( TimeUnit.MINUTES ); |
| 118 | + return vertxOptions; |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) { |
| 122 | + DeploymentOptions options = new DeploymentOptions(); |
| 123 | + options.setInstances( VERTICLE_INSTANCES ); |
| 124 | + |
| 125 | + Vertx vertx = Vertx.vertx( vertxOptions() ); |
| 126 | + final Mutiny.SessionFactory sessionFactory = createHibernateSessionFactory( USE_DOCKER, vertx ) |
| 127 | + .unwrap( Mutiny.SessionFactory.class ); |
| 128 | + vertx.deployVerticle( () -> new WorldVerticle( () -> sessionFactory ), options ) |
| 129 | + .onSuccess( s -> { |
| 130 | + LOG.info( "✅ Deployment success" ); |
| 131 | + LOG.info( "💡 Vert.x app started" ); |
| 132 | + } ) |
| 133 | + .onFailure( err -> LOG.errorf( "🔥 Deployment failure", err ) ); |
| 134 | + } |
| 135 | +} |
0 commit comments