BenchmarkRunner_ReduceMember.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Configs;
  3. using Vit.Linq.ExpressionTree;
  4. using Vitorm;
  5. using Vitorm.Sql;
  6. namespace App.Runner
  7. {
  8. class Config : ManualConfig
  9. {
  10. // https://benchmarkdotnet.org/articles/configs/configs.html
  11. public Config()
  12. {
  13. WithOptions(ConfigOptions.DisableOptimizationsValidator);
  14. }
  15. }
  16. [Config(typeof(Config))]
  17. //[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
  18. [InProcess]
  19. public partial class BenchmarkRunner_ReduceMember
  20. {
  21. //[Params(100)]
  22. public int N = 2;
  23. //[Params(true, false)]
  24. public bool executeQuery = false;
  25. //[Params(false, true)]
  26. public bool queryJoin = true;
  27. [Params(10, 100, 1000)]
  28. public int take = 100;
  29. //[Params(0, 10)]
  30. public int? skip = 10;
  31. //[Params(true, false)]
  32. public bool reduceMember = true;
  33. [Params("EntityConstructor", "CompiledLambda")]
  34. public string entityReader = "EntityConstructor";
  35. IQueryable<User> userQuery;
  36. public IQueryable<User> GetQueryable() => userQuery;
  37. [GlobalSetup]
  38. public void Setup()
  39. {
  40. DataConvertArgument.CalculateToConstant_ManuallyReduceMember = reduceMember;
  41. if (entityReader == "CompiledLambda")
  42. SqlDbContext.defaultEntityReaderType = typeof(global::Vitorm.Sql.DataReader.EntityReader.CompiledLambda.EntityReader);
  43. else
  44. SqlDbContext.defaultEntityReaderType = typeof(global::Vitorm.Sql.DataReader.EntityReader.EntityConstructor.EntityReader);
  45. }
  46. [Benchmark]
  47. public void Run()
  48. {
  49. userQuery = Data.Query<User>();
  50. for (int i = 0; i < N; i++)
  51. {
  52. if (queryJoin) QueryJoin();
  53. else Query();
  54. }
  55. }
  56. #region Executor
  57. int exceptUserId = 1;
  58. public void QueryJoin()
  59. {
  60. var userSet = GetQueryable();
  61. var minId = 1;
  62. var config = new { maxId = 10000 };
  63. var offsetId = 100;
  64. var query =
  65. from user in userSet
  66. from father in userSet.Where(father => user.fatherId == father.id).DefaultIfEmpty()
  67. from mother in userSet.Where(mother => user.motherId == mother.id).DefaultIfEmpty()
  68. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  69. orderby user.id
  70. select new
  71. {
  72. user,
  73. father,
  74. mother,
  75. testId = user.id + offsetId,
  76. hasFather = father.name != null ? true : false
  77. }
  78. ;
  79. Execute(query);
  80. }
  81. public void Query()
  82. {
  83. var userSet = GetQueryable();
  84. var minId = 1;
  85. var config = new { maxId = 10000 };
  86. var query =
  87. from user in userSet
  88. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  89. orderby user.id
  90. select user;
  91. Execute(query);
  92. }
  93. #endregion
  94. public void Execute<Result>(IQueryable<Result> query)
  95. {
  96. if (skip > 0) query = query.Skip(skip.Value);
  97. query = query.Take(take);
  98. if (executeQuery)
  99. {
  100. var userList = query.ToList();
  101. var rowCount = userList.Count();
  102. if (rowCount != take) throw new Exception($"query failed, expected row count : {take} , actual count: {rowCount} ");
  103. }
  104. else
  105. {
  106. var sql = query.ToExecuteString();
  107. if (string.IsNullOrEmpty(sql)) throw new Exception($"query failed, can not generated sql script");
  108. }
  109. }
  110. }
  111. }