annotate MonoPlay/Program.cs @ 144:8c0b95069066 v2

DRAFT: refactoring
author cin
date Fri, 06 Mar 2015 15:45:26 +0300
parents f75cfa58e3d4
children 706fccb85524
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
1 using System;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
2 using Implab.Diagnostics;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
3 using Implab.Parallels;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
4 using Implab;
103
cin
parents: 94
diff changeset
5 using System.Collections.Generic;
cin
parents: 94
diff changeset
6 using System.Collections.Concurrent;
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
7 using System.Threading;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
8
dc4942d09e74 improved tracing
cin
parents:
diff changeset
9 namespace MonoPlay {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
10 class MainClass {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
11 public static void Main(string[] args) {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
12 if (args == null)
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
13 throw new ArgumentNullException("args");
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
14
103
cin
parents: 94
diff changeset
15 var t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
16
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
17 const int reads = 100000;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
18 const int writes = 1000;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
19 const int readThreads = 8;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
20 const int writeThreads = 0;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
21
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
22 var l = new SharedLock();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
23 var st = new HashSet<int>();
125
f803565868a4 improved performance of promises
cin
parents: 124
diff changeset
24
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
25 Action reader1 = () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
26 for (int i =0; i < reads; i++) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
27 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
28 l.LockShared();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
29 st.Contains(i % 1000);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
30 Thread.Sleep(0);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
31 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
32 l.Release();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
33 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
34 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
35 };
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
36
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
37 Action reader2 = () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
38 for(var i = 0; i < reads; i++)
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
39 lock(st) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
40 st.Contains(i % 1000);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
41 Thread.Sleep(0);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
42 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
43 };
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
44
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
45 Action writer1 = () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
46 var rnd = new Random(Environment.TickCount);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
47 for (int i = 0; i < writes; i++) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
48 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
49 l.LockExclusive();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
50 st.Add(rnd.Next(1000));
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
51 //Thread.Sleep(1);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
52 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
53 l.Release();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
54 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
55 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
56 };
125
f803565868a4 improved performance of promises
cin
parents: 124
diff changeset
57
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
58 Action writer2 = () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
59 var rnd = new Random(Environment.TickCount);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
60 for (int i = 0; i < writes; i++) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
61 lock (st) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
62 st.Add(rnd.Next(1000));
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
63 //Thread.Sleep(1);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
64 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
65 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
66 };
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
67
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
68
125
f803565868a4 improved performance of promises
cin
parents: 124
diff changeset
69
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
70 var readers = new IPromise[readThreads];
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
71 for (int i = 0; i < readThreads; i++)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
72 readers[i] = AsyncPool.RunThread(reader2);
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
73
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
74 var writers = new IPromise[writeThreads];
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
75 for (int i = 0; i < writeThreads; i++)
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
76 writers[i] = AsyncPool.RunThread(writer1);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
77
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
78
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
79 new [] {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
80 readers.Bundle().On(() => Console.WriteLine("readers complete in {0} ms", Environment.TickCount - t1)),
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
81 writers.Bundle().On(() => Console.WriteLine("writers complete in {0} ms", Environment.TickCount - t1))
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
82 }.Bundle().Join();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
83
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
84
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
85
103
cin
parents: 94
diff changeset
86 var t2 = Environment.TickCount;
125
f803565868a4 improved performance of promises
cin
parents: 124
diff changeset
87 Console.WriteLine("done: {0} ms, {1:.00} Mb, {2} GC", t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0) );
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
88
dc4942d09e74 improved tracing
cin
parents:
diff changeset
89 }
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
90
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
91
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
92 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
93 }