annotate MonoPlay/Program.cs @ 124:a336cb13c6a9 v2

major update, added Drain mathod to AsyncQueue class
author cin
date Thu, 15 Jan 2015 02:43:14 +0300
parents 0c8685c8b56b
children f803565868a4
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;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
7
dc4942d09e74 improved tracing
cin
parents:
diff changeset
8 namespace MonoPlay {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
9 class MainClass {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
10 public static void Main(string[] args) {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
11 if (args == null)
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
12 throw new ArgumentNullException("args");
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
13
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
14 var q1 = new AsyncQueue<int>();
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 103
diff changeset
15 var q2 = new Queue<int>();
103
cin
parents: 94
diff changeset
16
cin
parents: 94
diff changeset
17 const int count = 10000000;
cin
parents: 94
diff changeset
18
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
19 int res1 = 0, res2 = 0;
103
cin
parents: 94
diff changeset
20 var t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
21
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
22 AsyncPool.RunThread(
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
23 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
24 for (var i = 0; i < count; i++)
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
25 q1.Enqueue(1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
26 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
27 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
28 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
29 for (var i = 0; i < count; i++)
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
30 q1.Enqueue(2);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
31 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
32 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
33 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
34 int temp = 0;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
35 int i = 0;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
36 while (i < count)
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
37 if (q1.TryDequeue(out temp)) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
38 i++;
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
39 res1 += temp;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
40 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
41 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
42 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
43 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
44 int temp = 0;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
45 int i = 0;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
46 while (i < count)
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
47 if (q1.TryDequeue(out temp)) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
48 i++;
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
49 res2 += temp;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
50 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
51 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
52 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
53 )
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
54 .Bundle()
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
55 .Join();
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
56
122
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
57 Console.WriteLine("done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", Environment.TickCount - t1, res1, res2, res1 + res2, count*2);
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
58
103
cin
parents: 94
diff changeset
59 var t2 = Environment.TickCount;
cin
parents: 94
diff changeset
60 Console.WriteLine("MTQueue: {0} ms", t2 - t1);
cin
parents: 94
diff changeset
61
cin
parents: 94
diff changeset
62 t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
63
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
64 for (var i = 0; i < count * 2; i++)
103
cin
parents: 94
diff changeset
65 q2.Enqueue(i);
cin
parents: 94
diff changeset
66
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
67 for (var i = 0; i < count * 2; i++)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
68 q2.Dequeue();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
69
103
cin
parents: 94
diff changeset
70 t2 = Environment.TickCount;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
71 Console.WriteLine("Queue: {0} ms", t2 - t1);
103
cin
parents: 94
diff changeset
72
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 103
diff changeset
73 q2 = new Queue<int>();
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
74
103
cin
parents: 94
diff changeset
75 t1 = Environment.TickCount;
cin
parents: 94
diff changeset
76
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
77
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 120
diff changeset
78 AsyncPool.RunThread(
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
79 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
80 for (var i = 0; i < count; i++)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
81 lock (q2)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
82 q2.Enqueue(i);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
83 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
84 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
85 for (var i = 0; i < count; i++)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
86 lock (q2)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
87 q2.Enqueue(i);
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
88 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
89 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
90 for (int i = 0; i < count ;)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
91 lock (q2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
92 if (q2.Count == 0)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
93 continue;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
94 q2.Dequeue();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
95 i++;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
96 }
108
cin
parents: 104
diff changeset
97
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
98 },
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
99 () => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
100 for (int i = 0; i < count ;)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
101 lock (q2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
102 if (q2.Count == 0)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
103 continue;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
104 q2.Dequeue();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
105 i++;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
106 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
107
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
108 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
109 )
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
110 .Bundle()
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
111 .Join();
108
cin
parents: 104
diff changeset
112
cin
parents: 104
diff changeset
113
103
cin
parents: 94
diff changeset
114
cin
parents: 94
diff changeset
115 t2 = Environment.TickCount;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
116 Console.WriteLine("Queue+Lock: {0} ms", t2 - t1);
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
117
dc4942d09e74 improved tracing
cin
parents:
diff changeset
118 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
119 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
120 }