11using Open . Collections ;
22using Open . Disposable ;
3+ using Open . Disposable . ObjectPools ;
34using System ;
45using System . Collections . Concurrent ;
6+ using System . Collections . Generic ;
7+ using System . Diagnostics ;
8+ using System . Linq ;
59using System . Threading ;
610using System . Threading . Tasks ;
711
812class Program
913{
10- static void Main ( string [ ] args )
11- {
14+
15+ static void ConsoleSeparator ( )
16+ {
17+ Console . WriteLine ( "------------------------------------" ) ;
18+ }
19+
20+ static void ConsoleNewLine ( )
21+ {
22+ Console . WriteLine ( ) ;
23+ }
24+
25+ const int COUNT = 200 ;
26+ const int REPEAT = 5000 ;
27+
28+ static TimedResult [ ] BenchmarkResults < T > ( Func < IObjectPool < T > > poolFactory )
29+ where T : class
30+ {
31+ return Benchmark < T > . Results ( COUNT , REPEAT , poolFactory ) ;
32+ }
33+
34+ static TimedResult [ ] BenchmarkResults < T > ( uint count , uint repeat , Func < IObjectPool < T > > poolFactory )
35+ where T : class
36+ {
37+ return Benchmark < T > . Results ( count , repeat , poolFactory ) ;
38+ }
39+
40+ static TimedResult [ ] BenchmarkResults < T > ( uint repeat , Func < IObjectPool < T > > poolFactory )
41+ where T : class
42+ {
43+ return Benchmark < T > . Results ( COUNT , repeat , poolFactory ) ;
44+ }
45+
46+ static void OutputResults < T > ( Func < IObjectPool < T > > poolFactory )
47+ where T : class
48+ {
49+ var results = BenchmarkResults ( poolFactory ) ;
50+ foreach ( var e in BenchmarkResults ( poolFactory ) )
51+ Console . WriteLine ( e ) ;
52+ Console . WriteLine ( "{0} Total" , results . Select ( r => r . Duration ) . Aggregate ( ( r1 , r2 ) => r1 + r2 ) ) ;
53+
54+ ConsoleNewLine ( ) ;
55+ }
56+
57+ static void Main ( string [ ] args )
58+ {
59+ Console . Write ( "Initializing..." ) ;
60+
61+ // Run once through first to scramble initial conditions.
62+ BenchmarkResults ( 100 , ( ) => ConcurrentBagObjectPool . Create < object > ( ) ) ;
63+ BenchmarkResults ( 100 , ( ) => LinkedListObjectPool . Create < object > ( ) ) ;
64+ BenchmarkResults ( 100 , ( ) => OptimisticArrayObjectPool . Create < object > ( ) ) ;
65+ // BenchmarkResults(100, () => BufferBlockObjectPool.Create<object>());
66+
67+ Console . SetCursorPosition ( 0 , Console . CursorTop ) ;
68+
69+ Console . WriteLine ( "ConcurrentBagObjectPool................................." ) ;
70+ OutputResults ( ( ) => ConcurrentBagObjectPool . Create < object > ( ) ) ;
71+
72+ Console . WriteLine ( "LinkedListObjectPool...................................." ) ;
73+ OutputResults ( ( ) => LinkedListObjectPool . Create < object > ( ) ) ;
74+
75+ Console . WriteLine ( "OptimisticArrayObjectPool..............................." ) ;
76+ OutputResults ( ( ) => OptimisticArrayObjectPool . Create < object > ( ) ) ;
77+
78+ //Console.WriteLine("BufferBlockObjectPool...................................");
79+ //OutputResults(() => BufferBlockObjectPool.Create<object>());
80+
81+ Console . WriteLine ( "(press any key when finished)" ) ;
82+ Console . ReadKey ( ) ;
83+ }
84+
85+
86+ static void OldTest ( )
87+ {
1288 var ts = new CancellationTokenSource ( ) ;
1389 ts . Cancel ( ) ;
1490 var task = Task . FromResult ( ts . Token ) ;
1591
1692
17- var pool = BufferBlockObjectPool . Create ( ( ) => new object ( ) , 1024 ) ;
93+ var pool = BufferBlockObjectPool . Create ( ( ) => new object ( ) , 1024 ) ;
1894 var trimmer = new ObjectPoolAutoTrimmer ( 20 , pool ) ;
1995 var clearer = new ObjectPoolAutoTrimmer ( 0 , pool , TimeSpan . FromSeconds ( 5 ) ) ;
2096 var tank = new ConcurrentBag < object > ( ) ;
2197
22- int count = 0 ;
23- while ( true )
24- {
98+ int count = 0 ;
99+ while ( true )
100+ {
25101 Console . WriteLine ( "Before {0}: {1}, {2}" , count , pool . Count , tank . Count ) ;
26102
27103 count ++ ;
28- tank . Add ( pool . Take ( ) ) ;
29- count ++ ;
104+ tank . Add ( pool . Take ( ) ) ;
105+ count ++ ;
30106 tank . Add ( new object ( ) ) ;
31107 count ++ ;
32108 tank . Add ( new object ( ) ) ;
33- foreach ( var o in tank . TryTakeWhile ( c => c . Count > 30 ) )
34- pool . Give ( o ) ;
109+ foreach ( var o in tank . TryTakeWhile ( c => c . Count > 30 ) )
110+ pool . Give ( o ) ;
35111
36- Console . WriteLine ( "After {0}: {1}, {2}" , count , pool . Count , tank . Count ) ;
112+ Console . WriteLine ( "After {0}: {1}, {2}" , count , pool . Count , tank . Count ) ;
37113
38114 if ( count % 30 == 0 )
39115 {
@@ -49,5 +125,6 @@ static void Main(string[] args)
49125 Console . WriteLine ( ) ;
50126
51127 }
128+
52129 }
53- }
130+ }
0 commit comments