You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/02/21 09:24:22 UTC

[incubator-shardingsphere-benchmark] branch master updated: update datasource util

This is an automated email from the ASF dual-hosted git repository.

zhaoyanan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shardingsphere-benchmark.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d41b97  update datasource util
4d41b97 is described below

commit 4d41b970923fa780ef36d106fe8e3c96b0595d72
Author: nancyzrh <zh...@126.com>
AuthorDate: Fri Feb 21 17:24:04 2020 +0800

    update datasource util
---
 .../java/service/util/config/DataSourceUtil.java   | 127 ++++++++++++++++++++-
 1 file changed, 126 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-benchmark/src/main/java/service/util/config/DataSourceUtil.java b/shardingsphere-benchmark/src/main/java/service/util/config/DataSourceUtil.java
index 5fb4501..5f9841d 100644
--- a/shardingsphere-benchmark/src/main/java/service/util/config/DataSourceUtil.java
+++ b/shardingsphere-benchmark/src/main/java/service/util/config/DataSourceUtil.java
@@ -17,8 +17,133 @@
 
 package service.util.config;
 
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+
+import javax.sql.DataSource;
+import java.sql.*;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
- * datasource utils for MySQLJdbc.
+ * datasource utils for MySQL Jdbc.
  */
 public class DataSourceUtil {
+    private static final Map<String, DataSource> datasourceMap = new HashMap<>();
+    
+    /**
+     * create data source for jdbc
+     * @param userName user name
+     * @param dataSourceName datasource name
+     * @param host host
+     * @param port port
+     * @param password pwd
+     */
+    public static void createDataSource(final String userName, final String dataSourceName, final String host, final int port, final String password) {
+        HikariConfig config = new HikariConfig();
+        config.setDriverClassName("com.mysql.jdbc.Driver");
+        config.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?useSSL=false&serverTimezone=UTC", host, port, dataSourceName));
+        config.setUsername(userName);
+        config.setPassword(password);
+        config.setMaximumPoolSize(200);
+        config.setConnectionTimeout(30000);
+        config.setIdleTimeout(60000);
+        config.setMaxLifetime(1800000);
+        config.addDataSourceProperty("useServerPrepStmts", Boolean.TRUE.toString());
+        config.addDataSourceProperty("cachePrepStmts", "true");
+        config.addDataSourceProperty("prepStmtCacheSize", 250);
+        config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
+        config.addDataSourceProperty("useLocalSessionState", Boolean.TRUE.toString());
+        config.addDataSourceProperty("rewriteBatchedStatements", Boolean.TRUE.toString());
+        config.addDataSourceProperty("cacheResultSetMetadata", Boolean.TRUE.toString());
+        config.addDataSourceProperty("cacheServerConfiguration", Boolean.TRUE.toString());
+        config.addDataSourceProperty("elideSetAutoCommits", Boolean.TRUE.toString());
+        config.addDataSourceProperty("maintainTimeStats", Boolean.FALSE.toString());
+        config.addDataSourceProperty("netTimeoutForStreamingResults", 0);
+        DataSource dataSource = new HikariDataSource(config);
+        datasourceMap.put(dataSourceName, dataSource);
+    }
+    
+    /**
+     * get datasource from name.
+     * @param dataSourceName input name
+     * @return datasource
+     */
+    public static DataSource getDataSource(final String dataSourceName) {
+        return datasourceMap.get(dataSourceName);
+    }
+    
+    /**
+     * for master slave compare
+     * @param datasource
+     * @throws SQLException
+     */
+    public static void  writeReadOp(final String datasource) throws SQLException {
+        String sql = "INSERT INTO ssperf (k,c,pad) VALUES (?,?,?)";
+        Connection connection = null;
+        try {
+            connection = getDataSource(datasource).getConnection();
+            PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
+            preparedStatement.setInt(1, 1);
+            preparedStatement.setString(2, "##-####");
+            preparedStatement.setString(3, "##-####");
+            preparedStatement.executeUpdate();
+            ResultSet result = preparedStatement.getGeneratedKeys();
+            result.next();
+            Long id = result.getLong(1);
+            sql = "select count(id) from ssperf";
+            preparedStatement = connection.prepareStatement(sql);
+            preparedStatement.executeQuery();
+            sql = "select max(id) from ssperf ignore index(`PRIMARY`)";
+            preparedStatement = connection.prepareStatement(sql);
+            preparedStatement.executeQuery();
+            sql = "delete from ssperf where k=? and id=?";
+            preparedStatement = connection.prepareStatement(sql);
+            preparedStatement.setInt(1, 1);
+            preparedStatement.setLong(2, id);
+            preparedStatement.executeUpdate();
+            preparedStatement.close();
+            result.close();
+        }catch (final SQLException ex) {
+            ex.printStackTrace();
+        } finally {
+            connection.close();
+        }
+    }
+    
+    public static void  writeOp(final String datasource) throws SQLException {
+        String sql = "INSERT INTO ssperf (k,c,pad) VALUES (?,?,?)";
+        Connection connection = null;
+        try {
+            connection = getDataSource(datasource).getConnection();
+            PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
+            preparedStatement.setInt(1, 1);
+            preparedStatement.setString(2, "##-####");
+            preparedStatement.setString(3, "##-####");
+            preparedStatement.executeUpdate();
+            ResultSet result = preparedStatement.getGeneratedKeys();
+            result.next();
+            Long id = result.getLong(1);
+            sql = "update ssperf set c=?,pad =? where id=? and k=1";
+            preparedStatement = connection.prepareStatement(sql);
+            preparedStatement.setString(1, "##-#####");
+            preparedStatement.setString(2, "##-#####");
+            preparedStatement.setLong(3, id);
+            preparedStatement.executeUpdate();
+            sql = "delete from ssperf where k=? and id=?";
+            preparedStatement = connection.prepareStatement(sql);
+            preparedStatement.setInt(1, 1);
+            preparedStatement.setLong(2, id);
+            preparedStatement.executeUpdate();
+            preparedStatement.close();
+            result.close();
+        }catch (final SQLException ex) {
+            ex.printStackTrace();
+        } finally {
+            connection.close();
+        }
+        
+    }
+    
 }
+