66using System . Collections . Generic ;
77using System . Diagnostics ;
88using System . Linq ;
9+ using System . Text . RegularExpressions ;
910using System . Threading ;
1011using System . Threading . Tasks ;
1112
@@ -22,34 +23,114 @@ static void ConsoleNewLine()
2223 Console . WriteLine ( ) ;
2324 }
2425
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- }
26+ const int ITERATIONS = 100000 ;
3327
3428 static TimedResult [ ] BenchmarkResults < T > ( uint count , uint repeat , Func < IObjectPool < T > > poolFactory )
3529 where T : class
3630 {
3731 return Benchmark < T > . Results ( count , repeat , poolFactory ) ;
3832 }
3933
40- static TimedResult [ ] BenchmarkResults < T > ( uint repeat , Func < IObjectPool < T > > poolFactory )
41- where T : class
34+ static readonly Regex TimeSpanRegex = new Regex ( @"((?:00:)+ (?:0\B)?) ([0.]*) (\S*)" , RegexOptions . IgnorePatternWhitespace ) ;
35+
36+ static void OutputResult ( TimeSpan result )
4237 {
43- return Benchmark < T > . Results ( COUNT , repeat , poolFactory ) ;
38+ var match = TimeSpanRegex . Match ( result . ToString ( ) ) ;
39+ Console . ForegroundColor = ConsoleColor . Black ;
40+ Console . Write ( match . Groups [ 1 ] . Value ) ;
41+ Console . ForegroundColor = ConsoleColor . DarkGray ;
42+ Console . Write ( match . Groups [ 2 ] . Value ) ;
43+ Console . ResetColor ( ) ;
44+ Console . Write ( match . Groups [ 3 ] . Value ) ;
4445 }
4546
46- static void OutputResults < T > ( Func < IObjectPool < T > > poolFactory )
47+ static void OutputResult ( TimedResult result , ConsoleColor ? labelColor = null )
48+ {
49+ OutputResult ( result . Duration ) ;
50+ Console . Write ( ' ' ) ;
51+ if ( labelColor . HasValue ) Console . ForegroundColor = labelColor . Value ;
52+ Console . WriteLine ( result . Label . Substring ( 4 ) ) ;
53+ Console . ResetColor ( ) ;
54+ }
55+
56+ static void OutputResult ( TimedResult result , TimedResult [ ] all )
57+ {
58+ var duration = result . Duration ;
59+ OutputResult ( duration ) ;
60+ Console . Write ( ' ' ) ;
61+ var these = all . Where ( r => r . Label == result . Label ) . Select ( r=> r . Duration ) . OrderBy ( d=> d ) . ToArray ( ) ;
62+ var min = these . First ( ) ;
63+ var max = these . Last ( ) ;
64+ if ( min != max )
65+ {
66+ if ( duration == min )
67+ Console . ForegroundColor = ConsoleColor . Green ;
68+ else if ( duration == max )
69+ Console . ForegroundColor = ConsoleColor . Red ;
70+ }
71+ Console . WriteLine ( result . Label . Substring ( 4 ) ) ;
72+ Console . ResetColor ( ) ;
73+ }
74+
75+ static void OutputResults ( IEnumerable < TimedResult > results )
76+ {
77+ foreach ( var e in results )
78+ OutputResult ( e ) ;
79+
80+ ConsoleNewLine ( ) ;
81+ }
82+
83+ static void OutputResults ( IEnumerable < TimedResult > results , TimedResult [ ] all )
84+ {
85+ foreach ( var e in results )
86+ OutputResult ( e , all ) ;
87+
88+ ConsoleNewLine ( ) ;
89+ }
90+
91+ static TimedResult [ ] OutputResults < T > ( uint count , uint repeat , Func < IObjectPool < T > > poolFactory )
4792 where T : class
4893 {
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 ) ) ;
94+ var results = BenchmarkResults ( count , repeat , poolFactory ) ;
95+ OutputResults ( results ) ;
96+ return results ;
97+ }
98+
99+ static void Test ( uint count , uint multiple = 1 )
100+ {
101+ var repeat = multiple * ITERATIONS / count ;
102+ Console . ForegroundColor = ConsoleColor . Cyan ;
103+ Console . WriteLine ( "Repeat {1:g} for size {0:g}" , count , repeat ) ;
104+ ConsoleSeparator ( ) ;
105+ Console . ResetColor ( ) ;
106+ ConsoleNewLine ( ) ;
107+
108+ var cursor = Console . CursorTop ;
109+ var results = new List < Tuple < string , TimedResult [ ] > > ( 3 ) ;
110+
111+ string header ;
112+
113+ Console . WriteLine ( header = "ConcurrentBagObjectPool................................." ) ;
114+ results . Add ( Tuple . Create ( header , OutputResults ( count , repeat , ( ) => ConcurrentBagObjectPool . Create < object > ( ( int ) count * 2 ) ) ) ) ;
115+
116+ Console . WriteLine ( header = "LinkedListObjectPool...................................." ) ;
117+ results . Add ( Tuple . Create ( header , OutputResults ( count , repeat , ( ) => LinkedListObjectPool . Create < object > ( ( int ) count * 2 ) ) ) ) ;
118+
119+ Console . WriteLine ( header = "OptimisticArrayObjectPool..............................." ) ;
120+ results . Add ( Tuple . Create ( header , OutputResults ( count , repeat , ( ) => OptimisticArrayObjectPool . Create < object > ( ( int ) count * 2 ) ) ) ) ;
121+
122+ //Console.WriteLine(header = "BufferBlockObjectPool...................................");
123+ //results.Add(Tuple.Create(header, OutputResults(count, repeat, () => BufferBlockObjectPool.Create<object>((int)count * 2))));
124+
125+ var all = results . SelectMany ( r => r . Item2 ) . ToArray ( ) ;
126+
127+ Console . SetCursorPosition ( 0 , cursor ) ;
128+
129+ foreach ( var r in results )
130+ {
131+ Console . WriteLine ( r . Item1 ) ;
132+ OutputResults ( r . Item2 , all ) ;
133+ }
53134
54135 ConsoleNewLine ( ) ;
55136 }
@@ -58,25 +139,22 @@ static void Main(string[] args)
58139 {
59140 Console . Write ( "Initializing..." ) ;
60141
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>());
142+ // Run once through first to scramble/warm-up initial conditions.
143+ BenchmarkResults ( 100 , 100 , ( ) => ConcurrentBagObjectPool . Create < object > ( 200 ) ) ;
144+ BenchmarkResults ( 100 , 100 , ( ) => LinkedListObjectPool . Create < object > ( 200 ) ) ;
145+ BenchmarkResults ( 100 , 100 , ( ) => OptimisticArrayObjectPool . Create < object > ( 200 ) ) ;
146+ // BenchmarkResults(100, 100, () => BufferBlockObjectPool.Create<object>());
66147
67148 Console . SetCursorPosition ( 0 , Console . CursorTop ) ;
68149
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 > ( ) ) ;
150+ Test ( 4 ) ;
151+ Test ( 10 , 2 ) ;
152+ Test ( 100 , 8 ) ;
153+ Test ( 250 , 16 ) ;
154+ Test ( 1000 , 24 ) ;
155+ Test ( 2000 , 32 ) ;
156+ Test ( 4000 , 48 ) ;
77157
78- //Console.WriteLine("BufferBlockObjectPool...................................");
79- //OutputResults(() => BufferBlockObjectPool.Create<object>());
80158
81159 Console . WriteLine ( "(press any key when finished)" ) ;
82160 Console . ReadKey ( ) ;
0 commit comments