[14744] | 1 | using System.Threading.Tasks;
|
---|
| 2 |
|
---|
| 3 | namespace TestPooling {
|
---|
| 4 | using System;
|
---|
| 5 | using System.Diagnostics;
|
---|
[14745] | 6 | using System.Threading;
|
---|
[14744] | 7 |
|
---|
| 8 | using TestPooling.Pool;
|
---|
| 9 |
|
---|
| 10 | class Program {
|
---|
[14745] | 11 | private const int PartitionSize = 512;
|
---|
| 12 | private const int ExampleCount = 200;
|
---|
| 13 | private const int InstanceCount = 1024;
|
---|
| 14 | private const int PopulationCount = 100;
|
---|
| 15 |
|
---|
| 16 | private static int TotalInstanceCount => PopulationCount * InstanceCount * ExampleCount;
|
---|
| 17 |
|
---|
[14744] | 18 | static void Main(string[] args) {
|
---|
| 19 | Test1();
|
---|
| 20 | Test1_2();
|
---|
| 21 | Test3();
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | private static void Test1() {
|
---|
| 25 | var sw = new Stopwatch();
|
---|
| 26 |
|
---|
| 27 | sw.Start();
|
---|
[14745] | 28 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 29 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 30 | using (var pool = new ManagedPool<Person>(PartitionSize, () => new Person())) {
|
---|
| 31 | for (var i = 0; i < InstanceCount; i++) {
|
---|
| 32 | var person = pool.Get();
|
---|
| 33 | person.Age = pc + e + i;
|
---|
| 34 | person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
|
---|
| 35 | }
|
---|
[14744] | 36 | }
|
---|
| 37 | }
|
---|
| 38 | });
|
---|
| 39 | sw.Stop();
|
---|
| 40 |
|
---|
[14745] | 41 | Console.WriteLine("1 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, ManagedPool<Person>.InstanceCount, TotalInstanceCount);
|
---|
[14744] | 42 | }
|
---|
| 43 |
|
---|
| 44 | private static void Test1_2() {
|
---|
| 45 | var sw = new Stopwatch();
|
---|
| 46 | sw.Start();
|
---|
| 47 |
|
---|
[14745] | 48 | var provider = new ManagedPoolProvider<Person>(PartitionSize);
|
---|
| 49 | provider.InitDummyPartition(() => new Person());
|
---|
[14744] | 50 |
|
---|
[14745] | 51 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 52 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 53 | using (var pool = provider.CreatePool()) {
|
---|
| 54 | for (var i = 0; i < InstanceCount; i++) {
|
---|
| 55 | var person = pool.Get();
|
---|
| 56 | person.Age = pc + e + i;
|
---|
| 57 | person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
|
---|
| 58 | }
|
---|
[14744] | 59 | }
|
---|
| 60 | }
|
---|
| 61 | });
|
---|
| 62 | sw.Stop();
|
---|
| 63 |
|
---|
[14745] | 64 | Console.WriteLine("1_2 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
|
---|
[14744] | 65 | }
|
---|
| 66 |
|
---|
| 67 | private static void Test3() {
|
---|
| 68 | var count = 0;
|
---|
| 69 | var sw = new Stopwatch();
|
---|
| 70 |
|
---|
| 71 | sw.Start();
|
---|
[14745] | 72 | Parallel.For(0, PopulationCount, pc => {
|
---|
| 73 | for (var e = 0; e < ExampleCount; e++) {
|
---|
| 74 | var list = new Person[InstanceCount];
|
---|
| 75 | for (var i = 0; i < InstanceCount; i++) {
|
---|
| 76 | list[i] = new Person {
|
---|
| 77 | Age = pc + e + i,
|
---|
| 78 | Name = string.Format("{0} - {1} - {2}", pc, e, i)
|
---|
| 79 | };
|
---|
| 80 | }
|
---|
[14744] | 81 |
|
---|
[14745] | 82 | Interlocked.Add(ref count, list.Length);
|
---|
[14744] | 83 | }
|
---|
| 84 | });
|
---|
| 85 | sw.Stop();
|
---|
| 86 |
|
---|
| 87 | Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);
|
---|
[14745] | 88 | Console.WriteLine("PartitionSize: {0}", count);
|
---|
[14744] | 89 | }
|
---|
| 90 |
|
---|
| 91 | private static void Test2() {
|
---|
| 92 | var pool = new ObjectPool<Person>(() => new Person(), 2048);
|
---|
| 93 |
|
---|
| 94 | Parallel.For(0, 10000, i => {
|
---|
| 95 | for (var j = 0; j < 3000; j++) {
|
---|
| 96 | var person = pool.Allocate();
|
---|
| 97 | person.Age = i + j;
|
---|
| 98 | person.Name = string.Format("{0} - {1}", i, j);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | //pool.Free()
|
---|
| 102 | });
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | [Serializable]
|
---|
| 107 | class Person {
|
---|
| 108 | public string Name { get; set; }
|
---|
| 109 | public int Age { get; set; }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|