├── .gitattributes ├── .gitignore ├── DapperEFCorePerformanceBenchmarks.sln └── DapperEFCorePerformanceBenchmarks ├── Constants.cs ├── DTOs ├── PlayerDTO.cs ├── SportDTO.cs └── TeamDTO.cs ├── DapperEFCorePerformanceBenchmarks.csproj ├── DataAccess ├── Dapper.cs ├── EntityFrameworkCore.cs ├── EntityFrameworkCoreWithTracking.cs └── ITestSignature.cs ├── Models ├── Player.cs ├── Sport.cs ├── SportContextEFCore.cs └── Team.cs ├── Program.cs └── TestData ├── Database.cs ├── Generator.cs ├── Names.cs └── TestResult.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29418.71 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DapperEFCorePerformanceBenchmarks", "DapperEFCorePerformanceBenchmarks\DapperEFCorePerformanceBenchmarks.csproj", "{BEEDECD0-FF2D-409A-BC79-EF4362AA5EA6}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {BEEDECD0-FF2D-409A-BC79-EF4362AA5EA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {BEEDECD0-FF2D-409A-BC79-EF4362AA5EA6}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {BEEDECD0-FF2D-409A-BC79-EF4362AA5EA6}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {BEEDECD0-FF2D-409A-BC79-EF4362AA5EA6}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {96729221-9846-4BE8-AD1A-F660DCDAD93E} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks 6 | { 7 | public static class Constants 8 | { 9 | /// 10 | /// The connection string is not included in the sample project as, in my setup, it was too unique. 11 | /// Consequently you will need to set up your own connection to a SQL database and use your own connection string. 12 | /// 13 | public static readonly string SportsConnectionString = "yourconnectionstring"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DTOs/PlayerDTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.DTOs 6 | { 7 | public class PlayerDTO 8 | { 9 | public int Id { get; set; } 10 | public string FirstName { get; set; } 11 | public string LastName { get; set; } 12 | public DateTime DateOfBirth { get; set; } 13 | public int TeamId { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DTOs/SportDTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.DTOs 6 | { 7 | public class SportDTO 8 | { 9 | public int Id { get; set; } 10 | public string Name { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DTOs/TeamDTO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.DTOs 6 | { 7 | public class TeamDTO 8 | { 9 | public int Id { get; set; } 10 | public int SportId { get; set; } 11 | public string Name { get; set; } 12 | public DateTime FoundingDate { get; set; } 13 | 14 | public List Players { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DapperEFCorePerformanceBenchmarks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DataAccess/Dapper.cs: -------------------------------------------------------------------------------- 1 | using Dapper; 2 | using DapperEFCorePerformanceBenchmarks.DTOs; 3 | using System.Collections.Generic; 4 | using System.Data.SqlClient; 5 | using System.Diagnostics; 6 | using System.Linq; 7 | 8 | namespace DapperEFCorePerformanceBenchmarks.DataAccess 9 | { 10 | public class Dapper : ITestSignature 11 | { 12 | public long GetPlayerByID(int id) 13 | { 14 | Stopwatch watch = new Stopwatch(); 15 | watch.Start(); 16 | using (SqlConnection conn = new SqlConnection(Constants.SportsConnectionString)) 17 | { 18 | conn.Open(); 19 | var player = conn.QuerySingle("SELECT Id, FirstName, LastName, DateOfBirth, TeamId FROM Player WHERE Id = @ID", new { ID = id }); 20 | } 21 | watch.Stop(); 22 | return watch.ElapsedMilliseconds; 23 | } 24 | 25 | public long GetRosterByTeamID(int teamId) 26 | { 27 | Stopwatch watch = new Stopwatch(); 28 | watch.Start(); 29 | using (SqlConnection conn = new SqlConnection(Constants.SportsConnectionString)) 30 | { 31 | conn.Open(); 32 | var team = conn.QuerySingle("SELECT Id, Name, SportID, FoundingDate FROM Team WHERE ID = @id", new { id = teamId }); 33 | 34 | team.Players = conn.Query("SELECT Id, FirstName, LastName, DateOfBirth, TeamId FROM Player WHERE TeamId = @ID", new { ID = teamId }).ToList(); 35 | } 36 | watch.Stop(); 37 | return watch.ElapsedMilliseconds; 38 | } 39 | 40 | public long GetTeamRostersForSport(int sportId) 41 | { 42 | Stopwatch watch = new Stopwatch(); 43 | watch.Start(); 44 | using (SqlConnection conn = new SqlConnection(Constants.SportsConnectionString)) 45 | { 46 | conn.Open(); 47 | var teams = conn.Query("SELECT ID, Name, SportID, FoundingDate FROM Team WHERE SportID = @ID", new { ID = sportId }); 48 | 49 | var teamIDs = teams.Select(x => x.Id).ToList(); 50 | 51 | var players = conn.Query("SELECT ID, FirstName, LastName, DateOfBirth, TeamID FROM Player WHERE TeamID IN @IDs", new { IDs = teamIDs }); 52 | 53 | foreach (var team in teams) 54 | { 55 | team.Players = players.Where(x => x.TeamId == team.Id).ToList(); 56 | } 57 | } 58 | watch.Stop(); 59 | return watch.ElapsedMilliseconds; 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DataAccess/EntityFrameworkCore.cs: -------------------------------------------------------------------------------- 1 | using DapperEFCorePerformanceBenchmarks.Models; 2 | using DapperEFCorePerformanceBenchmarks.TestData; 3 | using Microsoft.EntityFrameworkCore; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | 7 | namespace DapperEFCorePerformanceBenchmarks.DataAccess 8 | { 9 | public class EntityFrameworkCore : ITestSignature 10 | { 11 | public long GetPlayerByID(int id) 12 | { 13 | Stopwatch watch = new Stopwatch(); 14 | watch.Start(); 15 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 16 | { 17 | var player = context.Players.First(x => x.Id == id); 18 | } 19 | watch.Stop(); 20 | return watch.ElapsedMilliseconds; 21 | } 22 | 23 | public long GetRosterByTeamID(int teamId) 24 | { 25 | Stopwatch watch = new Stopwatch(); 26 | watch.Start(); 27 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 28 | { 29 | var players = context.Teams.Include(x => x.Players).AsNoTracking().Single(x => x.Id == teamId); 30 | } 31 | watch.Stop(); 32 | return watch.ElapsedMilliseconds; 33 | } 34 | 35 | public long GetTeamRostersForSport(int sportId) 36 | { 37 | Stopwatch watch = new Stopwatch(); 38 | watch.Start(); 39 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 40 | { 41 | var players = context.Teams.Include(x => x.Players).Where(x => x.SportId == sportId).AsNoTracking().ToList(); 42 | } 43 | watch.Stop(); 44 | return watch.ElapsedMilliseconds; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DataAccess/EntityFrameworkCoreWithTracking.cs: -------------------------------------------------------------------------------- 1 | using DapperEFCorePerformanceBenchmarks.DataAccess; 2 | using DapperEFCorePerformanceBenchmarks.Models; 3 | using DapperEFCorePerformanceBenchmarks.TestData; 4 | using Microsoft.EntityFrameworkCore; 5 | using System.Diagnostics; 6 | using System.Linq; 7 | 8 | public class EntityFrameworkCoreWithTracking : ITestSignature 9 | { 10 | public long GetPlayerByID(int id) 11 | { 12 | Stopwatch watch = new Stopwatch(); 13 | watch.Start(); 14 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 15 | { 16 | var player = context.Players.First(x=>x.Id == id); 17 | } 18 | watch.Stop(); 19 | return watch.ElapsedMilliseconds; 20 | } 21 | 22 | public long GetRosterByTeamID(int teamId) 23 | { 24 | Stopwatch watch = new Stopwatch(); 25 | watch.Start(); 26 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 27 | { 28 | var teamRoster = context.Teams.Include(x => x.Players).Single(x => x.Id == teamId); 29 | } 30 | watch.Stop(); 31 | return watch.ElapsedMilliseconds; 32 | } 33 | 34 | public long GetTeamRostersForSport(int sportId) 35 | { 36 | Stopwatch watch = new Stopwatch(); 37 | watch.Start(); 38 | using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) 39 | { 40 | var players = context.Teams.Include(x => x.Players).Where(x => x.SportId == sportId).ToList(); 41 | } 42 | watch.Stop(); 43 | return watch.ElapsedMilliseconds; 44 | } 45 | } -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/DataAccess/ITestSignature.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.DataAccess 6 | { 7 | public interface ITestSignature 8 | { 9 | long GetPlayerByID(int id); 10 | long GetRosterByTeamID(int teamID); 11 | long GetTeamRostersForSport(int sportID); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Models/Player.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | using System.Text; 6 | 7 | namespace DapperEFCorePerformanceBenchmarks.Models 8 | { 9 | [Table("Player")] 10 | public partial class Player 11 | { 12 | [DatabaseGenerated(DatabaseGeneratedOption.None)] 13 | public int Id { get; set; } 14 | 15 | [Required] 16 | [StringLength(200)] 17 | public string FirstName { get; set; } 18 | 19 | [Required] 20 | [StringLength(200)] 21 | public string LastName { get; set; } 22 | 23 | public DateTime DateOfBirth { get; set; } 24 | 25 | public int TeamId { get; set; } 26 | 27 | public virtual Team Team { get; set; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Models/Sport.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | using System.Text; 6 | 7 | namespace DapperEFCorePerformanceBenchmarks.Models 8 | { 9 | [Table("Sport")] 10 | public partial class Sport 11 | { 12 | public Sport() 13 | { 14 | Teams = new HashSet(); 15 | } 16 | 17 | [DatabaseGenerated(DatabaseGeneratedOption.None)] 18 | public int Id { get; set; } 19 | 20 | [StringLength(100)] 21 | public string Name { get; set; } 22 | 23 | public virtual ICollection Teams { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Models/SportContextEFCore.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Text; 6 | using System.Text.RegularExpressions; 7 | 8 | namespace DapperEFCorePerformanceBenchmarks.Models 9 | { 10 | public partial class SportContextEfCore : DbContext 11 | { 12 | public virtual DbSet Players { get; set; } 13 | public virtual DbSet Sports { get; set; } 14 | public virtual DbSet Teams { get; set; } 15 | 16 | public SportContextEfCore(DbContextOptions options) : base(options) { } 17 | 18 | protected override void OnModelCreating(ModelBuilder modelBuilder) 19 | { 20 | modelBuilder.Entity() 21 | .HasMany(e => e.Teams); 22 | 23 | 24 | modelBuilder.Entity() 25 | .HasMany(e => e.Players); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Models/Team.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | using System.Text; 6 | 7 | namespace DapperEFCorePerformanceBenchmarks.Models 8 | { 9 | [Table("Team")] 10 | public partial class Team 11 | { 12 | public Team() 13 | { 14 | Players = new HashSet(); 15 | } 16 | 17 | [DatabaseGenerated(DatabaseGeneratedOption.None)] 18 | public int Id { get; set; } 19 | 20 | [Required] 21 | [StringLength(200)] 22 | public string Name { get; set; } 23 | 24 | public DateTime FoundingDate { get; set; } 25 | 26 | public int SportId { get; set; } 27 | 28 | public virtual ICollection Players { get; set; } 29 | 30 | public virtual Sport Sport { get; set; } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using DapperEFCorePerformanceBenchmarks.DataAccess; 2 | using DapperEFCorePerformanceBenchmarks.DTOs; 3 | using DapperEFCorePerformanceBenchmarks.TestData; 4 | using Microsoft.EntityFrameworkCore; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.IO; 8 | using System.Linq; 9 | 10 | namespace DapperEFCorePerformanceBenchmarks 11 | { 12 | class Program 13 | { 14 | public static int NumPlayers { get; set; } 15 | public static int NumTeams { get; set; } 16 | public static int NumSports { get; set; } 17 | public static int NumRuns { get; set; } 18 | static void Main(string[] args) 19 | { 20 | char input; 21 | do 22 | { 23 | ShowMenu(); 24 | 25 | input = Console.ReadLine().First(); 26 | switch (input) 27 | { 28 | case 'Q': 29 | break; 30 | 31 | case 'T': 32 | List testResults = new List(); 33 | 34 | Console.WriteLine("# of Test Runs:"); 35 | NumRuns = int.Parse(Console.ReadLine()); 36 | 37 | //Gather Details for Test 38 | Console.WriteLine("# of Sports per Run: "); 39 | NumSports = int.Parse(Console.ReadLine()); 40 | 41 | Console.WriteLine("# of Teams per Sport: "); 42 | NumTeams = int.Parse(Console.ReadLine()); 43 | 44 | Console.WriteLine("# of Players per Team: "); 45 | NumPlayers = int.Parse(Console.ReadLine()); 46 | 47 | 48 | List sports = TestData.Generator.GenerateSports(NumSports); 49 | List teams = new List(); 50 | List players = new List(); 51 | foreach (var sport in sports) 52 | { 53 | var newTeams = TestData.Generator.GenerateTeams(sport.Id, NumTeams); 54 | teams.AddRange(newTeams); 55 | foreach (var team in newTeams) 56 | { 57 | var newPlayers = TestData.Generator.GeneratePlayers(team.Id, NumPlayers); 58 | players.AddRange(newPlayers); 59 | } 60 | } 61 | 62 | Database.Reset(); 63 | Database.Load(sports, teams, players); 64 | 65 | for (int i = 0; i < NumRuns + 1; i++) 66 | { 67 | if(i == 0) 68 | {//Discard first runs 69 | DataAccess.Dapper firstDapperTest = new DataAccess.Dapper(); 70 | RunTests(i, Framework.Dapper, firstDapperTest); 71 | 72 | EntityFrameworkCore firstEfCoreTest = new EntityFrameworkCore(); 73 | RunTests(i, Framework.EntityFrameworkCore, firstEfCoreTest); 74 | 75 | EntityFrameworkCoreWithTracking firstTrackingTest = new EntityFrameworkCoreWithTracking(); 76 | RunTests(i, Framework.EntityFrameworkCoreWithTracking, firstTrackingTest); 77 | } 78 | 79 | //Run real tests 80 | DataAccess.Dapper dapperTest = new DataAccess.Dapper(); 81 | testResults.AddRange(RunTests(i, Framework.Dapper, dapperTest)); 82 | 83 | EntityFrameworkCoreWithTracking trackingTest = new EntityFrameworkCoreWithTracking(); 84 | testResults.AddRange(RunTests(i, Framework.EntityFrameworkCoreWithTracking, trackingTest)); 85 | 86 | EntityFrameworkCore efCoreTest = new EntityFrameworkCore(); 87 | testResults.AddRange(RunTests(i, Framework.EntityFrameworkCore, efCoreTest)); 88 | } 89 | ProcessResults(testResults); 90 | 91 | break; 92 | } 93 | 94 | } 95 | while (input != 'Q'); 96 | } 97 | 98 | 99 | public static List RunTests(int runID, Framework framework, ITestSignature testSignature) 100 | { 101 | List results = new List(); 102 | 103 | TestResult result = new TestResult() { Run = runID, Framework = framework }; 104 | List playerByIDResults = new List(); 105 | for (int i = 1; i <= NumPlayers; i++) 106 | { 107 | playerByIDResults.Add(testSignature.GetPlayerByID(i)); 108 | } 109 | result.PlayerByIDMilliseconds = Math.Round(playerByIDResults.Average(), 3); 110 | 111 | List playersForTeamResults = new List(); 112 | for (int i = 1; i <= NumTeams; i++) 113 | { 114 | playersForTeamResults.Add(testSignature.GetRosterByTeamID(i)); 115 | } 116 | result.PlayersForTeamMilliseconds = Math.Round(playersForTeamResults.Average(), 3); 117 | List teamsForSportResults = new List(); 118 | for (int i = 1; i <= NumSports; i++) 119 | { 120 | teamsForSportResults.Add(testSignature.GetTeamRostersForSport(i)); 121 | } 122 | result.TeamsForSportMilliseconds = Math.Round(teamsForSportResults.Average(), 3); 123 | results.Add(result); 124 | 125 | return results; 126 | } 127 | 128 | public static void ProcessResults(List results) 129 | { 130 | var groupedResults = results.GroupBy(x => x.Framework); 131 | foreach (var group in groupedResults) 132 | { 133 | Console.WriteLine(group.Key.ToString() + " Results"); 134 | Console.WriteLine("Run #\tPlayer by ID\t\tPlayers per Team\t\tTeams per Sport"); 135 | var orderedResults = group.OrderBy(x => x.Run); 136 | foreach (var orderResult in orderedResults) 137 | { 138 | Console.WriteLine(orderResult.Run.ToString() + "\t\t" + orderResult.PlayerByIDMilliseconds + "\t\t\t" + orderResult.PlayersForTeamMilliseconds + "\t\t\t" + orderResult.TeamsForSportMilliseconds); 139 | } 140 | } 141 | } 142 | 143 | public static void ShowMenu() 144 | { 145 | Console.WriteLine("Please enter one of the following options:"); 146 | Console.WriteLine("Q - Quit"); 147 | Console.WriteLine("T - Run Test"); 148 | Console.WriteLine("Option:"); 149 | } 150 | } 151 | } -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/TestData/Database.cs: -------------------------------------------------------------------------------- 1 | using DapperEFCorePerformanceBenchmarks.DTOs; 2 | using DapperEFCorePerformanceBenchmarks.Models; 3 | using Microsoft.EntityFrameworkCore; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | 8 | namespace DapperEFCorePerformanceBenchmarks.TestData 9 | { 10 | public static class Database 11 | { 12 | public static void Reset() 13 | { 14 | using (SportContextEfCore context = new SportContextEfCore(GetOptions())) 15 | { 16 | context.Database.ExecuteSqlRaw("DELETE FROM Player"); 17 | context.Database.ExecuteSqlRaw("DELETE FROM Team"); 18 | context.Database.ExecuteSqlRaw("DELETE FROM Sport"); 19 | } 20 | } 21 | 22 | public static void Load(List sports, List teams, List players) 23 | { 24 | AddSports(sports); 25 | AddTeams(teams); 26 | AddPlayers(players); 27 | } 28 | 29 | private static void AddPlayers(List players) 30 | { 31 | using (SportContextEfCore context = new SportContextEfCore(GetOptions())) 32 | { 33 | foreach (var player in players) 34 | { 35 | context.Players.Add(new Player() 36 | { 37 | FirstName = player.FirstName, 38 | LastName = player.LastName, 39 | DateOfBirth = player.DateOfBirth, 40 | TeamId = player.TeamId, 41 | Id = player.Id 42 | }); 43 | } 44 | 45 | context.SaveChanges(); 46 | } 47 | } 48 | 49 | private static void AddTeams(List teams) 50 | { 51 | using (SportContextEfCore context = new SportContextEfCore(GetOptions())) 52 | { 53 | foreach (var team in teams) 54 | { 55 | context.Teams.Add(new Team() 56 | { 57 | Name = team.Name, 58 | Id = team.Id, 59 | SportId = team.SportId, 60 | FoundingDate = team.FoundingDate 61 | }); 62 | } 63 | 64 | context.SaveChanges(); 65 | } 66 | } 67 | 68 | private static void AddSports(List sports) 69 | { 70 | using (SportContextEfCore context = new SportContextEfCore(GetOptions())) 71 | { 72 | foreach (var sport in sports) 73 | { 74 | context.Sports.Add(new Sport() 75 | { 76 | Id = sport.Id, 77 | Name = sport.Name 78 | }); 79 | } 80 | 81 | context.SaveChanges(); 82 | } 83 | } 84 | 85 | public static DbContextOptions GetOptions() 86 | { 87 | DbContextOptionsBuilder builder = new DbContextOptionsBuilder(); 88 | builder.UseSqlServer(Constants.SportsConnectionString); 89 | 90 | return builder.Options; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/TestData/Generator.cs: -------------------------------------------------------------------------------- 1 | using DapperEFCorePerformanceBenchmarks.DTOs; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace DapperEFCorePerformanceBenchmarks.TestData 7 | { 8 | public static class Generator 9 | { 10 | public static List GeneratePlayers(int teamId, int count) 11 | { 12 | List players = new List(); 13 | 14 | var allFirstNames = Names.GetFirstNames(); 15 | var allLastNames = Names.GetLastNames(); 16 | Random rand = new Random(); 17 | DateTime start = new DateTime(1975, 1, 1); 18 | DateTime end = new DateTime(2001, 1, 1); 19 | 20 | for (int i = 0; i < count; i++) 21 | { 22 | PlayerDTO player = new PlayerDTO(); 23 | int newFirst = rand.Next(0, allFirstNames.Count - 1); 24 | player.FirstName = allFirstNames[newFirst]; 25 | int newLast = rand.Next(0, allLastNames.Count - 1); 26 | player.LastName = allLastNames[newLast]; 27 | player.DateOfBirth = RandomDay(rand, start, end); 28 | player.TeamId = teamId; 29 | player.Id = (((teamId - 1) * count) + (i + 1)); 30 | players.Add(player); 31 | } 32 | 33 | return players; 34 | } 35 | 36 | public static List GenerateTeams(int sportId, int count) 37 | { 38 | List teams = new List(); 39 | 40 | var allCityNames = Names.GetCityNames(); 41 | var allTeamNames = Names.GetTeamNames(); 42 | Random rand = new Random(); 43 | DateTime start = new DateTime(1900, 1, 1); 44 | DateTime end = new DateTime(2016, 1, 1); 45 | 46 | for (int i = 0; i < count; i++) 47 | { 48 | TeamDTO team = new TeamDTO(); 49 | int newCity = rand.Next(0, allCityNames.Count - 1); 50 | int newTeam = rand.Next(0, allTeamNames.Count - 1); 51 | team.Name = allCityNames[newCity] + " " + allTeamNames[newTeam]; 52 | team.FoundingDate = RandomDay(rand, start, end); 53 | team.SportId = sportId; 54 | team.Id = (((sportId - 1) * count) + (i + 1)); 55 | teams.Add(team); 56 | } 57 | 58 | return teams; 59 | } 60 | 61 | public static List GenerateSports(int count) 62 | { 63 | List sports = new List(); 64 | var allSportNames = Names.GetSportNames(); 65 | Random rand = new Random(); 66 | 67 | for (int i = 0; i < count; i++) 68 | { 69 | int newSport = rand.Next(0, allSportNames.Count - 1); 70 | sports.Add(new SportDTO() 71 | { 72 | Name = allSportNames[newSport], 73 | Id = i + 1 74 | }); 75 | } 76 | 77 | return sports; 78 | } 79 | 80 | private static DateTime RandomDay(Random rand, DateTime start, DateTime end) 81 | { 82 | int range = (end - start).Days; 83 | return start.AddDays(rand.Next(range)); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/TestData/Names.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.TestData 6 | { 7 | public static class Names 8 | { 9 | public static List GetFirstNames() 10 | { 11 | return new List(){ 12 | "Aaron", 13 | "James", 14 | "John", 15 | "Matthew", 16 | "Michael", 17 | "William", 18 | "David", 19 | "Luis", 20 | "Vincenzo", 21 | "Paul", 22 | "Mark", 23 | "Steven", 24 | "Edward", 25 | "Brian", 26 | "Mohamed", 27 | "Kristoff", 28 | "Christopher", 29 | "Mary", 30 | "Patricia", 31 | "Linda", 32 | "Barbara", 33 | "Kaylee", 34 | "Mackenzie", 35 | "Karen", 36 | "Nancy", 37 | "Ashley", 38 | "Jennifer", 39 | "Jessica", 40 | "Michelle", 41 | "Kimberly", 42 | "Maria", 43 | "Gretchen", 44 | "Franceska" 45 | }; 46 | } 47 | 48 | public static List GetLastNames() 49 | { 50 | return new List(){ 51 | "Smith", 52 | "Johnson", 53 | "Jones", 54 | "Williams", 55 | "Brown", 56 | "Miller", 57 | "Davis", 58 | "Garcia", 59 | "Rodriguez", 60 | "Wilson", 61 | "Martinez", 62 | "Anderson", 63 | "Taylor", 64 | "Thomas", 65 | "Moore", 66 | "Lee", 67 | "Gonzalez", 68 | "Harris", 69 | "Clark", 70 | "Lewis", 71 | "Robinson", 72 | "Walker", 73 | "Perez", 74 | "Hall", 75 | "Sanchez", 76 | "Wright", 77 | "White", 78 | "Jovanovich", 79 | "Morris", 80 | "Nguyen", 81 | "Edwards", 82 | "Murphy", 83 | "Rivera", 84 | "Baker", 85 | "Adams", 86 | "Carter", 87 | "Phillips", 88 | "Torres", 89 | "King", 90 | "Scott", 91 | "Davies", 92 | "Torrance", 93 | "Graham", 94 | "Leighty", 95 | "Jackson" 96 | }; 97 | } 98 | 99 | public static List GetCityNames() 100 | { 101 | return new List() 102 | { 103 | "New York", 104 | "Los Angeles", 105 | "Chicago", 106 | "Houston", 107 | "Philadelphia", 108 | "Phoenix", 109 | "San Francisco", 110 | "San Diego", 111 | "Dallas", 112 | "San Antonio", 113 | "Seattle", 114 | "Portland", 115 | "San Jose", 116 | "Nashville", 117 | "Indianapolis", 118 | "New Orleans", 119 | "Minneapolis", 120 | "Boston", 121 | "Toronto", 122 | "Washington", 123 | "Baltimore", 124 | "Charlotte", 125 | "Atlanta", 126 | "Miami", 127 | "Jacksonville", 128 | "Tampa Bay", 129 | "Milwaukee", 130 | "Detroit", 131 | "St Louis", 132 | "Kansas City" 133 | }; 134 | } 135 | 136 | public static List GetTeamNames() 137 | { 138 | return new List() 139 | { 140 | "Panthers", 141 | "Cougars", 142 | "Lions", 143 | "Bears", 144 | "Minutemen", 145 | "Raptors", 146 | "Cardinals", 147 | "Lightning", 148 | "Thunder", 149 | "Hurricanes", 150 | "Bison", 151 | "Devils", 152 | "Pterodactyls", 153 | "Rockers", 154 | "Canes", 155 | "Knights", 156 | "Waves", 157 | "Hangers", 158 | "Bombers", 159 | "Wizards", 160 | "Brawlers", 161 | "Volunteers", 162 | "Hawks", 163 | "Thrashers", 164 | "Snakes", 165 | "Venom", 166 | "Liberty", 167 | "Warriors", 168 | "Sparks", 169 | "Huskies", 170 | "Penguins", 171 | "Cheetahs", 172 | "Moose", 173 | "Sabres", 174 | "Mercenaries" 175 | }; 176 | } 177 | 178 | public static List GetSportNames() 179 | { 180 | return new List() 181 | { 182 | "Baseball", 183 | "American Football", 184 | "Association Football", 185 | "Rugby", 186 | "Basketball", 187 | "Ice Hockey", 188 | "Lacrosse", 189 | "Cricket", 190 | "Curling", 191 | "Field Hockey", 192 | "Quidditch", 193 | "Track & Field" 194 | }; 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /DapperEFCorePerformanceBenchmarks/TestData/TestResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DapperEFCorePerformanceBenchmarks.TestData 6 | { 7 | public class TestResult 8 | { 9 | public double PlayerByIDMilliseconds { get; set; } 10 | public double PlayersForTeamMilliseconds { get; set; } 11 | public double TeamsForSportMilliseconds { get; set; } 12 | public Framework Framework { get; set; } 13 | public int Run { get; set; } 14 | } 15 | 16 | public enum Framework 17 | { 18 | EntityFrameworkCore, 19 | EntityFrameworkCoreWithTracking, 20 | Dapper 21 | } 22 | } 23 | --------------------------------------------------------------------------------