You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/labs/doris/DorisTest/Program.cs

103 lines
3.3 KiB

using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace DorisTest
{
public class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("Server=localhost;Port=9030;Database=test;Uid=root;Allow User Variables=True;");
}
}
internal class Program
{
private static void Main(string[] args)
{
//using var db = new TestContext();
//create database test;
//use test;
//CREATE TABLE test
//(
// orderid VARCHAR(32),
// username VARCHAR(32),
// amount BIGINT DEFAULT '0'
//)
//UNIQUE KEY(orderid)
//distributed by hash(orderid) buckets 1
//properties(
// "replication_num" = "1"
//);
for (int i = 0; i < 32; i++)
{
var b = i + 1;
Task.Run(() => UpdateDB(b, 100000));
}
while (true)
{
Thread.Sleep(15 * 1000);
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
private static void UpdateDB(int b, int num)
{
var watch = new Stopwatch();
var conn = CreateConnection();
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandTimeout = 60;
watch.Start();
for (int i = 0; i < num; i++)
{
cmd.CommandText = $"insert into test values('{Guid.NewGuid().ToString().Replace("-", "")}','{GetChar()}{GetChar()}{GetChar()}',{GetNumber()});";
try
{
cmd.ExecuteNonQuery();
if (i % 10 == 0 || i == num - 1)
{
watch.Stop();
Console.WriteLine(b + ":" + watch.ElapsedMilliseconds + ":" + i.ToString());
watch = new Stopwatch();
watch.Start();
}
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"error on thread {b}. {ex.GetType().FullName} :{ex.Message}");
conn.Open();
cmd = conn.CreateCommand();
}
catch (Exception ex)
{
Console.WriteLine($"error on thread {b}. {ex.GetType().FullName} :{ex.Message}");
conn = CreateConnection();
conn.Open();
cmd = conn.CreateCommand();
}
}
cmd.Dispose();
conn.Close();
}
private static MySqlConnection CreateConnection()
{
return new MySqlConnection("Server=localhost;Port=9030;Database=test;Uid=root;Allow User Variables=True;");
}
private static char GetChar()
{
return (char)new Random((int)DateTime.UtcNow.Ticks).Next(97, 122);
}
private static int GetNumber()
{
return new Random((int)DateTime.UtcNow.Ticks).Next(1, 1000);
}
}
}