-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadSet.cs
More file actions
268 lines (220 loc) · 7.46 KB
/
Copy pathThreadSet.cs
File metadata and controls
268 lines (220 loc) · 7.46 KB
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
261
262
263
264
265
266
267
268
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NFX;
using NFX.Parsing;
namespace UserGraph
{
public class ThreadSet : DisposableObject
{
public ThreadSet(IUserGraph graph)
{
m_Graph = graph;
m_Log = new ConcurrentQueue<string>();
m_List = new List<Thread>();
}
protected override void Destructor()
{
base.Destructor();
foreach (var t in m_List) t.Join();
}
private IUserGraph m_Graph;
private int m_UserCount;
private int m_PostReads;
private int m_PostWrites;
private int m_PostVotes;
private int m_PostDeletes;
private ConcurrentQueue<string> m_Log;
private List<Thread> m_List;
private long m_stat_ReadHit;
private long m_stat_ReadMiss;
private long m_stat_Write;
private long m_stat_DeleteHit;
private long m_stat_DeleteMiss;
public int Count { get { return m_List.Count; } }
public string TryGetLog()
{
string result;
if (!m_Log.TryDequeue(out result)) return null;
return result;
}
public void Log(string message)
{
if (m_Log.Count > 1000) return;
m_Log.Enqueue(message);
}
public void Stats(out long rHit,
out long rMiss,
out long dHit,
out long dMiss,
out long write
)
{
rHit = Interlocked.Exchange(ref m_stat_ReadHit, 0);
rMiss = Interlocked.Exchange(ref m_stat_ReadMiss, 0);
dHit = Interlocked.Exchange(ref m_stat_DeleteHit, 0);
dMiss = Interlocked.Exchange(ref m_stat_DeleteMiss, 0);
write = Interlocked.Exchange(ref m_stat_Write, 0);
}
public void Set(int threads, int userCount, int postR, int postW, int postV, int postD)
{
m_UserCount = userCount;
m_PostReads = postR;
m_PostWrites = postW;
m_PostVotes = postV;
m_PostDeletes = postD;
lock (m_List)
{
while (m_List.Count > threads) m_List.RemoveAt(0);
while (m_List.Count < threads)
{
var t = new Thread(threadSpin);
t.IsBackground = true;
m_List.Add(t);
t.Start(m_List.Count);
}
}
}
private void threadSpin(object thread)
{
Log("{0} {1} Thread Started".Args(m_Graph.GetType().Name, App.TimeSource.Now));
var threadNumber = (int)thread;
var cnt = 0;
while(!DisposeStarted)
{
if (cnt > 200)
{
cnt = 0;
lock (m_List)
{
if (m_List.Count < threadNumber) break;
}
}
else cnt++;
//var zzzuid = m_Graph.NewUserID();
//var zzzuser = makeUser(zzzuid);
//m_Graph.PutUser(zzzuser);
//continue;
try
{
var existingUserCount = m_Graph.UserCount;
var existingPostCount = m_Graph.PostCount;
if (existingUserCount < m_UserCount)//create new users
{
var uid = m_Graph.NewUserID();
var user = makeUser(uid);
m_Graph.PutUser(user);
}
else if (existingUserCount > m_UserCount)//delete
{
var delete = existingUserCount - m_UserCount;
var toDelete = delete - ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)(delete * 0.2d));
for (var i = 0; i < toDelete; i++)//delete users
{
var uid = getRandomUserID();
m_Graph.RemoveUser(uid);
}
}
if (m_PostWrites>0)
{
var toWrite = m_PostWrites - ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)(m_PostWrites * 0.25d));
for (var i = 0; i < toWrite; i++)//write posts
{
var uid = getRandomUserID();
var pid = m_Graph.NewPostID();
var post = makePost(uid, pid, existingUserCount);
m_Graph.PutPost(post);
}
}
if (m_PostDeletes > 0)
{
var toDelete = m_PostDeletes - ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)(m_PostDeletes * 0.5d));
for (var i = 0; i < toDelete; i++)//delete posts
{
var pid = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)existingPostCount);
m_Graph.RemovePost(pid);
}
}
if (m_PostVotes > 0)
{
var toVote = m_PostVotes - ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)(m_PostVotes * 0.5d));
for (var i = 0; i < toVote; i++)//delete posts
{
var pid = getRandomPostID();
m_Graph.VotePost(pid, ExternalRandomGenerator.Instance.NextScaledRandomInteger(-5, +5));
}
}
if (m_PostReads > 0)
{
var toRead = m_PostReads - ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)(m_PostReads * 0.5d));
for (var i = 0; i < toRead; i++)//delete posts
{
var uid = getRandomUserID();
User user;
m_Graph.GetUserPosts(uid, out user);
}
}
//randomize
//Thread.SpinWait(ExternalRandomGenerator.Instance.NextScaledRandomInteger(10, 1000));
// if (ExternalRandomGenerator.Instance.NextRandomInteger>1500000000)
// Thread.Sleep(ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 50));
}
catch(Exception error)
{
Log("{0} {1} Thread Exception: {2}".Args(m_Graph.GetType().Name, App.TimeSource.Now, error.ToMessageWithType()));
}
}
Log("{0} {1} Thread Finished".Args(m_Graph.GetType().Name, App.TimeSource.Now));
}
private long getRandomUserID()
{
return (long)(ExternalRandomGenerator.Instance.NextRandomDouble * m_Graph.UserIDRange);
}
private long getRandomPostID()
{
return (long)(ExternalRandomGenerator.Instance.NextRandomDouble * m_Graph.PostIDRange);
}
private User makeUser(long id)
{
var chance = ExternalRandomGenerator.Instance.NextRandomInteger;
var now = App.TimeSource.UTCNow;
var user = new User
{
ID = id,
Name = "User Joe "+id.ToString(),
RegistrationDate = now,
LastLoginDate = now,
EMail = id.ToString()+"mydomain.com",
CanVote = (chance & 1) == 0
};
if (chance > 500000000)
user.Location = NaturalTextGenerator.GenerateCityName();
return user;
}
private Post makePost(long uid, long pid, long existingUserCout)
{
var chance = ExternalRandomGenerator.Instance.NextRandomInteger;
var now = App.TimeSource.UTCNow;
var post = new Post
{
PostID = pid,
UserID = uid,
CreateDate = now,
Title = "aaaaaaaaaaaaaaaaaaaaa",//NaturalTextGenerator.Generate(128),
Text = "werwqruwqoeruowpifuopiusodiufsaoufoisuafuposaufoisuf"//NaturalTextGenerator.Generate( ExternalRandomGenerator.Instance.NextScaledRandomInteger(12, 256) ),
};
if (chance > 1500000000)
{
var mcnt = chance % 6;
post.MentionUserIDs = new long[mcnt];
for (var i = 0; i < mcnt; i++)
post.MentionUserIDs[i] = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, (int)existingUserCout);
}
return post;
}
}
}