// Path: common-db/src/main/java/org/simpleflatmapper/db/ConnectionParam.java
// @State(Scope.Benchmark)
// public class ConnectionParam {
//     @Param(value="H2")
//     public DbTarget db;
// 
//     public DataSource dataSource;
// 
//     public Connection connection;
// 
//     @Setup
//     public void init() throws SQLException, NamingException {
//         dataSource = ConnectionHelper.getDataSource(db);
// 
//         if (db != DbTarget.MOCK) {
//             Connection conn = dataSource.getConnection();
//             try {
//                 ConnectionHelper.createTableAndInsertData(conn, ConnectionHelper.Table.SMALL);
//                 ConnectionHelper.createTableAndInsertData(conn, ConnectionHelper.Table.BIG);
//             } finally {
//                 conn.close();
//             }
//         }
//         connection = dataSource.getConnection();
// 
//         // Create initial context
//         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
//                 InitialContextFactory.class.getName());
//         InitialContext ic = new InitialContext();
// 
//         ic.bind("java:datasource", dataSource);
// 
//     }
// 
//     public Connection getConnection() throws SQLException {
//         return dataSource.getConnection();
//     }
// 
//     public void executeStatement(String statement, ResultSetHandler handler, Object... params) throws Exception {
//         PreparedStatement prepareStatement = connection.prepareStatement(statement);
//         try {
//             setParams(prepareStatement, params);
//             ResultSet rs = prepareStatement.executeQuery();
//             try {
//                 handler.handle(rs);
//             }finally {
//                 rs.close();
//             }
//         } finally {
//             prepareStatement.close();
//         }
//     }
// 
//     public void setParams(PreparedStatement prepareStatement, Object[] params) throws SQLException {
//         if (params != null) {
//             for(int i = 0; i < params.length; i++) {
//                 prepareStatement.setObject(i + 1, params[i]);
//             }
//         }
//     }
// }
// 
// Path: common-jpa/src/main/java/org/simpleflatmapper/jpa/beans/JPATester.java
// public class JPATester {
// 
// 
//     public static void select4Fields(EntityManagerFactory entityManagerFactory, LimitParam limitParam, Blackhole blackhole) {
//         select(entityManagerFactory, limitParam, blackhole, "select s from MappedObject4 s");
//     }
// 
//     public static void select16Fields(EntityManagerFactory entityManagerFactory, LimitParam limitParam, Blackhole blackhole) {
//         select(entityManagerFactory, limitParam, blackhole, "select s from MappedObject16 s");
//     }
// 
//     public static void select(EntityManagerFactory entityManagerFactory, LimitParam limit, Blackhole blackhole, String strQuery)  {
//         EntityManager entityManager = entityManagerFactory.createEntityManager();
//         try {
//             Query query = entityManager.createQuery(strQuery);
//             query.setMaxResults(limit.limit);
//             List<?> sr = query.getResultList();
//             for (Object o : sr) {
//                 blackhole.consume(o);
//             }
//         } finally {
//             entityManager.close();
//         }
//     }
// }


import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.simpleflatmapper.db.ConnectionParam;
import org.simpleflatmapper.db.DbTarget;
import org.simpleflatmapper.jpa.beans.JPATester;
import org.simpleflatmapper.param.LimitParam;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package org.simpleflatmapper;



@State(Scope.Benchmark)
/**
 * batoo has a dependency on a old library of asm.
 * batoo benchmark cannot run at the sane time as sfm asm.
 * needs to exclude asm from the pom to run batoo.
 * 
 *
 */

/*
Benchmark                 (db)  (limit)   Mode  Cnt       Score      Error  Units
BatooBenchmark._04Fields    H2        1  thrpt   20  424105.769 ± 7998.794  ops/s
BatooBenchmark._04Fields    H2       10  thrpt   20  184095.607 ± 2349.584  ops/s
BatooBenchmark._04Fields    H2      100  thrpt   20   26205.799 ±  371.146  ops/s
BatooBenchmark._04Fields    H2     1000  thrpt   20    2667.718 ±   56.348  ops/s
BatooBenchmark._16Fields    H2        1  thrpt   20  172638.194 ± 3858.660  ops/s
BatooBenchmark._16Fields    H2       10  thrpt   20   64638.056 ± 4692.277  ops/s
BatooBenchmark._16Fields    H2      100  thrpt   20    8910.878 ±  731.142  ops/s
BatooBenchmark._16Fields    H2     1000  thrpt   20     916.999 ±   40.375  ops/s

 */
public class HibernateBenchmark {

	private EntityManagerFactory sf;
	
	@Param(value="H2")
	DbTarget db;


	@Setup
	public void init() throws Exception  {
		ConnectionParam connParam = new ConnectionParam();
		connParam.db = db;
		connParam.init();
		sf = Persistence.createEntityManagerFactory("jpa");
	}

