Guest User

Untitled

a guest
Nov 16th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. package generate_merge;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.Properties;
  19. import java.util.Random;
  20. import java.util.UUID;
  21. import java.util.concurrent.ExecutorService;
  22. import java.util.concurrent.Executors;
  23.  
  24.  
  25. import com.dao.AvailablePools;
  26.  
  27. public class LoadTestDB
  28. {
  29.    
  30.  
  31.     private static final String dbClassName = "com.mysql.jdbc.Driver";
  32.     private static final String dbClassRedshift = "com.amazon.redshift.jdbc4.Driver";
  33.     private static String host = null;
  34.     private static String port = null;
  35.     //private static final String CONNECTION = "jdbc:mysql://192.168.99.100:3306/";
  36. //  private static final String CONNECTION = "jdbc:mysql://10.0.0.119:3306/";  /*Tunneled MySQL */
  37. //  private static final String CONNECTION_RS = "jdbc:redshift://10.0.0.239:5439/myntra_dw";
  38.     private static final String USER = "ddp_ro";
  39.     private static final String PASSWORD = "Admin@123!";
  40.    
  41.     private static final String RS_USER = "myntrapoc";
  42.     private static final String RS_PASSWORD = "5KuLi523T11k82G";
  43.     private static  Integer file_count = 0;
  44.     private static SimpleDateFormat load_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  45.     private static SimpleDateFormat load_date = new SimpleDateFormat("yyyyMMdd");
  46.    
  47.     private static Runtime runtime = Runtime.getRuntime();
  48.    
  49.     private static long startTimer()
  50.     {
  51.         return System.currentTimeMillis();
  52.     }
  53.    
  54.     private static long endTimer()
  55.     {
  56.         return System.currentTimeMillis();
  57.     }
  58.  
  59.        
  60.     private static void worker(Integer file_count) throws Exception{
  61.         try{
  62.             Properties p = new Properties();
  63.             p.put("user", USER);
  64.             p.put("password", PASSWORD);
  65.            
  66.             Properties p_rs = new Properties();
  67.             p_rs.put("user", RS_USER);
  68.             p_rs.put("password", RS_PASSWORD);
  69.            
  70.             String sql_adhoc_query = "select query_txt from adhoc_query.adhoc_query_log where query_id % 12="+file_count+" and start_time >= '2016-11-16 00:00:00' and substring(query_id,11) >5000000 and query_txt not like '%_table_%' order by start_time limit 2000";
  71.             //Connection conn= DriverManager.getConnection(CONNECTION, p);
  72.             Connection conn=null;
  73.             try
  74.             {
  75.             conn= AvailablePools.ADHOC_QUERY_POOL.getConnection();
  76.  
  77.             }
  78.             catch(Exception e)
  79.             {
  80.                 e.printStackTrace();
  81.             }
  82.             Statement stmt = conn.createStatement();
  83.            
  84.            
  85.             ResultSet rs = stmt.executeQuery(sql_adhoc_query);
  86.             Connection conn_rs=null;
  87.             Statement  stmt_rs=null;
  88.             //Statement stmt_rs = null;
  89.             while(rs.next()){
  90.                 String sql = rs.getString("query_txt");
  91.                 long start_time,end_time,run_time;
  92.                 start_time = startTimer();
  93.                 try
  94.                 {
  95.                     end_time = endTimer();
  96.                     run_time = (end_time - start_time)/1000 ;
  97.                     System.out.println("Inside Try");
  98.                     System.out.println(sql);
  99.  
  100.                     conn_rs = AvailablePools.REDSHIFT_QUERY_POOL.getConnection();
  101.                     conn_rs.createStatement().execute(sql);
  102.                     start_time = startTimer();
  103.                     end_time = endTimer();
  104.                     end_time = endTimer();
  105.                     run_time = (end_time - start_time)/1000 ;
  106.                 }catch(Exception e){
  107.                     System.out.println("Thread is : "+file_count);
  108.                     System.out.println(e.toString());
  109.                     if(conn_rs != null){
  110.                         try{
  111.                             conn_rs.close();
  112.                         }catch(Exception connE){e.printStackTrace();}
  113.                     }
  114.                     e.printStackTrace();
  115.                 }
  116.        
  117.             }  //End While
  118.      
  119.         }catch(Exception e) {
  120.             System.out.println("Thread is : "+file_count);
  121.             e.printStackTrace();
  122.         } //Outer Catch
  123.    }
  124.    
  125.     public static void main(String[] args) throws Exception
  126.     {
  127.         Class.forName(dbClassName);
  128.         //ResetEnvironment();
  129.         ExecutorService executor = Executors.newFixedThreadPool(12);
  130.         for (int i = 0; i < 12; i++  )
  131.         {
  132.             executor.submit(new Runnable()
  133.             {
  134.                 @Override
  135.                 public void run()
  136.                 {
  137.                     try
  138.                     {
  139.                         synchronized (LoadTestDB.class)
  140.                         {
  141.                             file_count = file_count + 1;
  142.                         }
  143.                         worker(file_count);
  144.                         //TimeUnit.MILLISECONDS.sleep(10);
  145.                     }
  146.                     catch (Exception e)
  147.                     {
  148.                        
  149.                         e.printStackTrace();
  150.                     }
  151.                 }
  152.             });
  153.         }
  154.  
  155.        
  156.         executor.shutdown();
  157.         // Wait until all threads are finish
  158.         while (!executor.isTerminated()) {
  159.  
  160.         }
  161.         System.out.println("\nFinished all threads");
  162.  
  163.        
  164.     }
  165.    
  166.    
  167.  
  168.  
  169.  
  170.  
  171.  
  172. }
Add Comment
Please, Sign In to add comment