package com.benchmark;


import com.benchmark.util.DateUtility;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Date;
import java.util.Random;
import java.util.UUID;


public class MySQL
{
    private Connection con;

    public static void main(String[] args)
    {
        new MySQL();
    }

    public MySQL()
    {
        try
        {
            connect();
            selectTest();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void connect()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "test", "root", "admin");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void createTable() throws SQLException
    {
        try
        {
            con.createStatement().execute("DROP TABLE test");
            con.createStatement().execute("CREATE TABLE test " + "(id INTEGER not NULL AUTO_INCREMENT, " + " test1 VARCHAR(255), " + " test2 VARCHAR(255), " +
                                          " test3 VARCHAR(255), " + " test4 INTEGER, " + " PRIMARY KEY ( id ))");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            con.close();
        }
    }

    private void insertBenchmarkVeri()
    {
        final Statement st;
        try
        {
            st = (Statement)con.createStatement();
            Random generator = new Random();
            String c, d, e;
            int x = 0;
            int ayniKayitLimit = 50000; // select yapildiginda donecek kayit sayisi
            int nrCount = 0;

            long startTime, totalTime;
            startTime = System.currentTimeMillis();
            System.out.println("startTime for insert operation in ms: " + startTime + " sys date: " + DateUtility.getToday());
            ;
            for (int i = 0; i < 1000000; i++)
            {
                if (i % 250000 == 0)
                {
                    System.gc();
                }
                final String randomString = UUID.randomUUID().toString();
                c = randomString.substring(0, 5);
                d = randomString.substring(5, 10);
                e = randomString.substring(10, 15);
                if (ayniKayitLimit >= nrCount)
                {
                    nrCount++;
                    st.execute("INSERT INTO `test` (`test1`, `test2`, `test3`, `test4`) VALUES ('" + c + "', '" + d + "', '" + e + "', 12345)");
                } else
                {
                    x = generator.nextInt(9999);
                    st.execute("INSERT INTO `test` (`test1`, `test2`, `test3`, `test4`) VALUES ('" + c + "', '" + d + "', '" + e + "', '" + x + "')");
                }
            }
            totalTime = DateUtility.getSecondsBetween(startTime, System.currentTimeMillis());
            System.out.println("totalTime of insert operation in second: " + totalTime);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    private void selectTest()
    {
        ResultSet res;
        long startTime, totalTime;
        Statement st;
        try
        {
            st = (Statement)con.createStatement();
            startTime = System.currentTimeMillis();
            res = st.executeQuery("SELECT * FROM test WHERE `test4`=12345");
            totalTime = System.currentTimeMillis() - startTime;
            System.out.println("Select time: " + totalTime + " ms");
            startTime = System.currentTimeMillis();
            int count = -1;
            while (res.next())
            {
                count++;
            }
            totalTime = System.currentTimeMillis() - startTime;
            System.out.println("Fetch time of select operation in ms: " + totalTime);
            System.out.println("Rows of select count: " + count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}