	@Benchmark
	public void _04Fields(LimitParam limit, final Blackhole blackhole) throws Exception {
// Path: common-db/src/main/java/org/simpleflatmapper/db/ConnectionParam.java
// @State(Scope.Benchmark)
// public class ConnectionParam {
//     @Param(value="H2")
//     public DbTarget db;
// 
//     public DataSource dataSource;
// 
//     public Connection connection;
// 
//     @Setup
//     public void init() throws SQLException, NamingException {
//         dataSource = ConnectionHelper.getDataSource(db);
// 
//         if (db != DbTarget.MOCK) {
//             Connection conn = dataSource.getConnection();
//             try {
//                 ConnectionHelper.createTableAndInsertData(conn, ConnectionHelper.Table.SMALL);
//                 ConnectionHelper.createTableAndInsertData(conn, ConnectionHelper.Table.BIG);
//             } finally {
//                 conn.close();
//             }
//         }
//         connection = dataSource.getConnection();
// 
//         // Create initial context
//         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
//                 InitialContextFactory.class.getName());
//         InitialContext ic = new InitialContext();
// 
//         ic.bind("java:datasource", dataSource);
// 
//     }
// 
//     public Connection getConnection() throws SQLException {
//         return dataSource.getConnection();
//     }
// 
//     public void executeStatement(String statement, ResultSetHandler handler, Object... params) throws Exception {
//         PreparedStatement prepareStatement = connection.prepareStatement(statement);
//         try {
//             setParams(prepareStatement, params);
//             ResultSet rs = prepareStatement.executeQuery();
//             try {
//                 handler.handle(rs);
//             }finally {
//                 rs.close();
//             }
//         } finally {
//             prepareStatement.close();
//         }
//     }
// 
//     public void setParams(PreparedStatement prepareStatement, Object[] params) throws SQLException {
//         if (params != null) {
//             for(int i = 0; i < params.length; i++) {
//                 prepareStatement.setObject(i + 1, params[i]);
//             }
//         }
//     }
// }
// 
// Path: common-jpa/src/main/java/org/simpleflatmapper/jpa/beans/JPATester.java
// public class JPATester {
// 
// 
//     public static void select4Fields(EntityManagerFactory entityManagerFactory, LimitParam limitParam, Blackhole blackhole) {
//         select(entityManagerFactory, limitParam, blackhole, "select s from MappedObject4 s");
//     }
// 
//     public static void select16Fields(EntityManagerFactory entityManagerFactory, LimitParam limitParam, Blackhole blackhole) {
//         select(entityManagerFactory, limitParam, blackhole, "select s from MappedObject16 s");
//     }
// 
//     public static void select(EntityManagerFactory entityManagerFactory, LimitParam limit, Blackhole blackhole, String strQuery)  {
//         EntityManager entityManager = entityManagerFactory.createEntityManager();
//         try {
//             Query query = entityManager.createQuery(strQuery);
//             query.setMaxResults(limit.limit);
//             List<?> sr = query.getResultList();
//             for (Object o : sr) {
//                 blackhole.consume(o);
//             }
//         } finally {
//             entityManager.close();
//         }
//     }
// }


// Path: jpa-hibernate/src/main/java/org/simpleflatmapper/HibernateBenchmark.java
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.simpleflatmapper.db.ConnectionParam;
import org.simpleflatmapper.db.DbTarget;
import org.simpleflatmapper.jpa.beans.JPATester;
import org.simpleflatmapper.param.LimitParam;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

package org.simpleflatmapper;



@State(Scope.Benchmark)
/**
 * batoo has a dependency on a old library of asm.
 * batoo benchmark cannot run at the sane time as sfm asm.
 * needs to exclude asm from the pom to run batoo.
 * 
 *
 */

/*
Benchmark                 (db)  (limit)   Mode  Cnt       Score      Error  Units
BatooBenchmark._04Fields    H2        1  thrpt   20  424105.769 ± 7998.794  ops/s
BatooBenchmark._04Fields    H2       10  thrpt   20  184095.607 ± 2349.584  ops/s
BatooBenchmark._04Fields    H2      100  thrpt   20   26205.799 ±  371.146  ops/s
BatooBenchmark._04Fields    H2     1000  thrpt   20    2667.718 ±   56.348  ops/s
BatooBenchmark._16Fields    H2        1  thrpt   20  172638.194 ± 3858.660  ops/s
BatooBenchmark._16Fields    H2       10  thrpt   20   64638.056 ± 4692.277  ops/s
BatooBenchmark._16Fields    H2      100  thrpt   20    8910.878 ±  731.142  ops/s
BatooBenchmark._16Fields    H2     1000  thrpt   20     916.999 ±   40.375  ops/s

 */
public class HibernateBenchmark {

	private EntityManagerFactory sf;
	
	@Param(value="H2")
	DbTarget db;


	@Setup
	public void init() throws Exception  {
		ConnectionParam connParam = new ConnectionParam();
		connParam.db = db;
		connParam.init();
		sf = Persistence.createEntityManagerFactory("jpa");
	}

	@Benchmark
	public void _04Fields(LimitParam limit, final Blackhole blackhole) throws Exception {
		JPATester.select4Fields(sf, limit, blackhole);