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 static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
|
|
53 for (var i = offset; i < offset + len; i++)
|
|
54 q.Enqueue(data[i]);
|
|
55 }
|
229
|
56
|
233
|
57 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
|
|
58 actual = 0;
|
|
59 T res;
|
|
60 while (q.TryDequeue(out res)) {
|
|
61 buffer[offset + actual] = res;
|
|
62 actual++;
|
|
63 if (actual == len)
|
|
64 break;
|
|
65 }
|
|
66 return actual != 0;
|
|
67 }
|
234
|
68
|
233
|
69
|
234
|
70 /*static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
|
233
|
71 q.EnqueueRange(data, offset, len);
|
229
|
72 }
|
|
73
|
233
|
74 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
|
|
75 return q.TryDequeueRange(buffer, offset, len, out actual);
|
234
|
76 }*/
|
233
|
77
|
|
78
|
229
|
79 static void Main(string[] args) {
|
|
80
|
233
|
81 //var queue = new ConcurrentQueue<int>();
|
|
82 var queue = new AsyncQueue<int>();
|
|
83 //var queue = new SimpleAsyncQueue<int>();
|
|
84
|
|
85 const int wBatch = 32;
|
|
86 const long wCount = 1000000;
|
|
87 const long total = wBatch * wCount * 3;
|
|
88
|
|
89 long r1 = 0, r2 = 0, r3 = 0;
|
|
90 const int rBatch = 1000;
|
|
91 long read = 0;
|
|
92
|
|
93 var t1 = Environment.TickCount;
|
|
94
|
|
95 AsyncPool.RunThread(
|
|
96 () => {
|
|
97 var buffer = new int[wBatch];
|
|
98 for (int i = 0; i < wBatch; i++)
|
|
99 buffer[i] = 1;
|
|
100
|
|
101 for (int i = 0; i < wCount; i++)
|
|
102 EnqueueRange(queue, buffer, 0, wBatch);
|
|
103 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
|
|
104 },
|
|
105 () => {
|
|
106 var buffer = new int[wBatch];
|
|
107 for (int i = 0; i < wBatch; i++)
|
|
108 buffer[i] = 1;
|
|
109
|
|
110 for (int i = 0; i < wCount; i++)
|
|
111 EnqueueRange(queue, buffer, 0, wBatch);
|
|
112 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
|
|
113 },
|
|
114 () => {
|
|
115 var buffer = new int[wBatch];
|
|
116 for (int i = 0; i < wBatch; i++)
|
|
117 buffer[i] = 1;
|
|
118
|
|
119 for (int i = 0; i < wCount; i++)
|
|
120 EnqueueRange(queue, buffer, 0, wBatch);
|
|
121 Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1);
|
|
122 },
|
|
123 () => {
|
|
124 var buffer = new int[rBatch];
|
|
125
|
|
126 while (read < total) {
|
|
127 int actual;
|
|
128 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
129 for (int i = 0; i < actual; i++)
|
|
130 r1 += buffer[i];
|
|
131 Interlocked.Add(ref read, actual);
|
|
132 }
|
|
133 }
|
|
134
|
|
135 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1);
|
|
136 }/*,
|
|
137 () => {
|
|
138 var buffer = new int[rBatch];
|
|
139
|
|
140 while (read < total) {
|
|
141 int actual;
|
|
142 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
143 for (int i = 0; i < actual; i++)
|
|
144 r2 += buffer[i];
|
|
145 Interlocked.Add(ref read, actual);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1);
|
|
150 }*//*,
|
|
151 () => {
|
|
152 var buffer = new int[rBatch];
|
|
153
|
|
154 while (read < total) {
|
|
155 int actual;
|
|
156 if (TryDequeueRange(queue, buffer, 0, rBatch, out actual)) {
|
|
157 for (int i = 0; i < actual; i++)
|
|
158 r3 += buffer[i];
|
|
159 Interlocked.Add(ref read, actual);
|
|
160 }
|
|
161 }
|
|
162
|
|
163 Console.WriteLine("done reader #3: {0} ms", Environment.TickCount - t1);
|
|
164 }*/
|
|
165 )
|
|
166 .PromiseAll()
|
|
167 .Join();
|
|
168
|
|
169
|
|
170 Console.WriteLine(
|
|
171 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
|
|
172 Environment.TickCount - t1,
|
|
173 r1,
|
|
174 r2,
|
|
175 r1 + r2 + r3,
|
|
176 total
|
|
177 );
|
229
|
178
|
|
179 Console.WriteLine("done");
|
|
180 }
|
|
181 }
|
|
182 }
|