Guest User

test_pxc

a guest
Jun 6th, 2018
24
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Modifications;
  4.  
  5. $configs = [
  6.     'host'       => 'localhost',
  7.     'port'       => 3306,
  8.     'user'       => 'root',
  9.     'pass'       => 'root',
  10.     'db_name'    => 'test',
  11.     'table_name' => 'test_modification',
  12. ];
  13.  
  14. $test = new Script1($configs);
  15. $test->run();
  16.  
  17. class Script1
  18. {
  19.     /** @var array */
  20.     private $configs = [];
  21.  
  22.     private $connection = NULL;
  23.  
  24.     // ########################################
  25.  
  26.     public function __construct(array $configs)
  27.     {
  28.         $this->configs = $configs;
  29.     }
  30.  
  31.     // ########################################
  32.  
  33.     public function run()
  34.     {
  35.         try {
  36.  
  37.             $this->preparationWorkingEnvironment();
  38.  
  39.             while (true) {
  40.  
  41.                 if ($this->getConnection()->query($this->getRandomQuery()) === false) {
  42.                     throw $this->createException('Could not execute query.');
  43.                 }
  44.             }
  45.  
  46.         } catch (\Exception $exception) {
  47.             echo $exception->getMessage();
  48.         }
  49.     }
  50.  
  51.     // ########################################
  52.  
  53.     public function preparationWorkingEnvironment()
  54.     {
  55.         $createTableSql = <<<SQL
  56.  
  57. CREATE TABLE IF NOT EXISTS {$this->configs['table_name']} (
  58.   id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  59.   field_int int(10) NOT NULL,
  60.   field_float float NOT NULL,
  61.   field_varchar varchar(100) NOT NULL,
  62.   field_text longtext NOT NULL,
  63.   field_datetime datetime NOT NULL,
  64.   PRIMARY KEY (id)
  65. )
  66. ENGINE = INNODB
  67. CHARACTER SET utf8
  68. COLLATE utf8_unicode_ci;
  69.    
  70. SQL;
  71.  
  72.         if ($this->getConnection()->query($createTableSql) === false) {
  73.             throw $this->createException('Could not create table.');
  74.         }
  75.  
  76.         $preparedInserts = [];
  77.         for ($i = 1; $i <= 1000; $i++) {
  78.  
  79.             $floatVal = mt_rand() / mt_getrandmax();
  80.             $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
  81.             $dateTime->modify("+ {$i} day");
  82.  
  83.             $preparedInserts[] = "({$i}, {$floatVal}, '{$this->generateRandomString(25)}', '{$this->generateRandomString(200)}', '{$dateTime->format('Y-m-d H:i:s')}')";
  84.         }
  85.  
  86.         $insertQuery = "INSERT INTO {$this->configs['table_name']} (field_int, field_float, field_varchar, field_text, field_datetime)
  87.                        VALUES " . implode(',', $preparedInserts);
  88.  
  89.         if ($this->getConnection()->query($insertQuery) === false) {
  90.             throw $this->createException('Can not fill the table with test data.');
  91.         }
  92.     }
  93.  
  94.     // ########################################
  95.  
  96.     private function generateRandomString($length = 10)
  97.     {
  98.         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  99.         $charactersLength = strlen($characters);
  100.         $randomString = '';
  101.  
  102.         for ($i = 0; $i < $length; $i++) {
  103.             $randomString .= $characters[rand(0, $charactersLength - 1)];
  104.         }
  105.  
  106.         return $randomString;
  107.     }
  108.  
  109.     // ########################################
  110.  
  111.     private function getRandomQuery()
  112.     {
  113.         $queries = [
  114.             "INSERT INTO {$this->configs['table_name']} (field_int, field_float, field_varchar, field_text, field_datetime)
  115.             VALUES (51, 0.51, '{$this->generateRandomString(25)}', '{$this->generateRandomString(200)}', '{$this->getCurrentDateTime()->format('Y-m-d H:i:s')}')",
  116.             "UPDATE {$this->configs['table_name']} SET field_text = '{$this->generateRandomString(150)}' WHERE field_int > 10 LIMIT 400",
  117.             "DELETE FROM {$this->configs['table_name']} WHERE field_float = 0.51",
  118.         ];
  119.  
  120.         return $queries[rand(0, 2)];
  121.     }
  122.  
  123.     // ########################################
  124.  
  125.     private function getConnection()
  126.     {
  127.         if (!is_null($this->connection)) {
  128.             return $this->connection;
  129.         }
  130.  
  131.         $connection = new \PDO("mysql:host={$this->configs['host']};dbname={$this->configs['db_name']};port={$this->configs['port']}",
  132.                                $this->configs['user'], $this->configs['pass'],
  133.                                [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
  134.  
  135.         return $this->connection = $connection;
  136.     }
  137.  
  138.     // ########################################
  139.  
  140.     private function createException($message)
  141.     {
  142.         return new \Exception($message . PHP_EOL . 'Error info:' . PHP_EOL . print_r($this->getConnection()->errorInfo(), true));
  143.     }
  144.  
  145.     private function getCurrentDateTime()
  146.     {
  147.         return new \DateTime('now', new \DateTimeZone('UTC'));
  148.     }
  149.  
  150.     // ########################################
  151. }
RAW Paste Data