11using System ;
2+ using System . Collections . Generic ;
23using JsonApiDotNetCore . Configuration ;
4+ using JsonApiDotNetCore . Diagnostics ;
35using JsonApiDotNetCoreExample . Data ;
6+ using JsonApiDotNetCoreExample . Models ;
47using Microsoft . AspNetCore . Authentication ;
58using Microsoft . AspNetCore . Builder ;
69using Microsoft . AspNetCore . Hosting ;
710using Microsoft . EntityFrameworkCore ;
811using Microsoft . Extensions . Configuration ;
912using Microsoft . Extensions . DependencyInjection ;
13+ using Microsoft . Extensions . Logging ;
1014using Newtonsoft . Json ;
1115using Newtonsoft . Json . Converters ;
1216
@@ -15,53 +19,139 @@ namespace JsonApiDotNetCoreExample.Startups
1519 public sealed class Startup : EmptyStartup
1620 {
1721 private readonly string _connectionString ;
22+ private readonly ICodeTimerSession _codeTimingSession ;
1823
1924 public Startup ( IConfiguration configuration )
2025 {
26+ _codeTimingSession = new DefaultCodeTimerSession ( ) ;
27+ CodeTimingSessionManager . Capture ( _codeTimingSession ) ;
28+
2129 string postgresPassword = Environment . GetEnvironmentVariable ( "PGPASSWORD" ) ?? "postgres" ;
2230 _connectionString = configuration [ "Data:DefaultConnection" ] . Replace ( "###" , postgresPassword ) ;
2331 }
2432
2533 // This method gets called by the runtime. Use this method to add services to the container.
2634 public override void ConfigureServices ( IServiceCollection services )
2735 {
28- services . AddSingleton < ISystemClock , SystemClock > ( ) ;
29-
30- services . AddDbContext < AppDbContext > ( options =>
36+ using ( CodeTimingSessionManager . Current . Measure ( "Configure other (startup)" ) )
3137 {
32- options . UseNpgsql ( _connectionString ) ;
38+ services . AddSingleton < ISystemClock , SystemClock > ( ) ;
39+
40+ services . AddDbContext < AppDbContext > ( options =>
41+ {
42+ options . UseNpgsql ( _connectionString ) ;
3343#if DEBUG
34- options . EnableSensitiveDataLogging ( ) ;
35- options . EnableDetailedErrors ( ) ;
44+ options . EnableSensitiveDataLogging ( ) ;
45+ options . EnableDetailedErrors ( ) ;
3646#endif
37- } ) ;
47+ } ) ;
3848
39- services . AddJsonApi < AppDbContext > ( options =>
40- {
41- options . Namespace = "api/v1" ;
42- options . UseRelativeLinks = true ;
43- options . ValidateModelState = true ;
44- options . IncludeTotalResourceCount = true ;
45- options . SerializerSettings . Formatting = Formatting . Indented ;
46- options . SerializerSettings . Converters . Add ( new StringEnumConverter ( ) ) ;
49+ using ( CodeTimingSessionManager . Current . Measure ( "Configure JSON:API (startup)" ) )
50+ {
51+ services . AddJsonApi < AppDbContext > ( options =>
52+ {
53+ options . Namespace = "api/v1" ;
54+ options . UseRelativeLinks = true ;
55+ options . ValidateModelState = true ;
56+ options . IncludeTotalResourceCount = true ;
57+ options . SerializerSettings . Formatting = Formatting . Indented ;
58+ options . SerializerSettings . Converters . Add ( new StringEnumConverter ( ) ) ;
4759#if DEBUG
48- options . IncludeExceptionStackTraceInErrors = true ;
60+ options . IncludeExceptionStackTraceInErrors = true ;
4961#endif
50- } , discovery => discovery . AddCurrentAssembly ( ) ) ;
62+ } , discovery => discovery . AddCurrentAssembly ( ) ) ;
63+ }
64+ }
5165 }
5266
5367 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
54- public override void Configure ( IApplicationBuilder app , IWebHostEnvironment environment )
68+ public override void Configure ( IApplicationBuilder app , IWebHostEnvironment environment , ILoggerFactory loggerFactory )
5569 {
56- using ( IServiceScope scope = app . ApplicationServices . CreateScope ( ) )
70+ using ( CodeTimingSessionManager . Current . Measure ( "Initialize other (startup)" ) )
71+ {
72+ ILogger < Startup > logger = loggerFactory . CreateLogger < Startup > ( ) ;
73+
74+ CreateTestData ( app ) ;
75+
76+ app . UseRouting ( ) ;
77+
78+ using ( CodeTimingSessionManager . Current . Measure ( "Initialize JSON:API (startup)" ) )
79+ {
80+ app . UseJsonApi ( ) ;
81+ }
82+
83+ app . UseEndpoints ( endpoints => endpoints . MapControllers ( ) ) ;
84+
85+ string result = CodeTimingSessionManager . Current . GetResult ( ) ;
86+ logger . LogWarning ( $ "Measurement results for application startup:{ Environment . NewLine } { result } ") ;
87+ }
88+
89+ _codeTimingSession . Dispose ( ) ;
90+ }
91+
92+ private static void CreateTestData ( IApplicationBuilder app )
93+ {
94+ using IServiceScope scope = app . ApplicationServices . CreateScope ( ) ;
95+
96+ var appDbContext = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
97+
98+ appDbContext . Database . EnsureDeleted ( ) ;
99+ appDbContext . Database . EnsureCreated ( ) ;
100+
101+ var tags = new List < Tag >
102+ {
103+ new Tag
104+ {
105+ Name = "Personal"
106+ } ,
107+ new Tag
108+ {
109+ Name = "Family"
110+ } ,
111+ new Tag
112+ {
113+ Name = "Work"
114+ }
115+ } ;
116+
117+ var owner = new Person
118+ {
119+ FirstName = "John" ,
120+ LastName = "Doe"
121+ } ;
122+
123+ var assignee = new Person
124+ {
125+ FirstName = "Jane" ,
126+ LastName = "Doe"
127+ } ;
128+
129+ for ( int index = 0 ; index < 20 ; index ++ )
57130 {
58- var appDbContext = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
59- appDbContext . Database . EnsureCreated ( ) ;
131+ appDbContext . TodoItems . Add ( new TodoItem
132+ {
133+ Description = $ "Task { index + 1 } ",
134+ Owner = owner ,
135+ Assignee = assignee ,
136+ TodoItemTags = new HashSet < TodoItemTag >
137+ {
138+ new TodoItemTag
139+ {
140+ Tag = tags [ 0 ]
141+ } ,
142+ new TodoItemTag
143+ {
144+ Tag = tags [ 1 ]
145+ } ,
146+ new TodoItemTag
147+ {
148+ Tag = tags [ 2 ]
149+ }
150+ }
151+ } ) ;
60152 }
61153
62- app . UseRouting ( ) ;
63- app . UseJsonApi ( ) ;
64- app . UseEndpoints ( endpoints => endpoints . MapControllers ( ) ) ;
154+ appDbContext . SaveChanges ( ) ;
65155 }
66156 }
67157}
0 commit comments