-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataGenerator.cs
260 lines (213 loc) · 9.25 KB
/
DataGenerator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using DocuTest.Shared.Enums;
using System.Data.SqlClient;
namespace DocuTest.Data.Main.DAL.Generators;
public static class DataGenerator
{
private static Random random = new Random();
private static Sequential sequential = new Sequential();
private static IList<(Guid Id, string Name)> documentTypes = new List<(Guid Id, string Name)>()
{
(new Guid(DocumentType.Mail), "Mail"),
(new Guid(DocumentType.CV), "CV"),
(new Guid(DocumentType.Invoice), "Invoice"),
(new Guid(DocumentType.Other), "Other")
};
private static IList<(Guid Id, string Name, string Email)> users = new List<(Guid Id, string Name, string Email)>()
{
(new Guid("A1B801AC-A916-4096-86A1-A879B369D41F"), "User", "someone@example.com"),
(new Guid("39773585-d12d-47ab-8ad3-c0cd003815a9"), "Manager", "manager@example.com"),
(new Guid("971aa795-c31e-4545-b4e3-d13a884580ab"), "Admin", "admin@example.com"),
(new Guid("bcc088b2-fb80-4911-81d7-3b16d512c73d"), "CEO", "ceo@example.com")
};
public static async Task Generate(
string connectionString,
int totalDocuments = 50_000_000,
int batchSize = 1000,
int minFilesCount = 1,
int maxFilesCount = 5,
int minMetadataCount = 0,
int maxMetadataCount = 5,
int threadsPerCore = 2)
{
sequential.Batch = 0;
int batchCount = totalDocuments / batchSize;
int maxDegreeOfParallelism = Environment.ProcessorCount * threadsPerCore;
Queue<SqlConnection> connectionPool = UniqueConnections(connectionString, maxDegreeOfParallelism);
try
{
await InsertUsers(connectionPool.First());
await InsertDocumentTypes(connectionPool.First());
Parallel.For(sequential.Batch, batchCount, new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism }, async _ =>
{
Guid userId = users[random.Next(0, users.Count)].Id;
SqlConnection connection = AcquireConnection(connectionPool);
using (SqlTransaction transaction = connection.BeginTransaction())
{
try
{
for (int batchItem = 0; batchItem < batchSize; batchItem++)
{
Guid documentId = Guid.NewGuid();
await InsertDocument(transaction, documentId, userId, $"Document {sequential.Batch * batchSize + batchItem}");
foreach (Guid fileId in Enumerable.Range(minFilesCount, random.Next(minFilesCount, maxFilesCount)).Select(_ => Guid.NewGuid()))
{
await InsertFile(transaction, fileId);
await InsertDocumentFile(transaction, documentId, fileId);
for (int k = minMetadataCount; k < random.Next(minMetadataCount, maxMetadataCount); k++)
await InsertMetadata(transaction, fileId, $"Metadata {k}");
}
}
transaction.Commit();
ReleaseConnection(connectionPool, connection);
}
catch (Exception ex)
{
transaction.Commit();
ReleaseConnection(connectionPool, connection);
Console.WriteLine($"Error occurred during batch {sequential.Batch + 1}: {ex.Message}");
}
}
Console.WriteLine($"Completed batch {sequential.Batch + 1}/{batchCount}");
});
}
catch (Exception ex)
{
Console.WriteLine($"An unspecified error occured: {ex.Message}");
throw;
}
finally
{
foreach(SqlConnection connection in connectionPool)
connection.Close();
}
}
private static Queue<SqlConnection> UniqueConnections(string connectionString, int maxDegreeOfParallelism)
{
Queue<SqlConnection> connections = new Queue<SqlConnection>(maxDegreeOfParallelism);
for (int i = 0; i <= maxDegreeOfParallelism; i++)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
connections.Enqueue(connection);
}
return connections;
}
private static SqlConnection AcquireConnection(Queue<SqlConnection> connections)
{
do
{
lock (connections)
{
if (connections.Any())
return connections.Dequeue();
}
}
while (true);
}
private static void ReleaseConnection(Queue<SqlConnection> connections, SqlConnection connection)
{
lock (connections)
{
connections.Enqueue(connection);
}
lock (sequential)
{
sequential.Batch++;
}
}
private static async Task InsertFile(SqlTransaction transaction, Guid fileId)
{
using (SqlCommand command = transaction.Connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandText = @"
INSERT INTO [dbo].[File] ([Id], [Name], [Extension], [Content])
VALUES (@Id, @Name, @Extension, @Content)";
command.Parameters.AddWithValue("@Id", fileId);
command.Parameters.AddWithValue("@Name", fileId);
command.Parameters.AddWithValue("@Extension", "txt");
command.Parameters.AddWithValue("@Content", 1024);
await command.ExecuteNonQueryAsync();
}
}
private static async Task InsertUsers(SqlConnection connection)
{
foreach ((Guid Id, string Name, string Email) user in users)
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = @"
IF NOT EXISTS (SELECT 1 FROM [dbo].[User] WHERE [Id] = @Id)
INSERT INTO [dbo].[User] ([Id], [Name], [Email])
VALUES (@Id, @Name, @Email)";
command.Parameters.AddWithValue("@Id", user.Id);
command.Parameters.AddWithValue("@Name", user.Name);
command.Parameters.AddWithValue("@Email", user.Email);
await command.ExecuteNonQueryAsync();
}
}
}
private static async Task InsertDocumentTypes(SqlConnection connection)
{
foreach ((Guid Id, string Name) type in documentTypes)
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = @"
IF NOT EXISTS (SELECT 1 FROM [dbo].[DocumentType] WHERE [Id] = @Id)
INSERT INTO [dbo].[DocumentType] ([Id], [Name])
VALUES (@Id, @Name)";
command.Parameters.AddWithValue("@Id", type.Id);
command.Parameters.AddWithValue("@Name", type.Name);
await command.ExecuteNonQueryAsync();
}
}
}
private static async Task InsertDocument(SqlTransaction transaction, Guid documentId, Guid userId, string name)
{
Guid documentTypeId = documentTypes[random.Next(0, documentTypes.Count)].Id;
using (SqlCommand command = transaction.Connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandText = @"
INSERT INTO [dbo].[Document] ([Id], [Name], [DocumentTypeId], [UserId])
VALUES (@Id, @Name, @DocumentTypeId, @UserId)";
command.Parameters.AddWithValue("@Id", documentId);
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@DocumentTypeId", documentTypeId);
command.Parameters.AddWithValue("@UserId", userId);
await command.ExecuteNonQueryAsync();
}
}
private static async Task InsertDocumentFile(SqlTransaction transaction, Guid documentId, Guid fileId)
{
using (SqlCommand command = transaction.Connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandText = @"
INSERT INTO [dbo].[DocumentFile] ([DocumentId], [FileId])
VALUES (@DocumentId, @FileId)";
command.Parameters.AddWithValue("@DocumentId", documentId);
command.Parameters.AddWithValue("@FileId", fileId);
await command.ExecuteNonQueryAsync();
}
}
private static async Task InsertMetadata(SqlTransaction transaction, Guid fileId, string key)
{
using (SqlCommand command = transaction.Connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandText = @"
INSERT INTO [dbo].[Metadata] ([FileId], [Key], [Value])
VALUES (@FileId, @Key, @Value)";
command.Parameters.AddWithValue("@FileId", fileId);
command.Parameters.AddWithValue("@Key", key);
command.Parameters.AddWithValue("@Value", Guid.NewGuid());
await command.ExecuteNonQueryAsync();
}
}
}
internal class Sequential
{
internal int Batch;
}