/*
Copyright (C) 2010, Oleg Efimov <efimovov@gmail.com>

See license text in LICENSE file
*/

// Require modules
var
  sys = require('sys'),
  Mysql = require('../deps/felixge-node-mysql/lib/mysql').Client,
  conn,
  rows;

function selectAsyncBenchmark(callback, cfg) {
  var
    start_time,
    total_time;
  
  start_time = new Date();
  
  conn.query("SELECT * FROM " + cfg.test_table + ";", function (err, results, fields) {
    total_time = ((new Date()) - start_time) / 1000;
    sys.puts("**** " + cfg.insert_rows_count + " rows async selected in " + total_time + "s (" + Math.round(cfg.insert_rows_count / total_time) + "/s)");
    
    // Finish benchmark
    conn.end();
    callback.apply();
  });
}

function insertAsyncBenchmark(callback, cfg) {
  var
    start_time,
    total_time,
    i = 0;
  
  start_time = new Date();
  
  function insertAsync() {
    i += 1;
    if (i <= cfg.insert_rows_count) {
      conn.query(cfg.insert_query, function (err, result) {
        insertAsync();
      });
    } else {
      total_time = ((new Date()) - start_time) / 1000;
      sys.puts("**** " + cfg.insert_rows_count + " async insertions in " + total_time + "s (" + Math.round(cfg.insert_rows_count / total_time) + "/s)");
      
      setTimeout(function () {
        selectAsyncBenchmark(callback, cfg);
      }, cfg.delay_before_select);
    }
  }
  
  insertAsync();
}

function reconnectAsyncBenchmark(callback, cfg) {
  var
    start_time,
    total_time,
    i = 0;
  
    start_time = new Date();
    
    function reconnectAsync() {
      i += 1;
      if (i <= cfg.reconnect_count) {
        conn.end(function () {
          conn.connect(function (err, result) {
            reconnectAsync();
          });
        });
      } else {
        total_time = ((new Date()) - start_time) / 1000;
        sys.puts("**** " + cfg.reconnect_count + " async reconnects in " + total_time + "s (" + Math.round(cfg.reconnect_count / total_time) + "/s)");
        
        insertAsyncBenchmark(callback, cfg);
      }
    }
    
    reconnectAsync();
}

function escapeBenchmark(callback, cfg) {
  var
    start_time,
    total_time,
    i = 0,
    escaped_string;
  
  start_time = new Date();
  
  for (i = 0; i < cfg.escape_count; i += 1) {
    escaped_string = conn.escape(cfg.string_to_escape);
  }
  
  total_time = ((new Date()) - start_time) / 1000;
  sys.puts("**** " + cfg.escape_count + " escapes in " + total_time + "s (" + Math.round(cfg.escape_count / total_time) + "/s)");
  
  reconnectAsyncBenchmark(callback, cfg);
}

function startBenchmark(callback, cfg) {
  var
    start_time,
    total_time;
  
  start_time = new Date();
  
  conn = new Mysql();
  
  conn.host     = cfg.host;
  conn.user     = cfg.user;
  conn.password = cfg.password;
  conn.database = cfg.database;
  
  conn.connect(function (err, result) {
    conn.query("DROP TABLE IF EXISTS " + cfg.test_table + ";", function () {
      conn.query(cfg.create_table_query, function () {
        total_time = ((new Date()) - start_time) / 1000;
        sys.puts("**** Benchmark initialization time is " + total_time + "s");
        
        escapeBenchmark(callback, cfg);
      });
    });
  });
}

exports.run = function (callback, cfg) {
  startBenchmark(callback, cfg);
};


@@ -1387,32 +1387,709 @@
 nction reconnect
+DestroyAsyncBenchmark(callback, cfg) %7B%0A  var%0A    start_time,%0A    total_time,%0A    i = 0;%0A  %0A    start_time = new Date();%0A    %0A    function reconnectAsync() %7B%0A      i += 1;%0A      if (i %3C= cfg.reconnect_count) %7B%0A        conn.destroy();%0A        conn.connect(function (err, result) %7B%0A          reconnectAsync();%0A        %7D);%0A      %7D else %7B%0A        total_time = ((new Date()) - start_time) / 1000;%0A        sys.puts(%22**** %22 + cfg.reconnect_count + %22 async reconnects with .destroy() in %22 + total_time + %22s (%22 + Math.round(cfg.reconnect_count / total_time) + %22/s)%22);%0A        %0A        insertAsyncBenchmark(callback, cfg);%0A      %7D%0A    %7D%0A    %0A    reconnectAsync();%0A%7D%0A%0Afunction reconnectEnd
 AsyncBenchmark(c
@@ -2548,16 +2548,28 @@
 connects
+ with .end()
  in %22 + 
@@ -2653,38 +2653,48 @@
        %0A        
-insert
+reconnectDestroy
 AsyncBenchmark(c
@@ -3196,24 +3196,27 @@
 %0A  reconnect
+End
 AsyncBenchma

Add different reconnect benchmark with conn.destroy()