annotate Implab.Playground/Program.cs @ 234:8dd666e6b6bf v2

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