Mercurial > pub > ImplabNet
comparison 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 |
comparison
equal
deleted
inserted
replaced
229:5f7a3e1d32b9 | 233:d6fe09f5592c |
---|---|
1 using Implab.Formats.Json; | 1 using Implab.Formats.Json; |
2 using Implab.Parallels; | |
2 using Implab.Xml; | 3 using Implab.Xml; |
3 using System; | 4 using System; |
5 using System.Collections.Concurrent; | |
4 using System.Collections.Generic; | 6 using System.Collections.Generic; |
5 using System.IO; | 7 using System.IO; |
6 using System.Linq; | 8 using System.Linq; |
7 using System.Text; | 9 using System.Text; |
10 using System.Threading; | |
8 using System.Threading.Tasks; | 11 using System.Threading.Tasks; |
9 using System.Xml; | 12 using System.Xml; |
10 using System.Xml.Serialization; | 13 using System.Xml.Serialization; |
11 | 14 |
12 namespace Implab.Playground { | 15 namespace Implab.Playground { |
13 public class Program { | 16 public class Program { |
14 | 17 |
15 [XmlRoot(Namespace = "XmlSimpleData")] | 18 static void EnqueueRange<T>(ConcurrentQueue<T> q, T[] data, int offset, int len) { |
16 public class XmlSimpleModel { | 19 for (var i = offset; i < offset + len; i++) |
17 [XmlElement] | 20 q.Enqueue(data[i]); |
18 public string Name { get; set; } | |
19 | |
20 [XmlElement] | |
21 public int Order { get; set; } | |
22 | |
23 [XmlElement] | |
24 public string[] Items { get; set; } | |
25 | |
26 } | 21 } |
27 | 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 } | |
39 | |
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 } | |
51 | |
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 } | |
57 | |
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); | |
73 } | |
74 | |
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 | |
28 static void Main(string[] args) { | 80 static void Main(string[] args) { |
29 var model = new XmlSimpleModel { | |
30 Name = "Tablet", | |
31 Order = 10, | |
32 Items = new string[] { "z1", "z2", "z3" } | |
33 }; | |
34 | 81 |
35 var doc = SerializationHelpers.SerializeAsXmlDocument(model); | 82 //var queue = new ConcurrentQueue<int>(); |
83 var queue = new AsyncQueue<int>(); | |
84 //var queue = new SimpleAsyncQueue<int>(); | |
36 | 85 |
37 var m2 = SerializationHelpers.DeserializeFromXmlNode<XmlSimpleModel>(doc.DocumentElement); | 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 ); | |
38 | 179 |
39 Console.WriteLine("done"); | 180 Console.WriteLine("done"); |
40 } | 181 } |
41 } | 182 } |
42 } | 183 } |