@After
  public void tearDown() {
    reporter.stop();
    reporter.report();

    executor.shutdownNow();
  }
 private void test(DataSource dataSource) throws SQLException {
   for (int i = 0; i < callCount; i++) {
     long startNanos = System.nanoTime();
     try (Connection connection = dataSource.getConnection()) {}
     timer.update(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
   }
   logReporter.report();
 }
 @Produces
 @Singleton
 Slf4jReporter reporter(MetricRegistry registry) {
   return Slf4jReporter.forRegistry(registry)
       .convertRatesTo(TimeUnit.SECONDS)
       .convertDurationsTo(TimeUnit.MILLISECONDS)
       .build();
 }
/**
 * <code>ConnectionPoolCallTest</code> - Checks how connection pool decreases latency
 *
 * @author Vlad Mihalcea
 */
@Ignore
public class ConnectionPoolCallTest extends DataSourceProviderIntegrationTest {

  private final Logger LOGGER = LoggerFactory.getLogger(getClass());

  private MetricRegistry metricRegistry = new MetricRegistry();

  private Timer timer = metricRegistry.timer("callTimer");

  private Slf4jReporter logReporter =
      Slf4jReporter.forRegistry(metricRegistry).outputTo(LOGGER).build();

  private int callCount = 1000;

  public ConnectionPoolCallTest(DataSourceProvider dataSourceProvider) {
    super(dataSourceProvider);
  }

  @Override
  protected Class<?>[] entities() {
    return new Class[] {};
  }

  @Test
  public void testNoPooling() throws SQLException {
    LOGGER.info("Test without pooling for {}", dataSourceProvider().database());
    test(dataSourceProvider().dataSource());
  }

  @Test
  public void testPooling() throws SQLException {
    LOGGER.info("Test with pooling for {}", dataSourceProvider().database());
    test(poolingDataSource());
  }

  private void test(DataSource dataSource) throws SQLException {
    for (int i = 0; i < callCount; i++) {
      long startNanos = System.nanoTime();
      try (Connection connection = dataSource.getConnection()) {}
      timer.update(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
    }
    logReporter.report();
  }

  protected HikariDataSource poolingDataSource() {
    Properties properties = new Properties();
    properties.setProperty(
        "dataSourceClassName", dataSourceProvider().dataSourceClassName().getName());
    properties.put("dataSourceProperties", dataSourceProvider().dataSourceProperties());
    properties.setProperty("minimumPoolSize", String.valueOf(1));
    properties.setProperty("maximumPoolSize", String.valueOf(3));
    properties.setProperty("connectionTimeout", String.valueOf(100));
    return new HikariDataSource(new HikariConfig(properties));
  }
}
  @Before
  public void setupOrg() {

    originalShardSize = ConfigurationManager.getConfigInstance().getProperty(GraphFig.SHARD_SIZE);

    originalShardTimeout =
        ConfigurationManager.getConfigInstance().getProperty(GraphFig.SHARD_CACHE_TIMEOUT);

    originalShardDelta =
        ConfigurationManager.getConfigInstance().getProperty(GraphFig.SHARD_MIN_DELTA);

    ConfigurationManager.getConfigInstance().setProperty(GraphFig.SHARD_SIZE, 500);

    final long cacheTimeout = 2000;
    // set our cache timeout to the above value
    ConfigurationManager.getConfigInstance()
        .setProperty(GraphFig.SHARD_CACHE_TIMEOUT, cacheTimeout);

    final long minDelta = (long) (cacheTimeout * 2.5);

    ConfigurationManager.getConfigInstance().setProperty(GraphFig.SHARD_MIN_DELTA, minDelta);

    // get the system property of the UUID to use.  If one is not set, use the defualt
    String uuidString = System.getProperty("org.id", "80a42760-b699-11e3-a5e2-0800200c9a66");

    scope = new ApplicationScopeImpl(IdGenerator.createId(UUID.fromString(uuidString), "test"));

    reporter =
        Slf4jReporter.forRegistry(registry)
            .outputTo(logger)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();

    reporter.start(10, TimeUnit.SECONDS);
  }
 static {
   logReporter.start(30, TimeUnit.SECONDS);
 }
/**
 * ConnectionProxyFactoryPerformanceTest - ConnectionProxyFactory Performance Test
 *
 * @author Vlad Mihalcea
 */
public class ConnectionProxyFactoryPerformanceTest extends ConnectionDecoratorTest {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(ConnectionProxyFactoryPerformanceTest.class);

  private static MetricRegistry metricRegistry = new MetricRegistry();

  private static Slf4jReporter logReporter =
      Slf4jReporter.forRegistry(metricRegistry).outputTo(LOGGER).build();

  private static Timer noProxyHistogram = metricRegistry.timer("no-proxy");
  private static Timer proxyHistogram = metricRegistry.timer("proxy");

  static {
    logReporter.start(30, TimeUnit.SECONDS);
  }

  private final Connection targetConnection = Mockito.mock(Connection.class);
  private final ConnectionCallback connectionCallback = Mockito.mock(ConnectionCallback.class);

  private final InvocationHandler invocationHandler =
      new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          return method.invoke(targetConnection, args);
        }
      };
  private final Connection proxy =
      (Connection)
          Proxy.newProxyInstance(
              this.getClass().getClassLoader(), new Class[] {Connection.class}, invocationHandler);

  private ConnectionDecorator connectionDecorator =
      new ConnectionDecorator(targetConnection, connectionCallback);

  @Test
  public void testPerformance() {
    if (TestUtils.isPerformanceTesting()) {
      long durationMillis = 30 * 1000;
      timeAllMethodsForDuration(
          new Callable() {
            @Override
            public Object call() throws Exception {
              callConnectionMethods(noProxyHistogram);
              return null;
            }
          },
          durationMillis);
      timeAllMethodsForDuration(
          new Callable() {
            @Override
            public Object call() throws Exception {
              callConnectionMethods(proxyHistogram);
              return null;
            }
          },
          durationMillis);
    }
  }

  private void callConnectionMethods(Timer timer) throws SQLException {
    long startNanos = System.nanoTime();
    try {
      connectionDecorator.getMetaData();
      String javaVersion = System.getProperty("java.version");
      if (javaVersion.contains("1.7") || javaVersion.contains("1.8")) {
        connectionDecorator.setSchema("schema");
        connectionDecorator.abort(null);
      }
    } finally {
      long endNanos = System.nanoTime();
      timer.update((endNanos - startNanos), TimeUnit.NANOSECONDS);
    }
  }

  private void timeAllMethodsForDuration(Callable callable, long durationMillis) {
    long startMillis = System.currentTimeMillis();
    int i = 0;
    while (true) {
      long endMillis = System.currentTimeMillis();
      if (i % 1000 == 0) {
        LOGGER.info("Iteration: {}, total millis {}!", i, endMillis - startMillis);
        if ((endMillis - startMillis) > durationMillis) {
          break;
        }
      }
      try {
        callable.call();
      } catch (Exception e) {
        throw new IllegalArgumentException(e);
      }
      i++;
    }
  }
}
 void onStop(@Disposes Slf4jReporter reporter) {
   reporter.stop();
 }
 void onStart(@Observes CamelContextStartedEvent event, Slf4jReporter reporter) {
   reporter.start(10L, TimeUnit.SECONDS);
 }