229
|
1 using Implab.Formats.Json;
|
233
|
2 using Implab.Parallels;
|
229
|
3 using Implab.Xml;
|
|
4 using System;
|
233
|
5 using System.Collections.Concurrent;
|
229
|
6 using System.Collections.Generic;
|
|
7 using System.IO;
|
|
8 using System.Linq;
|
|
9 using System.Text;
|
233
|
10 using System.Threading;
|
229
|
11 using System.Threading.Tasks;
|
|
12 using System.Xml;
|
|
13 using System.Xml.Serialization;
|
|
14
|
|
15 namespace Implab.Playground {
|
|
16 public class Program {
|
|
17
|
233
|
18 static void EnqueueRange<T>(ConcurrentQueue<T> q, T[] data, int offset, int len) {
|
|
19 for (var i = offset; i < offset + len; i++)
|
|
20 q.Enqueue(data[i]);
|
|
21 }
|
|
22
|
|
23 static bool TryDequeueRange<T>(ConcurrentQueue<T> q,T[] buffer,int offset, int len, out int actual) {
|
|
24 actual = 0;
|
|
25 T res;
|
|
26 while(q.TryDequeue(out res)) {
|
|
27 buffer[offset + actual] = res;
|
|
28 actual++;
|
|
29 if (actual == len)
|
|
30 break;
|
|
31 }
|
|
32 return actual != 0;
|
|
33 }
|
|
34
|
|
35 static void EnqueueRange<T>(SimpleAsyncQueue<T> q, T[] data, int offset, int len) {
|
|
36 for (var i = offset; i < offset + len; i++)
|
|
37 q.Enqueue(data[i]);
|
|
38 }
|
229
|
39
|
233
|
40 static bool TryDequeueRange<T>(SimpleAsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
|
|
41 actual = 0;
|
|
42 T res;
|
|
43 while (q.TryDequeue(out res)) {
|
|
44 buffer[offset + actual] = res;
|
|
45 actual++;
|
|
46 if (actual == len)
|
|
47 break;
|
|
48 }
|
|
49 return actual != 0;
|
|
50 }
|
229
|
51
|
233
|
52 /*
|
|
53 static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
|
|
54 for (var i = offset; i < offset + len; i++)
|
|
55 q.Enqueue(data[i]);
|
|
56 }
|
229
|
57
|
233
|
58 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
|
|
59 actual = 0;
|
|
60 T res;
|
|
61 while (q.TryDequeue(out res)) {
|
|
62 buffer[offset + actual] = res;
|
|
63 actual++;
|
|
64 if (actual == len)
|
|
65 break;
|
|
66 }
|
|
67 return actual != 0;
|
|
68 }
|
|
69 */
|
|
70
|
|
71 static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
|
|
72 q.EnqueueRange(data, offset, len);
|
229
|
73 }
|
|
74
|
233
|
75 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
|
|
76 return q.TryDequeueRange(buffer, offset, len, out actual);
|
|
77 }
|
|
78
|
|
79
|
229
|
80 static void Main(string[] args) {
|
|
81
|
233
|
82 //var queue = new ConcurrentQueue<int>();
|
|
83 var queue = new AsyncQueue<int>();
|
|
84 //var queue = new SimpleAsyncQueue<int>();
|
|
85
|
|
86 const int wBatch = 32;
|
|
87 const long wCount = 1000000;
|
|
88 const long total = wBatch * wCount * 3;
|
|
89
|
|
90 long r1 = 0, r2 = 0, r3 = 0;
|
|
91 const int rBatch = 1000;
|
|
92 long read = 0;
|
|
93
|
|
94 var t1 = Environment.TickCount;
|
|
95
|
|
96 AsyncPool.RunThread(
|
|
97 () => {
|
|
98 var buffer = new int[wBatch];
|
|
99 for (int i = 0; i < wBatch; i++)
|
|
100 buffer[i] = 1;
|
|
101
|
|
102 for (int i = 0; i < wCount; i++)
|
|
103 EnqueueRange(queue, buffer, 0, wBatch);
|
|
104 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
|
|
105 },
|
|
106 () => {
|
|
107 var buffer = new int[wBatch];
|
|
108 for (int i = 0; i < wBatch; i++)
|
|
109 buffer[i] = 1;
|
|
110
|
|
111 for (int i = 0; i < wCount; i++)
|
|
112 EnqueueRange(queue, buffer, 0, wBatch);
|
|
113 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
|
|
114 },
|
|
115 () => {
|
|
116 var buffer = new int[wBatch];
|
|
117 for (int i = 0; i < wBatch; i++)
|
|
118 buffer[i] = 1;
|
|
119
|
|
120 for (int i = 0; i < wCount; i++)
|
|
121 EnqueueRange(queue, buffer, 0, wBatch);
|
|
122 Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1);
|
|
123 },
|
|
124 () => {
|
|
125 var buffer = new int[rBatch];
|
|
126
|
|
127 while (read < total) {
|
|
128 int actual;
|
|
129 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
130 for (int i = 0; i < actual; i++)
|
|
131 r1 += buffer[i];
|
|
132 Interlocked.Add(ref read, actual);
|
|
133 }
|
|
134 }
|
|
135
|
|
136 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1);
|
|
137 }/*,
|
|
138 () => {
|
|
139 var buffer = new int[rBatch];
|
|
140
|
|
141 while (read < total) {
|
|
142 int actual;
|
|
143 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
144 for (int i = 0; i < actual; i++)
|
|
145 r2 += buffer[i];
|
|
146 Interlocked.Add(ref read, actual);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1);
|
|
151 }*//*,
|
|
152 () => {
|
|
153 var buffer = new int[rBatch];
|
|
154
|
|
155 while (read < total) {
|
|
156 int actual;
|
|
157 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
158 for (int i = 0; i < actual; i++)
|
|
159 r3 += buffer[i];
|
|
160 Interlocked.Add(ref read, actual);
|
|
161 }
|
|
162 }
|
|
163
|
|
164 Console.WriteLine("done reader #3: {0} ms", Environment.TickCount - t1);
|
|
165 }*/
|
|
166 )
|
|
167 .PromiseAll()
|
|
168 .Join();
|
|
169
|
|
170
|
|
171 Console.WriteLine(
|
|
172 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
|
|
173 Environment.TickCount - t1,
|
|
174 r1,
|
|
175 r2,
|
|
176 r1 + r2 + r3,
|
|
177 total
|
|
178 );
|
229
|
179
|
|
180 Console.WriteLine("done");
|
|
181 }
|
|
182 }
|
|
183 }
|