This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
113 lines (96 loc) · 3.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace BulkInsertInvestigation
{
class Program
{
private static Random random = new Random();
private static string connectionString = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
static void Main(string[] args)
{
Console.WriteLine("Preparing data records");
int batchSize = 10000;
var dataRecords = CreateDataRecords(100000);
IEnumerable<Type> inserterTypes = GetInserterTypes();
foreach (var inserterType in inserterTypes)
{
try
{
RunTest(dataRecords, batchSize, inserterType);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
}
Console.WriteLine();
}
private static void RunTest(IList<Customer> dataRecords, int batchSize, Type inserterType)
{
Console.WriteLine("Inserting {0} records in {1} batch size via {2}", dataRecords.Count, batchSize, inserterType);
var inserter = (IInserter) Activator.CreateInstance(inserterType, connectionString, batchSize);
var stopWatch = Stopwatch.StartNew();
inserter.Insert(dataRecords);
stopWatch.Stop();
Console.WriteLine("{0}: Done in {1} seconds", inserterType, stopWatch.Elapsed.TotalSeconds);
TruncateTable();
}
private static void TruncateTable()
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "TRUNCATE TABLE dbo.Customers";
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
Console.WriteLine("Customers table truncated");
}
}
private static IEnumerable<Type> GetInserterTypes()
{
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(IInserter).IsAssignableFrom(type) && type.IsClass);
}
private static IList<Customer> CreateDataRecords(int count)
{
var result = new List<Customer>(count);
for (int i = 0; i < count; i++)
{
result.Add(GenerateDataRecord());
}
return result;
}
private static Customer GenerateBadDataRecord()
{
return new Customer
{
Address = null,
Email = null,
Name = null
};
}
private static Customer GenerateDataRecord()
{
return new Customer
{
Address = string.Format("Amosova str, {0}", random.Next(100)),
Email = string.Format("{0}@email.com", Guid.NewGuid()),
IsActive = random.NextDouble() > 0.5,
Longitude = random.NextDouble(),
Latitude = random.NextDouble(),
Name = "John Smith"
};
}
}
}