Advertisement
pietrovich

Untitled

Sep 24th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. class Inserter
  2. {
  3.     private readonly MySqlConnection _connection;
  4.  
  5.     public Inserter()
  6.     {
  7.         MySqlConnectionStringBuilder sb = new MySqlConnectionStringBuilder();
  8.         sb.AllowBatch = true;
  9.         sb.Database = "playground";
  10.         sb.Server = "127.0.0.1";
  11.         sb.UserID = "qwe";
  12.         sb.Password = "qweqwe";
  13.         sb.UseAffectedRows = false;
  14.         sb.Pooling = true;
  15.  
  16.         _connection = new MySqlConnection(sb.ConnectionString);
  17.     }
  18.  
  19.     private MySqlConnection Connection {
  20.         get {
  21.             if (_connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken) _connection.Open();
  22.             return _connection;
  23.         }
  24.     }
  25.  
  26.     string stub;
  27.     private string GetRandomString(int len, int seed)
  28.     {
  29.         if (null != stub) return stub;  //генерить рандомные строки дело затратное,
  30.                         //поэтому для тестирования вставок обойдемся одной. но ежели раскоменить здесь и (2),
  31.                         //то можно и "уникальные" сроки вставлять
  32.         StringBuilder sb = new StringBuilder();
  33.         byte[] ba = new byte[len];
  34.         Random r = new Random(DateTime.Now.Ticks);
  35.         for (int i = 0; i < len; i++)
  36.         {
  37.             r.NextBytes(ba);
  38.             sb.Append((char)(65 + ba[i] % 25));
  39.         }
  40.         stub = sb.ToString(); // (2)
  41.         return sb.ToString();
  42.     }
  43.  
  44.     public TimeSpan PerformInsert(int count, int len, bool fake)
  45.     {
  46.         DateTime start = DateTime.Now;
  47.         for (int i = 0; i < count; i++)
  48.         {
  49.             string s = GetRandomString(len, i);
  50.             //Console.WriteLine(s);
  51.             if (!fake) Insert(s);
  52.         }
  53.  
  54.         return DateTime.Now.Subtract(start);
  55.     }
  56.  
  57.     private void Insert(string s)
  58.     {
  59.         MySqlCommand c = new MySqlCommand();
  60.         c.Connection = Connection;
  61.         c.Transaction = Connection.BeginTransaction(); //комментим здесь и (3) чтобы отключить транзакции
  62.         c.CommandText = "INSERT into `habr` set `col` = @col";
  63.         c.Parameters.AddWithValue("@col", s);
  64.         c.ExecuteNonQuery();
  65.         c.Transaction.Commit(); // (3) с транзакциями помедленнее, примерно 3сек/1000
  66.         c.Connection.Close();
  67.     }
  68.  
  69.     public bool IsReady()
  70.     {
  71.         try
  72.         {
  73.             MySqlCommand c = new MySqlCommand();
  74.             c.Connection = Connection;
  75.             c.CommandText = "Select 1;";
  76.             object o = c.ExecuteScalar();
  77.             return 1 == Convert.ToInt32(o);
  78.         }
  79.         catch (Exception e)
  80.         {
  81.             Console.WriteLine(e);
  82.         }
  83.         return false;
  84.     }
  85. }
  86.  
  87. /* USAGE example: */
  88. //  Inserter i = new Inserter();
  89. //  if (i.IsReady())
  90. //  {
  91. //      Console.WriteLine("ready");
  92. //      TimeSpan ts= i.PerformInsert(1000, 2000, false);
  93. //      Console.WriteLine(ts.ToString());
  94. //  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement