<commit_before>﻿using System;
using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Dapper;
using DataAccessPlayground.EntityFramework;
using Xunit;

namespace DataAccessPlayground.Tests
{
	public class Song
	{
		public string SongName { get; set; }
		public string AlbumName { get; set; }
		public string ArtistName { get; set; }
	}

	public class PerformanceTests
	{
		private const string ConnectionString = "data source=DEV-TEMPLATE;initial catalog=Chinook;integrated security=True;multipleactiveresultsets=True;";
		Stopwatch _timer = new Stopwatch();


		[Fact]
		public void DapperSelectTest()
		{
			_timer.Reset();
			_timer.Start();

			var query = @"SELECT Album.Title as AlbumName, Track.Name as SongName, Artist.Name as ArtistName 
						FROM Track LEFT JOIN Album ON Track.AlbumId = Album.AlbumId
						LEFT JOIN Artist ON Album.ArtistId = Artist.ArtistId
						WHERE Artist.Name = @name";

			using (IDbConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				var songs = connection.Query<Song>(query, new {name = "<NAME>"});
				Assert.True(songs.Count() > 0);
			}

			_timer.Stop();
			Console.WriteLine(_timer.ElapsedMilliseconds);
		}

		[Fact]
		public void EntityFrameworkSelectTest()
		{
			_timer.Reset();
			_timer.Start();

			using (var entities = new ChinookEntities())
			{
				var songs = from track in entities.Tracks
							where track.Album.Artist.Name == "<NAME>"
							select new Song
							{
								AlbumName = track.Album.Title,
								ArtistName = track.Album.Artist.Name,
								SongName = track.Name
							};

				Assert.True(songs.ToList().Count > 0);
			}

			_timer.Stop();
			Console.WriteLine(_timer.ElapsedMilliseconds);
		}

		private static readonly Func<ChinookEntities, string, IQueryable<Song>> Query =
			CompiledQuery.Compile<ChinookEntities, string, IQueryable<Song>>(
				(entities, name) => from track in entities.Tracks
				                    where track.Album.Artist.Name == name
				                    select new Song
				                           	{
				                           		AlbumName = track.Album.Title,
				                           		ArtistName = track.Album.Artist.Name,
				                           		SongName = track.Name
				                           	});

		[Fact]
		public void EntityFrameworkPreCompiledSelectTest()
		{
			_timer.Reset();
			_timer.Start();

			using (var entities = new ChinookEntities())
			{
				var songs = Query.Invoke(entities, "Pearl Jam");
				Assert.True(songs.ToList().Count > 0);
			}

			_timer.Stop();
			Console.WriteLine(_timer.ElapsedMilliseconds);
		}
	}
}<commit_msg>Using InlineData test attribute to execute SQL select test multiple times.<commit_after>﻿using System;
using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Dapper;
using DataAccessPlayground.EntityFramework;
using Xunit;
using Xunit.Extensions;

namespace DataAccessPlayground.Tests
{
	public class Song
	{
		public string SongName { get; set; }
		public string AlbumName { get; set; }
		public string ArtistName { get; set; }
	}

	public class PerformanceTests
	{
		private const string ConnectionString = "data source=DEV-TEMPLATE;initial catalog=Chinook;integrated security=True;multipleactiveresultsets=True;";
		private readonly Stopwatch _timer = new Stopwatch();

		[Theory]
		[InlineData(1)]
		[InlineData(10)]
		[InlineData(100)]
		[InlineData(500)]
		[InlineData(1000)]
		public void DapperSelectTest(int total)
		{
			_timer.Reset();
			_timer.Start();

			const string query = @"SELECT Album.Title as AlbumName, Track.Name as SongName, Artist.Name as ArtistName 
								   FROM Track LEFT JOIN Album ON Track.AlbumId = Album.AlbumId
								   LEFT JOIN Artist ON Album.ArtistId = Artist.ArtistId
								   WHERE Artist.Name = @name";

			for (int i = 0; i < total; i++)
			{
				using (IDbConnection connection = new SqlConnection(ConnectionString))
				{
					connection.Open();
					var songs = connection.Query<Song>(query, new {name = "<NAME>"});
					Assert.True(songs.Count() > 0);
				}
			}
			_timer.Stop();
			Console.WriteLine("test=DapperSelectTest; total={0}; time={1};", total, _timer.ElapsedMilliseconds);
		}

		[Theory]
		[InlineData(1)]
		[InlineData(10)]
		[InlineData(100)]
		[InlineData(500)]
		[InlineData(1000)]
		public void EntityFrameworkSelectTest(int total)
		{
			_timer.Reset();
			_timer.Start();

			for (int i = 0; i < total; i++)
			{
				using (var entities = new ChinookEntities())
				{
					var query = from track in entities.Tracks
					            where track.Album.Artist.Name == "<NAME>"
					            select new Song
					                   	{
					                   		AlbumName = track.Album.Title,
					                   		ArtistName = track.Album.Artist.Name,
					                   		SongName = track.Name
					                   	};

					var songs = query.ToList();
					Assert.True(songs.Count > 0);
				}
			}

			_timer.Stop();
			Console.WriteLine("test=EntityFrameworkSelectTest; total={0}; time={1};", total, _timer.ElapsedMilliseconds);
		}

		private static Func<ChinookEntities, string, IQueryable<Song>> _query;

		Func<ChinookEntities, string, IQueryable<Song>> Query
		{
			get
			{
				return _query ?? (_query = CompiledQuery.Compile<ChinookEntities, string, IQueryable<Song>>(
							(entities, name) => from track in entities.Tracks
												where track.Album.Artist.Name == name
												select new Song
					                           			{
					                           				AlbumName = track.Album.Title,
					                           				ArtistName = track.Album.Artist.Name,
					                           				SongName = track.Name
					                           			}));
			}
		}

		[Theory]
		[InlineData(1)]
		[InlineData(10)]
		[InlineData(100)]
		[InlineData(500)]
		[InlineData(1000)]
		public void EntityFrameworkPreCompiledSelectTest(int total)
		{
			_timer.Reset();
			_timer.Start();

			for (int i = 0; i < total; i++)
			{

				using (var entities = new ChinookEntities())
				{
					var songs = Query.Invoke(entities, "<NAME>");
					Assert.True(songs.ToList().Count > 0);
				}
			}

			_timer.Stop();
			Console.WriteLine("test=EntityFrameworkPreCompiledSelectTest; total={0}; time={1};", total, _timer.ElapsedMilliseconds);
		}
	}
}
{'fraction_non_alphanumeric': 0.11268482490272373, 'fraction_numerical': 0.008404669260700388, 'mean_word_length': 2.5799442896935934, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6638026', 'n_tokens_mistral': 2456, 'n_tokens_neox': 1918, 'n_words': 445}