All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.postgresql.benchmark.statement.FinalizeStatement Maven / Gradle / Ivy

There is a newer version: 42.2.2
Show newest version
/*
 * Copyright (c) 2003, PostgreSQL Global Development Group
 * See the LICENSE file in the project root for more information.
 */

package org.postgresql.benchmark.statement;

import org.postgresql.util.ConnectionUtil;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Here we measure the time it takes to create and close a dummy statement.
 *
 * 

To run this and other benchmarks (you can run the class from within IDE): * *

mvn package && java -classpath postgresql-driver.jar:target/benchmarks.jar * -Duser=postgres -Dpassword=postgres -Dport=5433 -wi 10 -i 10 -f 1
* *

To run with profiling: * *

java -classpath postgresql-driver.jar:target/benchmarks.jar -prof gc -f 1 -wi * 10 -i 10
*/ @Fork(value = 1, jvmArgsPrepend = "-Xmx128m") @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) @State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class FinalizeStatement { @Param({"0", "1", "10", "100"}) private int leakPct; private float leakPctFloat; private Connection connection; //JCP! if mvn.project.property.current.jdk < "1.7" //JCP> private Random random = new Random(); //JCP! endif @Setup(Level.Trial) public void setUp() throws SQLException { Properties props = ConnectionUtil.getProperties(); connection = DriverManager.getConnection(ConnectionUtil.getURL(), props); leakPctFloat = 0.01f * leakPct; } @TearDown(Level.Trial) public void tearDown() throws SQLException { connection.close(); } @Benchmark public Statement createAndLeak() throws SQLException { Statement statement = connection.createStatement(); Random rnd; //JCP! if mvn.project.property.current.jdk < "1.7" //JCP> rnd = random; //JCP! else rnd = java.util.concurrent.ThreadLocalRandom.current(); //JCP! endif if (rnd.nextFloat() >= leakPctFloat) { statement.close(); } return statement; } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(FinalizeStatement.class.getSimpleName()) .addProfiler(GCProfiler.class) .detectJvmArgs() .build(); new Runner(opt).run(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy