annotate Implab.Playground/Program.cs @ 233:d6fe09f5592c v2

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