[14777] | 1 | using System.Threading.Tasks;
|
---|
| 2 |
|
---|
| 3 | namespace TestPooling {
|
---|
| 4 | using System;
|
---|
| 5 | using System.Collections.Generic;
|
---|
| 6 | using System.Diagnostics;
|
---|
| 7 | using System.Linq;
|
---|
[15189] | 8 | using System.Text;
|
---|
[14777] | 9 | using System.Threading;
|
---|
| 10 | using DirtyList;
|
---|
| 11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
[14834] | 12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
|
---|
[14777] | 13 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
[15334] | 14 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
[14834] | 15 | using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
|
---|
[14777] | 16 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
| 17 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
| 18 | using HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier;
|
---|
| 19 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 20 | using HeuristicLab.Random;
|
---|
| 21 |
|
---|
| 22 | class Program {
|
---|
| 23 | private const int PartitionSize = 512;
|
---|
| 24 | private const int ExampleCount = 200;
|
---|
| 25 | private const int InstanceCount = 1024;
|
---|
[15334] | 26 | private const int PopulationCount = 1000;
|
---|
[14777] | 27 |
|
---|
| 28 | private static int TotalInstanceCount => PopulationCount * InstanceCount * ExampleCount;
|
---|
| 29 |
|
---|
| 30 | static void Main(string[] args) {
|
---|
[15334] | 31 | // object pooling
|
---|
[14777] | 32 | //Test1();
|
---|
[15344] | 33 | //Test1_2();
|
---|
[14777] | 34 | //Test3();
|
---|
| 35 |
|
---|
[14875] | 36 | //TestCollectionPooling();
|
---|
[14777] | 37 |
|
---|
| 38 | //DirtyListTest();
|
---|
| 39 |
|
---|
| 40 | //TestAddRangeNConcat();
|
---|
| 41 |
|
---|
| 42 | //TestStacks();
|
---|
| 43 |
|
---|
| 44 | //SimpleTest();
|
---|
| 45 | //Stepwise().Wait();
|
---|
| 46 | //PerformanceTestInterpreter();
|
---|
| 47 | //PerformanceParallelTestInterpreter();
|
---|
| 48 | //PerformanceTestLinearCodeGenerator();
|
---|
[15189] | 49 | //TestRobustness();
|
---|
[14777] | 50 | //TestPool();
|
---|
| 51 | //TestPoolPerformance();
|
---|
| 52 | //TestSimplifier();
|
---|
| 53 | //FlagsTest();
|
---|
| 54 | //TestExpressionDependencies();
|
---|
[14905] | 55 | //TestSplitLogic();
|
---|
[15273] | 56 | //TestStringBuilder();
|
---|
[15334] | 57 | //TestRandomReset();
|
---|
[14777] | 58 |
|
---|
[15334] | 59 | //TestRandomBiased(0, 4, 0.01);
|
---|
| 60 | //TestRandomBiased(0, 4, 0.25);
|
---|
| 61 | //TestRandomBiased(0, 4, 0.5);
|
---|
| 62 | //TestRandomBiased(0, 4, 1);
|
---|
| 63 | //TestRandomBiased(0, 4, 2);
|
---|
| 64 | //TestRandomBiased(0, 4, 3);
|
---|
| 65 | //TestRandomBiased(0, 4, 4);
|
---|
[15344] | 66 | TestOverflowException();
|
---|
[15334] | 67 |
|
---|
| 68 |
|
---|
[14777] | 69 | Console.WriteLine("\nPress any key to continue...");
|
---|
| 70 | Console.ReadKey(false);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[15344] | 73 | private static void TestOverflowException() {
|
---|
| 74 | // overflow
|
---|
| 75 | //var x = long.MinValue / -1;
|
---|
| 76 | var y = unchecked(long.MaxValue + long.MaxValue);
|
---|
| 77 | var z = unchecked((long)double.MaxValue);
|
---|
| 78 | var a = double.MaxValue + (double.MaxValue - 1);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[15334] | 81 | private static void TestRandomBiased(int min, int max, double bias) {
|
---|
| 82 | var random = new MersenneTwister();
|
---|
| 83 | var total = 100000;
|
---|
| 84 |
|
---|
| 85 | var distribution = Enumerable
|
---|
| 86 | .Range(0, total)
|
---|
| 87 | .Select(_ => random.NextBiased(min, max, bias))
|
---|
| 88 | .GroupBy(x => x)
|
---|
| 89 | .OrderBy(g => g.Key)
|
---|
| 90 | .ToDictionary(g => g.Key, g => g.Count() / (double)total * 100);
|
---|
| 91 |
|
---|
| 92 | Console.WriteLine($@"{min}|{max}|{bias}");
|
---|
| 93 |
|
---|
| 94 | foreach (var entry in distribution) {
|
---|
| 95 | Console.WriteLine($@"{entry.Key}: {entry.Value}");
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | Console.WriteLine();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[15189] | 101 | private static void TestStringBuilder() {
|
---|
| 102 | var sb = new StringBuilder();
|
---|
| 103 | sb.Append("test");
|
---|
| 104 |
|
---|
| 105 | var s1 = sb.ToString();
|
---|
| 106 | var s2 = sb.ToString();
|
---|
| 107 |
|
---|
| 108 | // false
|
---|
| 109 | var equal = ReferenceEquals(s1, s2);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[15273] | 112 | private static void TestRandomReset() {
|
---|
| 113 | var random = new MersenneTwister(1000);
|
---|
| 114 |
|
---|
| 115 | var x = random.Next();
|
---|
| 116 | random.Reset();
|
---|
| 117 | var y = random.Next();
|
---|
| 118 |
|
---|
| 119 | if (x != y) {
|
---|
| 120 | throw new InvalidOperationException();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[14905] | 124 | private static void TestSplitLogic() {
|
---|
| 125 | var sw = new Stopwatch();
|
---|
| 126 | var iMax = 100000000u;
|
---|
| 127 | var repeats = 10;
|
---|
| 128 | var or = new BooleanOrExpression();
|
---|
| 129 | var and = new BooleanAndExpression();
|
---|
| 130 | var ors = new Expression[iMax];
|
---|
| 131 | var ands = new Expression[iMax];
|
---|
| 132 | var booleans = new bool[iMax];
|
---|
| 133 |
|
---|
| 134 | for (var i = 0u; i < iMax; i++) {
|
---|
| 135 | ors[i] = or;
|
---|
| 136 | ands[i] = and;
|
---|
| 137 | booleans[i] = i % 2 == 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | var orsProgram = new PushProgram(ors);
|
---|
| 141 | var andsProgram = new PushProgram(ands);
|
---|
| 142 |
|
---|
| 143 | using (var interpreter = new PushInterpreter()) {
|
---|
| 144 | for (var i = 0u; i < repeats; i++) {
|
---|
| 145 | interpreter.BooleanStack.Push(booleans);
|
---|
| 146 |
|
---|
| 147 | sw.Start();
|
---|
| 148 | interpreter.Run(orsProgram);
|
---|
| 149 | sw.Stop();
|
---|
| 150 | interpreter.Reset();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | Console.WriteLine("ors:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
| 154 |
|
---|
| 155 | sw.Reset();
|
---|
| 156 | interpreter.Reset();
|
---|
| 157 |
|
---|
| 158 | for (var i = 0u; i < repeats; i++) {
|
---|
| 159 | interpreter.BooleanStack.Push(booleans);
|
---|
| 160 |
|
---|
| 161 | sw.Start();
|
---|
| 162 | interpreter.Run(andsProgram);
|
---|
| 163 | sw.Stop();
|
---|
| 164 | interpreter.Reset();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | Console.WriteLine("ands:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
| 168 |
|
---|
| 169 | sw.Reset();
|
---|
| 170 | interpreter.Reset();
|
---|
| 171 |
|
---|
| 172 | for (var i = 0u; i < repeats; i++) {
|
---|
| 173 | interpreter.BooleanStack.Push(booleans);
|
---|
| 174 |
|
---|
| 175 | sw.Start();
|
---|
| 176 | interpreter.Run(orsProgram);
|
---|
| 177 | sw.Stop();
|
---|
| 178 | interpreter.Reset();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | Console.WriteLine("ors:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
| 182 |
|
---|
| 183 | sw.Reset();
|
---|
| 184 | interpreter.Reset();
|
---|
| 185 |
|
---|
| 186 | for (var i = 0u; i < repeats; i++) {
|
---|
| 187 | interpreter.BooleanStack.Push(booleans);
|
---|
| 188 |
|
---|
| 189 | sw.Start();
|
---|
| 190 | interpreter.Run(andsProgram);
|
---|
| 191 | sw.Stop();
|
---|
| 192 | interpreter.Reset();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | Console.WriteLine("ands:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[14777] | 199 | private static void TestExpressionDependencies() {
|
---|
[14834] | 200 | var enabledExpressions = ExpressionTable.GetExpressionsByStackTypes(StackTypes.Boolean | StackTypes.Integer);
|
---|
[14777] | 201 | }
|
---|
| 202 |
|
---|
| 203 | private static void FlagsTest() {
|
---|
| 204 | var types = StackTypes.Boolean | StackTypes.Char | StackTypes.Integer;
|
---|
| 205 |
|
---|
| 206 | StackTypes test1 = (StackTypes)(StackTypes.Boolean - types);
|
---|
| 207 | StackTypes test2 = (StackTypes)((StackTypes.Boolean | StackTypes.Float) - types);
|
---|
| 208 | StackTypes test3 = ((StackTypes.Boolean | StackTypes.Float) & types);
|
---|
| 209 | StackTypes test4 = ((StackTypes.Boolean | StackTypes.Float) ^ types);
|
---|
| 210 | StackTypes test5 = ((StackTypes.Boolean | StackTypes.Float) | types);
|
---|
| 211 | StackTypes test6 = (StackTypes.Boolean | StackTypes.Float) & ~types;
|
---|
| 212 | StackTypes test7 = StackTypes.Boolean & ~types;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | private static void TestStacks() {
|
---|
| 216 | var sw = new Stopwatch();
|
---|
| 217 | var stack = new DirtyList.PushStack<int>();
|
---|
| 218 | var stack2 = new PushStack2<int>();
|
---|
| 219 | var stack3 = new PushStack3<int>();
|
---|
| 220 | var listC = new List<int>(1000);
|
---|
| 221 | var count = 200000;
|
---|
| 222 | var repeats = 10;
|
---|
| 223 |
|
---|
| 224 | for (var i = 0; i < 100; i++) {
|
---|
| 225 | listC.Add(i);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | sw.Start();
|
---|
| 229 | for (var j = 0; j < repeats; j++) {
|
---|
| 230 | stack.Clear();
|
---|
| 231 |
|
---|
| 232 | for (int i = 0; i < count; i++) {
|
---|
| 233 | stack.Push(listC);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | sw.Stop();
|
---|
| 237 |
|
---|
| 238 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
| 239 |
|
---|
| 240 | sw.Restart();
|
---|
| 241 | for (var j = 0; j < repeats; j++) {
|
---|
| 242 | stack2.Clear();
|
---|
| 243 |
|
---|
| 244 | for (int i = 0; i < count; i++) {
|
---|
| 245 | stack2.PushRange(listC);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | sw.Stop();
|
---|
| 249 |
|
---|
| 250 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
| 251 |
|
---|
| 252 | sw.Restart();
|
---|
| 253 | for (var j = 0; j < repeats; j++) {
|
---|
| 254 | stack3.Clear();
|
---|
| 255 |
|
---|
| 256 | for (int i = 0; i < count; i++) {
|
---|
| 257 | stack3.PushRange(listC);
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | sw.Stop();
|
---|
| 261 |
|
---|
| 262 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
| 263 |
|
---|
| 264 | sw.Restart();
|
---|
| 265 | for (int i = 0; i < count * listC.Count; i++) {
|
---|
| 266 | stack.Pop();
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
| 270 |
|
---|
| 271 | sw.Restart();
|
---|
| 272 | for (int i = 0; i < count * listC.Count; i++) {
|
---|
| 273 | stack2.Pop();
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | private static void TestAddRangeNConcat() {
|
---|
| 282 | var sw = new Stopwatch();
|
---|
| 283 | var count = 10000;
|
---|
| 284 | var listA = new List<int>();
|
---|
| 285 | var listB = new List<int>();
|
---|
| 286 | var listC = new List<int>(1000);
|
---|
| 287 |
|
---|
| 288 | for (var i = 0; i < 1000; i++) {
|
---|
| 289 | listC.Add(i);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | sw.Start();
|
---|
| 293 | for (int i = 0; i < count; i++) {
|
---|
| 294 | listA.AddRange(listC);
|
---|
| 295 | }
|
---|
| 296 | sw.Stop();
|
---|
| 297 |
|
---|
| 298 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
| 299 |
|
---|
| 300 | sw.Restart();
|
---|
| 301 | for (int i = 0; i < count; i++) {
|
---|
| 302 | listB = listB.Concat(listC).ToList();
|
---|
| 303 | }
|
---|
| 304 | sw.Stop();
|
---|
| 305 |
|
---|
| 306 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
| 307 |
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | private static void DirtyListTest() {
|
---|
| 311 | var sw = new Stopwatch();
|
---|
| 312 | var provider = new ManagedPoolProvider<Person>(100, () => new Person());
|
---|
| 313 | var provider2 = new ManagedPoolProvider<Person>(100, () => new Person());
|
---|
| 314 | var dl = new DirtyList<Person>();
|
---|
| 315 | var l = new List<Person>();
|
---|
| 316 |
|
---|
| 317 | sw.Start();
|
---|
| 318 | for (var i = 0; i < 30000000; i++) {
|
---|
| 319 | using (var pool = provider.CreatePool()) {
|
---|
| 320 | for (var j = 0; j < 2; j++) {
|
---|
| 321 | var person = pool.Get();
|
---|
| 322 | person.Age = i + j;
|
---|
| 323 | person.Name = (i + j).ToString();
|
---|
| 324 |
|
---|
| 325 | dl.Add(person);
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | dl.Clear();
|
---|
| 330 | }
|
---|
| 331 | sw.Stop();
|
---|
| 332 |
|
---|
| 333 | Console.WriteLine("DirtyList: {0}", sw.Elapsed.TotalSeconds);
|
---|
| 334 |
|
---|
| 335 | sw.Restart();
|
---|
| 336 | for (var i = 0; i < 30000000; i++) {
|
---|
| 337 | using (var pool = provider2.CreatePool()) {
|
---|
| 338 | for (var j = 0; j < 2; j++) {
|
---|
| 339 | var person = pool.Get();
|
---|
| 340 | person.Age = i + j;
|
---|
| 341 | person.Name = (i + j).ToString();
|
---|
| 342 |
|
---|
| 343 | l.Add(person);
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | l.Clear();
|
---|
| 348 | }
|
---|
| 349 | sw.Stop();
|
---|
| 350 |
|
---|
| 351 | Console.WriteLine("List: {0}", sw.Elapsed.TotalSeconds);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | private static void TestCollectionPooling() {
|
---|
| 355 | var sw = new Stopwatch();
|
---|
| 356 | sw.Start();
|
---|
| 357 |
|
---|
| 358 | var provider = new ManagedPoolProvider<PooledList<Person>>(PartitionSize, () => new PooledList<Person>());
|
---|
| 359 |
|
---|
| 360 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 361 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 362 | using (var pool = provider.CreatePool()) {
|
---|
| 363 | var list = pool.Get();
|
---|
| 364 |
|
---|
| 365 | for (var i = 0; i < InstanceCount; i++)
|
---|
| 366 | list.Add(new Person {
|
---|
| 367 | Age = pc + e + i,
|
---|
| 368 | Name = string.Format("{0} - {1} - {2}", pc, e, i)
|
---|
| 369 | });
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 | });
|
---|
| 373 | sw.Stop();
|
---|
| 374 |
|
---|
| 375 | Console.WriteLine("Collection Pooling - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | private static void Test1_2() {
|
---|
| 379 | var sw = new Stopwatch();
|
---|
| 380 | sw.Start();
|
---|
| 381 |
|
---|
| 382 | var provider = new ManagedPoolProvider<Person>(PartitionSize, () => new Person());
|
---|
| 383 |
|
---|
| 384 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 385 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 386 | using (var pool = provider.CreatePool()) {
|
---|
| 387 | for (var i = 0; i < InstanceCount; i++) {
|
---|
| 388 | var person = pool.Get();
|
---|
| 389 | person.Age = pc + e + i;
|
---|
| 390 | person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 | });
|
---|
| 395 | sw.Stop();
|
---|
| 396 |
|
---|
| 397 | Console.WriteLine("1_2 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | private static void Test3() {
|
---|
| 401 | var count = 0;
|
---|
| 402 | var sw = new Stopwatch();
|
---|
| 403 |
|
---|
| 404 | sw.Start();
|
---|
| 405 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 406 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 407 | var list = new Person[InstanceCount];
|
---|
| 408 | for (var i = 0; i < InstanceCount; i++) {
|
---|
| 409 | list[i] = new Person {
|
---|
| 410 | Age = pc + e + i,
|
---|
| 411 | Name = string.Format("{0} - {1} - {2}", pc, e, i)
|
---|
| 412 | };
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | Interlocked.Add(ref count, list.Length);
|
---|
| 416 | }
|
---|
| 417 | });
|
---|
| 418 | sw.Stop();
|
---|
| 419 |
|
---|
| 420 | Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);
|
---|
| 421 | Console.WriteLine("PartitionSize: {0}", count);
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | private static void Test2() {
|
---|
| 425 | var pool = new ObjectPool<Person>(() => new Person(), 2048);
|
---|
| 426 |
|
---|
| 427 | Parallel.For(0, 10000, i => {
|
---|
| 428 | for (var j = 0; j < 3000; j++) {
|
---|
| 429 | var person = pool.Allocate();
|
---|
| 430 | person.Age = i + j;
|
---|
| 431 | person.Name = string.Format("{0} - {1}", i, j);
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | //pool.Free()
|
---|
| 435 | });
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | static void SimpleTest() {
|
---|
| 439 | var interpreter = new PushInterpreter();
|
---|
| 440 |
|
---|
| 441 | interpreter.Run("5 INTEGER.DUP INTEGER.+");
|
---|
| 442 |
|
---|
| 443 | interpreter.PrintStacks();
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | static async Task Stepwise() {
|
---|
| 447 | var interpreter = new PushInterpreter();
|
---|
| 448 |
|
---|
[14834] | 449 | interpreter.Run("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )");
|
---|
[14777] | 450 |
|
---|
| 451 | while (!interpreter.IsCompleted) {
|
---|
| 452 | Console.Clear();
|
---|
| 453 | interpreter.PrintStacks();
|
---|
| 454 | interpreter.Step();
|
---|
| 455 |
|
---|
| 456 | var input = Console.ReadKey();
|
---|
| 457 | if (input.Key == ConsoleKey.Escape) {
|
---|
| 458 | break;
|
---|
| 459 | } else if (input.Key == ConsoleKey.Spacebar) {
|
---|
| 460 | await interpreter.ResumeAsync();
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | Console.Clear();
|
---|
| 465 | interpreter.PrintStacks();
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | static void PerformanceTestInterpreter() {
|
---|
| 469 | var program = PushParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
| 470 |
|
---|
| 471 | var interpreter = new PushInterpreter();
|
---|
| 472 | var sw = new Stopwatch();
|
---|
| 473 |
|
---|
| 474 | sw.Start();
|
---|
| 475 | for (var i = 0; i < 60000000; i++) {
|
---|
| 476 | interpreter.Run(program);
|
---|
[15273] | 477 | interpreter.ClearStacks();
|
---|
[14777] | 478 | }
|
---|
| 479 | sw.Stop();
|
---|
| 480 |
|
---|
| 481 | Console.WriteLine(sw.Elapsed);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | static void PerformanceParallelTestInterpreter() {
|
---|
| 485 | var program = PushParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
| 486 |
|
---|
| 487 | var sw = new Stopwatch();
|
---|
| 488 | var iterations = 100;
|
---|
| 489 | var amount = 600000;
|
---|
| 490 | var pool = new PushInterpreterPool(iterations, 1024);
|
---|
| 491 |
|
---|
| 492 | sw.Start();
|
---|
| 493 | Parallel.For(0, iterations, i => {
|
---|
| 494 | using (var interpreter = pool.Create()) {
|
---|
| 495 | for (var j = 0; j < amount; j++) {
|
---|
| 496 | interpreter.Run(program);
|
---|
[15273] | 497 | interpreter.ClearStacks();
|
---|
[14777] | 498 | }
|
---|
| 499 | }
|
---|
| 500 | });
|
---|
| 501 | sw.Stop();
|
---|
| 502 |
|
---|
| 503 | Console.WriteLine(sw.Elapsed);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | static void PerformanceTestRecursiveCodeGenerator() {
|
---|
| 507 | var sw = new Stopwatch();
|
---|
| 508 | var random = new FastRandom(1337);
|
---|
| 509 |
|
---|
| 510 | sw.Start();
|
---|
| 511 | var expressions = RecursiveCodeGenerator.RandomCode(60000000, random).ToList();
|
---|
| 512 | sw.Stop();
|
---|
| 513 |
|
---|
| 514 | Console.WriteLine("Generated {0} in {1}", expressions.Count, sw.Elapsed);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | static void PerformanceTestLinearCodeGenerator() {
|
---|
| 518 | var sw = new Stopwatch();
|
---|
| 519 | var random = new FastRandom(1337);
|
---|
[14897] | 520 | var config = new PushConfiguration();
|
---|
[14777] | 521 |
|
---|
| 522 | sw.Start();
|
---|
[14834] | 523 | var expressions = LinearCodeGenerator.RandomCode(60000000, null, random, config).ToList();
|
---|
[14777] | 524 | sw.Stop();
|
---|
| 525 |
|
---|
| 526 | Console.WriteLine("Generated {0} in {1}", expressions.Count, sw.Elapsed);
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | static void TestRobustness() {
|
---|
| 530 | var sw = new Stopwatch();
|
---|
| 531 | var parallelism = Environment.ProcessorCount;
|
---|
| 532 | var maxProgramSizeLimit = 1024;
|
---|
| 533 | var partitionSize = 100000;
|
---|
[14875] | 534 | var execCounters = new long[parallelism];
|
---|
[14777] | 535 |
|
---|
| 536 | var config = new PushConfiguration {
|
---|
| 537 | EvalPushLimit = 1024,
|
---|
[15334] | 538 | MaxProgramLength = 1024
|
---|
[14777] | 539 | };
|
---|
| 540 |
|
---|
[14905] | 541 | config.DisableStack(StackTypes.Exec);
|
---|
| 542 | config.DisableStack(StackTypes.Code);
|
---|
| 543 | config.DisableStack(StackTypes.Float);
|
---|
[14875] | 544 |
|
---|
[14905] | 545 | var errors = config.EnabledExpressions.Where(
|
---|
| 546 | name => {
|
---|
| 547 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
| 548 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
[14777] | 549 |
|
---|
[14905] | 550 | return attribute.StackType == StackTypes.Exec
|
---|
| 551 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Exec)
|
---|
| 552 | || attribute.StackType == StackTypes.Code
|
---|
| 553 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Code)
|
---|
| 554 | || attribute.StackType == StackTypes.Float
|
---|
| 555 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Float);
|
---|
| 556 | });
|
---|
| 557 |
|
---|
[14777] | 558 | var pool = new PushInterpreterPool(config);
|
---|
| 559 |
|
---|
| 560 | sw.Start();
|
---|
| 561 | Parallel.For(0, parallelism, i => {
|
---|
| 562 | var random = new FastRandom(1337);
|
---|
| 563 |
|
---|
| 564 | using (var interpreter = pool.Create(random)) {
|
---|
| 565 | for (var j = 0; j < partitionSize; j++) {
|
---|
| 566 | var program = LinearCodeGenerator.RandomProgram(maxProgramSizeLimit, random, config);
|
---|
| 567 | interpreter.Run(program);
|
---|
| 568 |
|
---|
| 569 | execCounters[i] += interpreter.ExecCounter;
|
---|
[14875] | 570 | interpreter.Reset();
|
---|
[14777] | 571 | }
|
---|
| 572 | }
|
---|
| 573 | });
|
---|
| 574 | sw.Stop();
|
---|
| 575 |
|
---|
| 576 | Console.WriteLine("ExecCount: {0}", execCounters.Sum());
|
---|
| 577 | Console.WriteLine("Duration: {0}", sw.Elapsed);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | static void TestPool() {
|
---|
| 581 | var pool = new PushInterpreterPool();
|
---|
| 582 | var normal = new PushInterpreter();
|
---|
| 583 | int id;
|
---|
| 584 |
|
---|
| 585 | using (var interpreter = pool.Create()) {
|
---|
| 586 | interpreter.Run("( 1 2 INTEGER.+ )");
|
---|
| 587 |
|
---|
| 588 | id = interpreter.GetHashCode();
|
---|
| 589 | if (id == normal.GetHashCode()) Console.WriteLine("equal 1");
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | using (var interpreter = pool.Create())
|
---|
| 593 | using (var interpreter2 = pool.Create()) {
|
---|
| 594 | interpreter.Run("( 1 2 INTEGER.+ )");
|
---|
| 595 | if (id == interpreter.GetHashCode()) Console.WriteLine("equal 2");
|
---|
| 596 | if (id == interpreter2.GetHashCode()) Console.WriteLine("equal 3");
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | static void TestPoolPerformance() {
|
---|
| 601 | var sw = new Stopwatch();
|
---|
| 602 | var iterations = 100000000;
|
---|
| 603 |
|
---|
| 604 | sw.Start();
|
---|
| 605 | for (var i = 0; i < iterations; i++) {
|
---|
| 606 | var interpreter = new PushInterpreter();
|
---|
[15273] | 607 | interpreter.ClearStacks();
|
---|
[14777] | 608 | }
|
---|
| 609 | sw.Stop();
|
---|
| 610 |
|
---|
| 611 | Console.WriteLine(sw.Elapsed);
|
---|
| 612 |
|
---|
| 613 | var pool = new PushInterpreterPool();
|
---|
| 614 | sw.Restart();
|
---|
| 615 | for (var i = 0; i < iterations; i++) {
|
---|
| 616 | using (var interpreter = pool.Create()) {
|
---|
[15273] | 617 | interpreter.ClearStacks();
|
---|
[14777] | 618 | }
|
---|
| 619 | }
|
---|
| 620 | sw.Stop();
|
---|
| 621 |
|
---|
| 622 | Console.WriteLine(sw.Elapsed);
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | static void TestSimplifier() {
|
---|
| 626 | var program = PushParser.ParseProgram("( 5 ( INTEGER.DUP FLOAT.+ FLOAT.- ) ( EXEC.DO ( EXEC.IF ) EXEC.Y ) INTEGER.+ )");
|
---|
| 627 | var pool = new PushInterpreterPool();
|
---|
| 628 | var random = new FastRandom(1337);
|
---|
| 629 |
|
---|
| 630 | var randomProg = LinearCodeGenerator.RandomProgram(4096 * 4, random); // 53
|
---|
| 631 |
|
---|
| 632 | Func<PushProgram, double> evaluator = prog => {
|
---|
| 633 | using (var interpreter = pool.Create(random)) {
|
---|
| 634 | interpreter.Run(prog);
|
---|
| 635 | return interpreter.IntegerStack.IsEmpty
|
---|
| 636 | ? double.MaxValue
|
---|
| 637 | : Math.Abs(interpreter.IntegerStack.Top - 10);
|
---|
| 638 | }
|
---|
| 639 | };
|
---|
| 640 |
|
---|
[15017] | 641 | var simplerProgram = Simplifier.Simplify(program, pool.PushConfiguration, evaluator);
|
---|
[14777] | 642 |
|
---|
| 643 | Console.WriteLine(simplerProgram);
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | [Serializable]
|
---|
| 648 | class Person : IPooledObject {
|
---|
| 649 | public string Name { get; set; }
|
---|
| 650 | public int Age { get; set; }
|
---|
| 651 |
|
---|
[15273] | 652 | public void Init() { }
|
---|
| 653 |
|
---|
[14777] | 654 | public void Reset() {
|
---|
| 655 | Age = default(int);
|
---|
| 656 | Name = null;
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 | }
|
---|