1+ // Copyright 2014-2015 Kasper B. Graversen
2+ //
3+ // Licensed to the Apache Software Foundation (ASF) under one
4+ // or more contributor license agreements. See the NOTICE file
5+ // distributed with this work for additional information
6+ // regarding copyright ownership. The ASF licenses this file
7+ // to you under the Apache License, Version 2.0 (the
8+ // "License"); you may not use this file except in compliance
9+ // with the License. You may obtain a copy of the License at
10+ //
11+ // http://www.apache.org/licenses/LICENSE-2.0
12+ //
13+ // Unless required by applicable law or agreed to in writing,
14+ // software distributed under the License is distributed on an
15+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+ // KIND, either express or implied. See the License for the
17+ // specific language governing permissions and limitations
18+ // under the License.
19+
20+ using System ;
21+ using System . Collections . Generic ;
22+ using NUnit . Framework ;
23+ using StatePrinting . Configurations ;
24+ using StatePrinting . OutputFormatters ;
25+
26+ namespace StatePrinting . Tests . PerformanceTests
27+ {
28+ [ TestFixture ]
29+ [ Explicit ]
30+ public class ManySmallCollections : PerformanceTestsBase
31+ {
32+ const int N = 1000000 ;
33+
34+ /// <summary>
35+ /// printing many times reveals the overhead of starting a print
36+ /// Version 3.0.0 - HPPavilion 7
37+ /// 3687
38+ /// </summary>
39+ [ Test ]
40+ public void TestTheOverheadOfStartingUp ( )
41+ {
42+ var toPrint = new Base ( ) ;
43+
44+ //var warmup =
45+ new Stateprinter ( ) . PrintObject ( toPrint ) ;
46+ new Stateprinter ( ) . PrintObject ( toPrint ) ;
47+ new Stateprinter ( ) . PrintObject ( toPrint ) ;
48+
49+ var mills = Time (
50+ ( ) =>
51+ {
52+ for ( int i = 0 ; i < N / 400 ; i ++ )
53+ {
54+ var printer = new Stateprinter ( ) ;
55+ printer . PrintObject ( toPrint ) ;
56+ }
57+ } ) ;
58+ Console . WriteLine ( " " + mills ) ;
59+ }
60+
61+
62+ /// <summary>
63+ /// Many small objects reveals the overhead of introspecting types.
64+ ///
65+ //1000: Time: 159 length 123003
66+ //2000: Time: 18 length 246003
67+ //4000: Time: 35 length 492003
68+ //8000: Time: 164 length 984003
69+ //16000: Time: 218 length 1968003
70+ //32000: Time: 323 length 3936003
71+ //64000: Time: 762 length 7872003
72+ //128000: Time: 1523 length 15744003
73+ //256000: Time: 2953 length 31488003
74+ //512000: Time: 5260 length 62976003
75+ //1024000: Time: 11159 length 125952003
76+ /// </summary>
77+ [ Test ]
78+ public void DumpManySmallCollections ( )
79+ {
80+ for ( int i = 1000 ; i <= N * 2 ; i *= 2 )
81+ {
82+ DumpNObjects ( i ) ;
83+ }
84+ }
85+
86+ /// <summary>
87+ /// Printing 1.000.000 objects.
88+ //curly: 17906 length: 195888912
89+ //json: 11433 length: 123000003
90+ //xml: 17106 length: 232000033
91+ /// </summary>
92+ [ Test ]
93+ public void TiminAllOutputFormattersAtNElements ( )
94+ {
95+ //var warmup
96+ new Stateprinter ( ) . PrintObject ( new ToDumpList ( ) ) ;
97+ new Stateprinter ( ) . PrintObject ( new ToDumpList ( ) ) ;
98+ new Stateprinter ( ) . PrintObject ( new ToDumpList ( ) ) ;
99+
100+ var x = CreateObjectsToDump ( N ) ;
101+ int length = 0 ;
102+ Console . WriteLine ( "Printing {0:0,0} objects." , N ) ;
103+
104+ var curly = new Stateprinter ( ) ;
105+ curly . Configuration . SetOutputFormatter ( new CurlyBraceStyle ( curly . Configuration ) ) ;
106+ long time = Time ( ( ) => length = curly . PrintObject ( x ) . Length ) ;
107+ Console . WriteLine ( "curly: {0} length: {1,10}" , time , length ) ;
108+
109+ var json = new Stateprinter ( ) ;
110+ json . Configuration . SetOutputFormatter ( new JsonStyle ( json . Configuration ) ) ;
111+ time = Time ( ( ) => length = json . PrintObject ( x ) . Length ) ;
112+ Console . WriteLine ( "json: {0} length: {1,10}" , time , length ) ;
113+
114+ var xml = new Stateprinter ( ) ;
115+ xml . Configuration . SetOutputFormatter ( new XmlStyle ( xml . Configuration ) ) ;
116+ time = Time ( ( ) => length = xml . PrintObject ( x ) . Length ) ;
117+ Console . WriteLine ( "xml: {0} length: {1,10}" , time , length ) ;
118+ }
119+
120+ private void DumpNObjects ( int max )
121+ {
122+ List < Base > x = CreateObjectsToDump ( max ) ;
123+
124+ var cfg = ConfigurationHelper . GetStandardConfiguration ( ) ;
125+ cfg . OutputFormatter = new JsonStyle ( cfg ) ;
126+ int length = 0 ;
127+ var mills = Time ( ( ) =>
128+ {
129+ var printer = new Stateprinter ( cfg ) ;
130+ var res = printer . PrintObject ( x ) ;
131+ length = res . Length ;
132+ } ) ;
133+ Console . WriteLine ( "{0,8}: Time: {1,6} length {2,10}" , max , mills , length ) ;
134+ }
135+
136+ static List < Base > CreateObjectsToDump ( int max )
137+ {
138+ var x = new List < Base > ( max ) ;
139+ for ( int i = 0 ; i < max / 2 ; i ++ )
140+ {
141+ x . Add ( new ToDumpList ( ) ) ;
142+ }
143+ for ( int i = 0 ; i < max / 2 ; i ++ )
144+ {
145+ x . Add ( new ToDumpList ( ) ) ;
146+ }
147+ return x ;
148+ }
149+
150+ internal class Base
151+ {
152+ internal string Boo = null ;
153+ }
154+
155+ private class ToDumpList : Base
156+ {
157+ internal List < string > Poos = new List < string > ( ) { "dd" , "bb" , "cc" } ;
158+ }
159+ private class ToDumpDic : Base
160+ {
161+ internal Dictionary < string , bool > Poos = new Dictionary < string , bool > ( ) { { "aa" , false } , { "bb" , true } } ;
162+ }
163+ }
164+ }
0 commit comments