Last active
April 11, 2017 14:49
-
-
Save jean-lourenco/65bdabece6d4bd77032e3d61654473a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using Dapper; | |
using Microsoft.EntityFrameworkCore; | |
using ServiceStack.DataAnnotations; | |
using ServiceStack.OrmLite; | |
using System; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Linq; | |
namespace orm_benchmark | |
{ | |
public class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<OrmSelect>(); | |
Console.WriteLine(summary); | |
Console.ReadKey(); | |
} | |
} | |
public class OrmSelect | |
{ | |
public const string ConnString = "Server=LOURENCO\\SQLEXPRESS;Database=rtm-hub;Trusted_Connection=True;"; | |
public OrmSelect() | |
{ | |
ExecuteSqlCommandSelect(); | |
ExecuteDapperSelect(); | |
ExecuteOrmLiteSelect(); | |
ExecuteEFCoreSelect(); | |
} | |
[Benchmark] | |
public TipoTransformacao ExecuteDapperSelect() | |
{ | |
using (var conn = new SqlConnection(ConnString)) | |
{ | |
conn.Open(); | |
return conn.Query<TipoTransformacao>("select Id, Chave, Descricao from TipoTransformacao where Id = 42").Single(); | |
} | |
} | |
[Benchmark] | |
public TipoTransformacao ExecuteOrmLiteSelect() | |
{ | |
var conn = new OrmLiteConnectionFactory(ConnString, SqlServerDialect.Provider); | |
using (var session = conn.Open()) | |
{ | |
return session.SingleById<TipoTransformacao>(42); | |
} | |
} | |
[Benchmark] | |
public SqlDataReader ExecuteSqlCommandSelect() | |
{ | |
using (var conn = new SqlConnection(ConnString)) | |
{ | |
conn.Open(); | |
using (var command = new SqlCommand("select Id, Chave, Descricao from TipoTransformacao where Id = 42", conn)) | |
{ | |
return command.ExecuteReader(); | |
} | |
} | |
} | |
[Benchmark] | |
public TipoTransformacao ExecuteEFCoreSelect() | |
{ | |
using (var db = new BenchmarkContext()) | |
{ | |
return db.TipoTransformacao.Where(x => x.Id == 42).Single(); | |
} | |
} | |
} | |
public class OrmInsert | |
{ | |
public const string ConnString = "Server=LOURENCO\\SQLEXPRESS;Database=rtm-hub;Trusted_Connection=True;"; | |
public OrmInsert() | |
{ | |
ExecuteSqlCommandInsert(); | |
ExecuteDapperInsert(); | |
ExecuteOrmLiteInsert(); | |
ExecuteEFCoreInsert(); | |
} | |
[Benchmark] | |
public int ExecuteDapperInsert() | |
{ | |
using (var conn = new SqlConnection(ConnString)) | |
{ | |
conn.Open(); | |
using (var tran = conn.BeginTransaction()) | |
{ | |
var linhas = conn.Execute("insert into TipoTransformacao (Chave, Descricao) values (@Chave, @Descricao)", new { Chave = "Testando dapper", Descricao = "Testando descricao"}, transaction: tran); | |
tran.Commit(); | |
return linhas; | |
} | |
} | |
} | |
[Benchmark] | |
public long ExecuteOrmLiteInsert() | |
{ | |
var conn = new OrmLiteConnectionFactory(ConnString, SqlServerDialect.Provider); | |
using (var session = conn.Open()) | |
{ | |
using (var tran = session.OpenTransaction()) | |
{ | |
var id = session.Insert(new TipoTransformacao { Chave = "Testando ormli", Descricao = "Testando descricao" }); | |
tran.Commit(); | |
return id; | |
} | |
} | |
} | |
[Benchmark] | |
public int ExecuteSqlCommandInsert() | |
{ | |
using (var conn = new SqlConnection(ConnString)) | |
{ | |
conn.Open(); | |
using (var tran = conn.BeginTransaction()) | |
{ | |
int linhas; | |
using (var command = new SqlCommand("insert into TipoTransformacao(Chave, Descricao) values (@v1, @v2)", conn, transaction: tran)) | |
{ | |
command.Parameters.AddWithValue("@v1", "Testando sqlco"); | |
command.Parameters.AddWithValue("@v2", "Testando descricao"); | |
linhas = command.ExecuteNonQuery(); | |
} | |
tran.Commit(); | |
return linhas; | |
} | |
} | |
} | |
[Benchmark] | |
public long ExecuteEFCoreInsert() | |
{ | |
using (var db = new BenchmarkContext()) | |
{ | |
using (var tran = db.Database.BeginTransaction()) | |
{ | |
var entity = new TipoTransformacao { Chave = "Testando efcor", Descricao = "Testando descricao" }; | |
db.Add(entity); | |
tran.Commit(); | |
db.SaveChanges(); | |
return entity.Id; | |
} | |
} | |
} | |
} | |
public class TipoTransformacao | |
{ | |
// Atributo necessário para o ORMLite | |
[AutoIncrement] | |
public long Id { get; set; } | |
public string Chave { get; set; } | |
public string Descricao { get; set; } | |
} | |
public class BenchmarkContext : DbContext | |
{ | |
public DbSet<TipoTransformacao> TipoTransformacao { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer(OrmSelect.ConnString); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment