-
Notifications
You must be signed in to change notification settings - Fork 21
/
Program.cs
1237 lines (1107 loc) · 51.5 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using Shielded;
using Shielded.ProxyGen;
namespace ConsoleTests
{
public class SimpleEntity
{
public virtual int Id { get; set; }
// by convention, overriden to execute the action as a commutable, non-conflicting operation
public virtual void Commute(Action a) { a(); }
// ...and this one gets called on every change. also called from commutes, in which case trying
// to access any other shielded field will throw.
protected void OnChanged(string property) { }
}
public class SmallEntity
{
public virtual int Id { get; set; }
public virtual int Value { get; set; }
}
class MainClass
{
private static Stopwatch _timer;
public static long mtTest(string name, int taskCount, Func<int, Task> task)
{
if (_timer == null)
_timer = Stopwatch.StartNew();
long time;
Console.Write("Test - {0}...", name);
time = _timer.ElapsedMilliseconds;
Task.WaitAll(
Enumerable.Range(0, taskCount)
.Select(task)
.ToArray());
time = _timer.ElapsedMilliseconds - time;
return time;
}
public static void TimeTests()
{
var randomizr = new Random();
int transactionCounter;
int sleepTime = 1;
int taskCount = 10000;
// a little warm up for Shielded
var warmUp = new Shielded<int>();
Shield.InTransaction(() => warmUp.Value = warmUp + 1);
foreach (var i in Enumerable.Repeat(0, 5))
{
var x = new int[100];
transactionCounter = 0;
var time = mtTest("dirty write", taskCount, _ =>
{
var rnd = randomizr.Next(100);
return Task.Factory.StartNew(() =>
{
Interlocked.Increment(ref transactionCounter);
int v = x[rnd];
if (sleepTime > 0) Thread.Sleep(sleepTime);
x[rnd] = v + 1;
},
sleepTime > 0 ? TaskCreationOptions.LongRunning : TaskCreationOptions.None
);
});
var correct = x.Sum() == taskCount;
Console.WriteLine(" {0} ms with {1} iterations and is {2}.",
time, transactionCounter, correct ? "correct" : "incorrect");
}
var lockCount = 100;
foreach (var i in Enumerable.Repeat(0, 5))
{
var x = new int[100];
transactionCounter = 0;
var l = Enumerable.Repeat(0, lockCount).Select(_ => new object()).ToArray();
var time = mtTest(string.Format("{0} lock write", lockCount), taskCount, _ =>
{
var rnd = randomizr.Next(100);
return Task.Factory.StartNew(() =>
{
lock (l[rnd % lockCount])
{
Interlocked.Increment(ref transactionCounter);
int v = x[rnd];
if (sleepTime > 0) Thread.Sleep(sleepTime);
x[rnd] = v + 1;
}
},
sleepTime > 0 ? TaskCreationOptions.LongRunning : TaskCreationOptions.None
);
});
var correct = x.Sum() == taskCount;
Console.WriteLine(" {0} ms with {1} iterations and is {2}.",
time, transactionCounter, correct ? "correct" : "incorrect");
}
foreach (var i in Enumerable.Repeat(0, 5))
{
var shx = Enumerable.Repeat(0, 100).Select(n => new Shielded<int>(n)).ToArray();
transactionCounter = 0;
var time = mtTest("shielded2 write", taskCount, _ =>
{
var rnd = randomizr.Next(100);
return Task.Factory.StartNew(() =>
{
Shield.InTransaction(() =>
{
Interlocked.Increment(ref transactionCounter);
int v = shx[rnd];
if (sleepTime > 0) Thread.Sleep(sleepTime);
shx[rnd].Value = v + 1;
});
},
sleepTime > 0 ? TaskCreationOptions.LongRunning : TaskCreationOptions.None
);
});
var correct = shx.Sum(s => s.Value) == taskCount;
Console.WriteLine(" {0} ms with {1} iterations and is {2}.",
time, transactionCounter, correct ? "correct" : "incorrect");
}
}
static void ParallelAddWithSaving()
{
var randomizr = new Random();
int transactionCounter;
int taskCount = 1000;
int sleepTime = 1;
int commitEventHit = 0;
SmallEntity[] shx = null;
int[] shxClone = null;
using (Shield.WhenCommitting<SmallEntity>(ents => {
foreach (var ent in ents)
{
Thread.Sleep((ent.Value * Interlocked.Increment(ref commitEventHit) * 31) & 0x3F);
shxClone[ent.Id] = ent.Value;
}
}))
{
foreach (var i in Enumerable.Repeat(0, 10))
{
shxClone = new int[100];
shx = Enumerable.Repeat(0, 100)
.Select((n, ind) => {
var ent = Factory.NewShielded<SmallEntity>();
Shield.InTransaction(() => {
ent.Id = ind;
});
return ent;
}).ToArray();
transactionCounter = 0;
commitEventHit = 0;
var time = mtTest("write with saving", taskCount, _ => {
var rnd = randomizr.Next(100);
return Task.Factory.StartNew(() => Shield.InTransaction(() => {
Interlocked.Increment(ref transactionCounter);
int v = shx[rnd].Value;
if (sleepTime > 0)
Thread.Sleep(sleepTime);
shx[rnd].Value = v + 1;
}), TaskCreationOptions.LongRunning);
});
var correct = shx.Sum(s => s.Value) == taskCount &&
shx.All(s => s.Value == 0 || shxClone[s.Id] == s.Value);
Console.WriteLine(" {0} ms - {1} reps, {2} commits, {3}.",
time, transactionCounter, commitEventHit, correct ? "correct" : "incorrect");
}
}
}
static void OneTransaction()
{
Shielded<int> sh = new Shielded<int>();
Shield.InTransaction(() =>
{
int x = sh;
Console.WriteLine("Value: {0}", x);
sh.Modify((ref int a) => a = x + 1);
Console.WriteLine("Value after increment: {0}", sh.Value);
});
}
struct Account
{
public int Id;
public decimal Balance;
// beware - copies of this struct share this reference!
public List<Transfer> Transfers;
}
struct Transfer
{
public int OtherId;
public decimal AmountReceived;
}
static void ControlledRace()
{
var acc1 = new Shielded<Account>(new Account()
{
Id = 1,
Balance = 1000M,
Transfers = new List<Transfer>()
});
var acc2 = new Shielded<Account>(new Account()
{
Id = 2,
Balance = 1000M,
Transfers = new List<Transfer>()
});
int transactionCount = 0;
mtTest("controlled race", 20, n =>
{
if (n % 2 == 0)
return Task.Factory.StartNew(() =>
{
Shield.InTransaction(() =>
{
Interlocked.Increment(ref transactionCount);
Shield.SideEffect(() => Console.WriteLine("Transferred 100.00 .. acc1 -> acc2"),
() => Console.WriteLine("Task 1 rollback!"));
acc1.Modify((ref Account a) =>
{
a.Balance = a.Balance - 100M;
var list = a.Transfers;
Shield.SideEffect(() => list.Add(
new Transfer() { OtherId = acc2.Value.Id, AmountReceived = -100M }));
});
Thread.Sleep(100);
acc2.Modify((ref Account a) =>
{
a.Balance = a.Balance + 100M;
var list = a.Transfers;
Shield.SideEffect(() => list.Add(
new Transfer() { OtherId = acc1.Value.Id, AmountReceived = 100M }));
});
});
}, TaskCreationOptions.LongRunning);
else
return Task.Factory.StartNew(() =>
{
Shield.InTransaction(() =>
{
Interlocked.Increment(ref transactionCount);
Shield.SideEffect(() => Console.WriteLine("Transferred 200.00 .. acc1 <- acc2"),
() => Console.WriteLine("Task 2 rollback!"));
acc2.Modify((ref Account a) =>
{
a.Balance = a.Balance - 200M;
var list = a.Transfers;
Shield.SideEffect(() => list.Add(
new Transfer() { OtherId = acc1.Value.Id, AmountReceived = -200M }));
});
Thread.Sleep(250);
acc1.Modify((ref Account a) =>
{
a.Balance = a.Balance + 200M;
var list = a.Transfers;
Shield.SideEffect(() => list.Add(
new Transfer() { OtherId = acc2.Value.Id, AmountReceived = 200M }));
});
});
}, TaskCreationOptions.LongRunning);
});
Console.WriteLine("\nCompleted 20 transactions in {0} total attempts.", transactionCount);
Console.WriteLine("Account 1 balance: {0}", acc1.Value.Balance);
foreach (var t in acc1.Value.Transfers)
{
Console.WriteLine(" {0:####,00}", t.AmountReceived);
}
Console.WriteLine("\nAccount 2 balance: {0}", acc2.Value.Balance);
foreach (var t in acc2.Value.Transfers)
{
Console.WriteLine(" {0:####,00}", t.AmountReceived);
}
}
private static void DictionaryTest()
{
var dict = new ShieldedDictNc<int, Shielded<int>>();
var randomizr = new Random();
while (true)
{
var transactionCounter = 0;
var time = mtTest("dictionary", 10000, i =>
{
var rnd = randomizr.Next(10);
if (i % 2 == 0)
// adder task - 500 of these
return Task.Factory.StartNew(() =>
{
Shield.InTransaction(() =>
{
Interlocked.Increment(ref transactionCounter);
var v = dict.ContainsKey(rnd) ? dict[rnd] : null;
int? num = v != null ? (int?)v.Value : null;
Thread.Sleep(1);
if (v == null)
dict[rnd] = new Shielded<int>(1);
else if (v.Value == -1)
dict.Remove(rnd);
else
v.Modify((ref int a) => a = num.Value + 1);
}
);
},
TaskCreationOptions.LongRunning
);
else
// subtractor task - 500 of these
return Task.Factory.StartNew(() =>
{
Shield.InTransaction(() =>
{
Interlocked.Increment(ref transactionCounter);
var v = dict.ContainsKey(rnd) ? dict[rnd] : null;
int? num = v != null ? (int?)v.Value : null;
Thread.Sleep(1);
if (v == null)
dict[rnd] = new Shielded<int>(-1);
else if (v.Value == 1)
dict.Remove(rnd);
else
v.Modify((ref int a) => a = num.Value - 1);
}
);
},
TaskCreationOptions.LongRunning
);
});
var sum = Enumerable.Range(0, 10).Sum(n => dict.ContainsKey(n) ? dict[n] : 0);
var zeroes = Shield.InTransaction(() => dict.Any(kvp => kvp.Value == 0));
Console.WriteLine(" {0} ms with {1} iterations and sum {2}, {3}",
time, transactionCounter, sum, zeroes ? "with zeroes!" : "no zeroes.");
}
}
/// <summary>
/// Creates a BetShop, and tries to buy a large number of random tickets. Afterwards it
/// checks that the rule limiting same ticket winnings is not violated.
/// </summary>
public static void BetShopTest()
{
int numEvents = 100;
var betShop = new BetShop(numEvents);
var randomizr = new Random();
int reportEvery = 10000;
var lastReport = new Shielded<int>(0);
var lastTime = new Shielded<DateTime>(DateTime.UtcNow);
long time;
using (var reportingCond = Shield.Conditional(
() => betShop.Tickets.Count >= lastReport + reportEvery,
() => {
DateTime newNow = DateTime.UtcNow;
int count = betShop.Tickets.Count;
int speed = (count - lastReport) * 1000 / (int)newNow.Subtract(lastTime).TotalMilliseconds;
lastTime.Value = newNow;
lastReport.Modify((ref int n) => n += reportEvery);
Shield.SideEffect(() =>
{
Console.Write("\n{0} at {1} item/s", count, speed);
});
}))
{
time = mtTest("bet shop w/ " + numEvents, 100000, i =>
{
decimal payIn = (randomizr.Next(10) + 1m) * 1;
int event1Id = randomizr.Next(numEvents) + 1;
int event2Id = randomizr.Next(numEvents) + 1;
int event3Id = randomizr.Next(numEvents) + 1;
int offer1Ind = randomizr.Next(3);
int offer2Ind = randomizr.Next(3);
int offer3Ind = randomizr.Next(3);
return Task.Factory.StartNew(() => Shield.InTransaction(() =>
{
var offer1 = betShop.Events[event1Id].BetOffers[offer1Ind];
var offer2 = betShop.Events[event2Id].BetOffers[offer2Ind];
var offer3 = betShop.Events[event3Id].BetOffers[offer3Ind];
betShop.BuyTicket(payIn, offer1, offer2, offer3);
}));
});
}
//}
var totalCorrect = betShop.VerifyTickets();
Console.WriteLine(" {0} ms with {1} tickets paid in and is {2}.",
time, betShop.Tickets.Count, totalCorrect ? "correct" : "incorrect");
}
public static void BetShopPoolTest()
{
int numThreads = 3;
int numTickets = 200000;
int numEvents = 100;
var barrier = new Barrier(2);
var betShop = new BetShop(numEvents);
var randomizr = new Random();
var bags = new List<Action>[numThreads];
var threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++)
{
var bag = bags[i] = new List<Action>();
threads[i] = new Thread(() => {
foreach (var a in bag)
a();
});
}
var complete = new Shielded<int>();
IDisposable completeCond = null;
completeCond = Shield.Conditional(() => complete == numTickets, () => {
barrier.SignalAndWait();
completeCond.Dispose();
});
var reportEvery = 10000;
Shielded<int> lastReport = new Shielded<int>(0);
Shielded<DateTime> lastTime = new Shielded<DateTime>(DateTime.UtcNow);
using (Shield.Conditional(
() => betShop.Tickets.Count >= lastReport + reportEvery,
() => {
DateTime newNow = DateTime.UtcNow;
int count = betShop.Tickets.Count;
int speed = (count - lastReport) * 1000 / (int)newNow.Subtract(lastTime).TotalMilliseconds;
lastTime.Value = newNow;
lastReport.Modify((ref int n) => n += reportEvery);
Shield.SideEffect(() =>
Console.Write("\n{0} at {1} item/s", count, speed));
}))
{
foreach (var i in Enumerable.Range(0, numTickets))
{
decimal payIn = (randomizr.Next(10) + 1m) * 1;
int event1Id = randomizr.Next(numEvents) + 1;
int event2Id = randomizr.Next(numEvents) + 1;
int event3Id = randomizr.Next(numEvents) + 1;
int offer1Ind = randomizr.Next(3);
int offer2Ind = randomizr.Next(3);
int offer3Ind = randomizr.Next(3);
bags[i % numThreads].Add(() => Shield.InTransaction(() => {
var offer1 = betShop.Events[event1Id].BetOffers[offer1Ind];
var offer2 = betShop.Events[event2Id].BetOffers[offer2Ind];
var offer3 = betShop.Events[event3Id].BetOffers[offer3Ind];
betShop.BuyTicket(payIn, offer1, offer2, offer3);
complete.Commute((ref int n) => n++);
}));
}
_timer = Stopwatch.StartNew();
for (int i = 0; i < numThreads; i++)
threads[i].Start();
barrier.SignalAndWait();
}
var time = _timer.ElapsedMilliseconds;
var totalCorrect = betShop.VerifyTickets();
Console.WriteLine(" {0} ms with {1} tickets paid in and is {2}.",
time, betShop.Tickets.Count, totalCorrect ? "correct" : "incorrect");
}
class TreeItem
{
public Guid Id = Guid.NewGuid();
}
public static void DictionaryPoolTest()
{
int numThreads = Environment.ProcessorCount;
int numItems = 1000000;
var tree = new ShieldedDictNc<int, int>();
var barrier = new Barrier(numThreads + 1);
var counter = 0;
int reportEvery = 10000;
var lastReport = 0;
long time;
int x;
var y = new Shielded<int>();
_timer = new Stopwatch();
_timer.Start();
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => { });
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Empty transactions in {0} ms.", time);
var bags = new List<Action>[numThreads];
var threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++)
{
var bag = bags[i] = new List<Action>();
threads[i] = new Thread(() => {
foreach (var a in bag)
{
try
{
a();
}
catch
{
Console.Write(" * ");
}
}
barrier.SignalAndWait();
});
}
var lastTime = _timer.ElapsedMilliseconds;
foreach (var i in Enumerable.Range(0, numItems))
{
var index = i;
bags[i % numThreads].Add(() => Shield.InTransaction(() => {
tree.Add(index, index);
Shield.SideEffect(() => {
var last = lastReport;
var count = Interlocked.Increment(ref counter);
var newNow = _timer.ElapsedMilliseconds;
if (count > last + reportEvery &&
Interlocked.CompareExchange(ref lastReport, last + reportEvery, last) == last)
{
var speed = reportEvery * 1000 / (newNow - lastTime);
lastTime = newNow; // risky, but safe ;)
Console.Write("\n{0} at {1} item/s", last + reportEvery, speed);
}
});
}));
}
lastTime = _timer.ElapsedMilliseconds;
for (int i = 0; i < numThreads; i++)
threads[i].Start();
barrier.SignalAndWait();
time = _timer.ElapsedMilliseconds;
Console.WriteLine("\nTOTAL: {0} ms, at {1} ops/s", time, numItems * 1000 / time);
Console.WriteLine("\nReading sequentially...");
time = _timer.ElapsedMilliseconds;
var keys = Shield.InTransaction(() => tree.Keys);
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Keys read in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
Shield.InTransaction(() => {
foreach (var kvp in tree)
x = kvp.Value;
});
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Items read by enumerator in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
Shield.InTransaction(() => {
foreach (var kvp in tree.OrderBy(kvp => kvp.Key))
x = kvp.Value;
});
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Items read by sorted enumerator in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
Shield.InTransaction(() => {
foreach (var k in keys)
x = tree[k];
});
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Items read by key in one trans in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
foreach (var k in keys)
x = tree[k];
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Items read by key separately in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
keys.AsParallel().ForAll(k => x = tree[k]);
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Items read by key in parallel in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
{
var a = y.Value;
}
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("One field out-of-tr. reads in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => { });
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("Empty transactions in {0} ms.", time);
}
static long Timed(string name, int numRepeats, Action act)
{
var time = _timer.ElapsedMilliseconds;
act();
time = _timer.ElapsedMilliseconds - time;
var front = string.Format("{0} in {1} ms, ", name, time);
Console.WriteLine("{0}{2}cost {1} us per rep.", front, time * 1000.0 / numRepeats,
string.Join(string.Empty, Enumerable.Repeat("\t", Math.Max(0, 5 - (front.Length / 8)))));
return time;
}
static void SimpleOps()
{
_timer = Stopwatch.StartNew();
var numItems = 1000000;
var repeatsPerTrans = 50;
Console.WriteLine(
"Testing simple ops with {0} iterations, and repeats per trans (N) = {1}",
numItems, repeatsPerTrans);
var accessTest = new Shielded<int>();
Timed("WARM UP", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
accessTest.Value = 3;
var a = accessTest.Value;
accessTest.Modify((ref int n) => n = 5);
a = accessTest.Value;
});
});
var emptyTime = Timed("empty transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => { });
});
var emptyReturningTime = Timed("1 empty transaction w/ result", numItems, () => {
// this version uses the generic, result-returning InTransaction, which involves creation
// of a closure, i.e. an allocation.
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => 5);
});
var outOfTrReadTime = Timed("N out-of-tr. reads", numItems, () => {
// this version uses the generic, result-returning InTransaction, which involves creation
// of a closure, i.e. an allocation.
foreach (var k in Enumerable.Repeat(1, numItems * repeatsPerTrans))
{
var a = accessTest.Value;
}
});
// the purpose here is to get a better picture of the expense of using Shielded. a more
// complex project would probably, during one transaction, repeatedly access the same
// field. does this cost much more than a single-access transaction? if it is the same
// field, then any significant extra expense is unacceptable.
var oneReadTime = Timed("1-read transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
var a = accessTest.Value;
});
});
var nReadTime = Timed("N-reads transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
int a;
for (int i = 0; i < repeatsPerTrans; i++)
a = accessTest.Value;
});
});
var oneReadModifyTime = Timed("1-read-1-modify tr.", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
var a = accessTest.Value;
accessTest.Modify((ref int n) => n = 1);
});
});
// Assign is no longer commutable, for performance reasons. It is faster,
// particularly when repeated (almost 10 times), and you can see the difference
// that not reading the old value does.
var oneReadAssignTime = Timed("1-read-1-assign tr.", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
var a = accessTest.Value;
accessTest.Value = 1;
});
});
var oneModifyTime = Timed("1-modify transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => accessTest.Modify((ref int n) => n = 1));
});
var nModifyTime = Timed("N-modify transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
for (int i = 0; i < repeatsPerTrans; i++)
accessTest.Modify((ref int n) => n = 1);
});
});
var accessTest2 = new Shielded<int>();
var modifyModifyTime = Timed("modify-modify transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
accessTest.Modify((ref int n) => n = 1);
accessTest2.Modify((ref int n) => n = 2);
});
});
// here Modify is the first call, making all Reads as fast as can be,
// reading direct from local storage.
var oneModifyNReadTime = Timed("1-modify-N-reads tr.", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
accessTest.Modify((ref int n) => n = 1);
int a;
for (int i = 0; i < repeatsPerTrans; i++)
a = accessTest.Value;
});
});
var oneAssignTime = Timed("1-assign transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => accessTest.Value = 1);
});
var nAssignTime = Timed("N-assigns transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
for (int i = 0; i < repeatsPerTrans; i++)
accessTest.Value = 1;
});
});
var oneCommuteTime = Timed("1-commute transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => accessTest.Commute((ref int n) => n = 1));
});
var nCommuteTime = Timed("N-commute transactions", numItems, () => {
foreach (var k in Enumerable.Repeat(1, numItems/10))
Shield.InTransaction(() => {
for (int i = 0; i < repeatsPerTrans; i++)
accessTest.Commute((ref int n) => n = 1);
});
});
Console.WriteLine("\ncost of empty transaction = {0:0.000} us", emptyTime / (numItems / 1000.0));
Console.WriteLine("cost of the closure in InTransaction<T> = {0:0.000} us",
(emptyReturningTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an out-of-tr. read = {0:0.000} us",
outOfTrReadTime * 1000.0 / (numItems * repeatsPerTrans));
Console.WriteLine("cost of the first read = {0:0.000} us",
(oneReadTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional read = {0:0.000} us",
(nReadTime - oneReadTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
Console.WriteLine("cost of Modify after read = {0:0.000} us",
(oneReadModifyTime - oneReadTime) / (numItems / 1000.0));
Console.WriteLine("cost of Assign after read = {0:0.000} us",
(oneReadAssignTime - oneReadTime) / (numItems / 1000.0));
Console.WriteLine("cost of the first Modify = {0:0.000} us",
(oneModifyTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional Modify = {0:0.000} us",
(nModifyTime - oneModifyTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
Console.WriteLine("cost of a second, different Modify = {0:0.000} us",
(modifyModifyTime - oneModifyTime) / (numItems / 1000.0));
Console.WriteLine("cost of a read after Modify = {0:0.000} us",
(oneModifyNReadTime - oneModifyTime) / (repeatsPerTrans * numItems / 1000.0));
Console.WriteLine("cost of the first Assign = {0:0.000} us",
(oneAssignTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional Assign = {0:0.000} us",
(nAssignTime - oneAssignTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
Console.WriteLine("cost of the first commute = {0:0.000} us",
(oneCommuteTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional commute = {0:0.000} us",
(nCommuteTime*10 - oneCommuteTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
}
public static void SimpleProxyOps()
{
var entity = Factory.NewShielded<SimpleEntity>();
long time;
_timer = Stopwatch.StartNew();
var numItems = 1000000;
var repeatsPerTrans = 50;
Console.WriteLine(
"Testing simple proxy ops with {0} iterations, and repeats per trans (N) = {1}",
numItems, repeatsPerTrans);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
var a = entity.Id;
entity.Id = 3;
});
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("WARM UP in {0} ms.", time);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => { });
var emptyTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("empty transactions in {0} ms.", emptyTime);
// this version uses the generic, result-returning InTransaction, which involves creation
// of a closure, i.e. an allocation.
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => 5);
var emptyReturningTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1 non-transactional read w/ returning result in {0} ms.", emptyReturningTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => { var a = entity.Id; });
var oneReadTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1-read transactions in {0} ms.", oneReadTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems * repeatsPerTrans))
{
var a = entity.Id;
}
var nOutOfTrReadTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("N out-of-tr. reads in {0} ms.", nOutOfTrReadTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
int a;
for (int i = 0; i < repeatsPerTrans; i++)
a = entity.Id;
});
var nReadTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("N-reads transactions in {0} ms.", nReadTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
var a = entity.Id;
entity.Id = 1;
});
var oneReadWriteTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1-read-1-write transactions in {0} ms.", oneReadWriteTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => entity.Id = 1);
var oneWriteTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1-write transactions in {0} ms.", oneWriteTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
for (int i = 0; i < repeatsPerTrans; i++)
entity.Id = 1;
});
var nWriteTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("N-write transactions in {0} ms.", nWriteTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => {
entity.Id = 2;
int a;
for (int i = 0; i < repeatsPerTrans; i++)
a = entity.Id;
});
var oneWriteNReadTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1-write-N-reads transactions in {0} ms.", oneWriteNReadTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems))
Shield.InTransaction(() => entity.Commute(() => entity.Id = 1));
var oneCommuteTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("1-commute transactions in {0} ms.", oneCommuteTime);
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numItems/10))
Shield.InTransaction(() => {
for (int i = 0; i < repeatsPerTrans; i++)
entity.Commute(() => entity.Id = 1);
});
var nCommuteTime = _timer.ElapsedMilliseconds - time;
Console.WriteLine("N-commute transactions in {0} ms.", nCommuteTime);
Console.WriteLine("\ncost of empty transaction = {0:0.000} us", emptyTime / (numItems / 1000.0));
Console.WriteLine("cost of the closure in InTransaction<T> = {0:0.000} us",
(emptyReturningTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an out-of-tr. read = {0:0.000} us",
nOutOfTrReadTime * 1000.0 / (numItems * repeatsPerTrans));
Console.WriteLine("cost of the first read = {0:0.000} us",
(oneReadTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional read = {0:0.000} us",
(nReadTime - oneReadTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
Console.WriteLine("cost of write after read = {0:0.000} us",
(oneReadWriteTime - oneReadTime) / (numItems / 1000.0));
Console.WriteLine("cost of the first write = {0:0.000} us",
(oneWriteTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional write = {0:0.000} us",
(nWriteTime - oneWriteTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
Console.WriteLine("cost of a read after write = {0:0.000} us",
(oneWriteNReadTime - oneWriteTime) / (repeatsPerTrans * numItems / 1000.0));
Console.WriteLine("cost of the first commute = {0:0.000} us",
(oneCommuteTime - emptyTime) / (numItems / 1000.0));
Console.WriteLine("cost of an additional commute = {0:0.000} us",
(nCommuteTime*10 - oneCommuteTime) / ((repeatsPerTrans - 1) * numItems / 1000.0));
}
public static void MultiFieldOps()
{
long time;
_timer = Stopwatch.StartNew();
var numTrans = 100000;
var fields = 20;
Console.WriteLine(
"Testing multi-field ops with {0} iterations, and nuber of fields (N) = {1}",
numTrans, fields);
var accessTest = new Shielded<int>[fields];
for (int i = 0; i < fields; i++)
accessTest[i] = new Shielded<int>();
var dummy = new Shielded<int>();
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numTrans))
Shield.InTransaction(() => {
dummy.Value = 3;
var a = dummy.Value;
dummy.Modify((ref int n) => n = 5);
a = dummy.Value;
});
time = _timer.ElapsedMilliseconds - time;
Console.WriteLine("WARM UP in {0} ms.", time);
var results = new long[fields];
foreach (var i in Enumerable.Range(0, fields))
{
time = _timer.ElapsedMilliseconds;
foreach (var k in Enumerable.Repeat(1, numTrans))
Shield.InTransaction(() => {
for (int j = 0; j <= i; j++)
accessTest[j].Modify((ref int n) => n = 1);
});
results[i] = _timer.ElapsedMilliseconds - time;
Console.WriteLine("{0} field modifiers in {1} ms.", i + 1, results[i]);
}
}
public static void TreeTest()
{
int numTasks = 100000;
var tree = new ShieldedTreeNc<Guid, TreeItem>();
int transactionCount = 0;
Shielded<int> lastReport = new Shielded<int>(0);
Shielded<int> countComplete = new Shielded<int>(0);
if (true)
{
var treeTime = mtTest("tree", numTasks, i =>
{
return Task.Factory.StartNew(() =>
{
var item1 = new TreeItem();
Shield.InTransaction(() =>
{
//Interlocked.Increment(ref transactionCount);
tree.Add(item1.Id, item1);
// countComplete.Commute((ref int c) => c++);
}
);