1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using JsonApiDotNetCore . Configuration ;
4
+ using JsonApiDotNetCore . Diagnostics ;
3
5
using JsonApiDotNetCoreExample . Data ;
6
+ using JsonApiDotNetCoreExample . Models ;
4
7
using Microsoft . AspNetCore . Authentication ;
5
8
using Microsoft . AspNetCore . Builder ;
6
9
using Microsoft . AspNetCore . Hosting ;
7
10
using Microsoft . EntityFrameworkCore ;
8
11
using Microsoft . Extensions . Configuration ;
9
12
using Microsoft . Extensions . DependencyInjection ;
13
+ using Microsoft . Extensions . Logging ;
10
14
using Newtonsoft . Json ;
11
15
using Newtonsoft . Json . Converters ;
12
16
@@ -15,53 +19,139 @@ namespace JsonApiDotNetCoreExample.Startups
15
19
public sealed class Startup : EmptyStartup
16
20
{
17
21
private readonly string _connectionString ;
22
+ private readonly ICodeTimerSession _codeTimingSession ;
18
23
19
24
public Startup ( IConfiguration configuration )
20
25
{
26
+ _codeTimingSession = new DefaultCodeTimerSession ( ) ;
27
+ CodeTimingSessionManager . Capture ( _codeTimingSession ) ;
28
+
21
29
string postgresPassword = Environment . GetEnvironmentVariable ( "PGPASSWORD" ) ?? "postgres" ;
22
30
_connectionString = configuration [ "Data:DefaultConnection" ] . Replace ( "###" , postgresPassword ) ;
23
31
}
24
32
25
33
// This method gets called by the runtime. Use this method to add services to the container.
26
34
public override void ConfigureServices ( IServiceCollection services )
27
35
{
28
- services . AddSingleton < ISystemClock , SystemClock > ( ) ;
29
-
30
- services . AddDbContext < AppDbContext > ( options =>
36
+ using ( CodeTimingSessionManager . Current . Measure ( "Configure other (startup)" ) )
31
37
{
32
- options . UseNpgsql ( _connectionString ) ;
38
+ services . AddSingleton < ISystemClock , SystemClock > ( ) ;
39
+
40
+ services . AddDbContext < AppDbContext > ( options =>
41
+ {
42
+ options . UseNpgsql ( _connectionString ) ;
33
43
#if DEBUG
34
- options . EnableSensitiveDataLogging ( ) ;
35
- options . EnableDetailedErrors ( ) ;
44
+ options . EnableSensitiveDataLogging ( ) ;
45
+ options . EnableDetailedErrors ( ) ;
36
46
#endif
37
- } ) ;
47
+ } ) ;
38
48
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 ( ) ) ;
47
59
#if DEBUG
48
- options . IncludeExceptionStackTraceInErrors = true ;
60
+ options . IncludeExceptionStackTraceInErrors = true ;
49
61
#endif
50
- } , discovery => discovery . AddCurrentAssembly ( ) ) ;
62
+ } , discovery => discovery . AddCurrentAssembly ( ) ) ;
63
+ }
64
+ }
51
65
}
52
66
53
67
// 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 )
55
69
{
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 ++ )
57
130
{
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
+ } ) ;
60
152
}
61
153
62
- app . UseRouting ( ) ;
63
- app . UseJsonApi ( ) ;
64
- app . UseEndpoints ( endpoints => endpoints . MapControllers ( ) ) ;
154
+ appDbContext . SaveChanges ( ) ;
65
155
}
66
156
}
67
157
}
0 commit comments