再续:ORM性能测试用例
既然是ORM的测试, 就得体现这么几点 :
one-one, many-ony, many-many关系
typed query 类型化查询
entity crud 实体的crud
traverse entity list 实体集合的构造和遍历
从便利性上讲, 需要满足这样几点:
master-details: 保存master, 可以自动保存details, 删除master, 可以自动删除details, 但是还有一种master-details是弱关联, 必须区分开来, 所以测试用例中的delete必须要保证orderitem也删除
实体对象属性的透明获取
(之前给的压缩包没有打好, 现在重新上传了, 另外做了些改动, maste-detail变为三级:
order->orderitem->itemdetail) orderitem和itemdetail都包含一个attachment的对象
在原来的基础上修改, 结果不知怎么回事, 代码总显示不出来了, 只好删除重发一个
这里给出我的性能测试的dbschema和unit test
https://files.cnblogs.com/progame/dbschema.rar
[Test]
public void TestCRUDPerformance()
{
Product p = new Product("CAR001");
Customer c = new Customer(DBContext.CustomerID);
List<Guid> ids = new List<Guid>();
// clear table
s.Delete(Domain.Order.OrderID.IsNotNull);
s.Delete(Domain.OrderItem.ItemID.IsNotNull);
s.Delete(Domain.ItemDetail.DetailID.IsNotNull);
s.Delete(Domain.Attachment.id.IsNotNull);
DateTime dt = DateTime.Now;
int count = 100;
EntityList<Order> ol = null;
// create
for (int i = 0; i < count; i++)
{
Order o = new Order(Guid.NewGuid());
ids.Add(o.OrderID);
o.Amount = 100f;
o.OrderDate = DateTime.Today;
o.OrderNo = "PI_" + i.ToString("000");
o.Remark = "description";
o.Customer = c;
o.CreatedDateTime = DateTime.Now;
for (int j = 0; j < 10; j++)
{
OrderItem item = new OrderItem(Guid.NewGuid());
item.Amount = 100f;
item.Product = p;
item.Quantity = 10;
o.Items.Add(item);
Attachment itematt = new Attachment(Guid.NewGuid());
itematt.desc = "item";
item.Attachment = itematt;
for (int k = 0; k < 10; k++)
{
ItemDetail dtl = new ItemDetail(Guid.NewGuid());
dtl.Remark = "nothing at all";
item.Details.Add(dtl);
Attachment detailatt = new Attachment(Guid.NewGuid());
detailatt.desc = "item";
dtl.Attachment = detailatt;
}
}
s.Insert(o);
}
TimeSpan ts = DateTime.Now - dt;
EntityList<Attachment> attlist = s.GetList<Attachment>();
Assert.AreEqual(110 * count, attlist.Count);
Console.WriteLine("test insert performance use: {0} ms", ts.TotalMilliseconds);
// typed query
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
ol = s.GetList<Order>(
Domain.Order.OrderDate == DateTime.Today &
Domain.Order.OrderID == ids[i] &
Domain.Order.Items.Include(
Domain.OrderItem.Details.Include(
Domain.ItemDetail.Remark.IsNotNull)));
Assert.AreEqual(1, ol.Count);
}
ts = DateTime.Now - dt;
Console.WriteLine("test typed query performance use: {0} ms", ts.TotalMilliseconds);
//enity list read
dt = DateTime.Now;
ol = s.GetList<Order>();
double? sum = 0f;
double? sum2 = 0f;
int detailcount = 0;
foreach (Order o in ol)
{
foreach (OrderItem item in o.Items)
{
sum += item.Amount;
sum2 += item.Order.Amount;
detailcount += item.Details.Count;
}
}
ts = DateTime.Now - dt;
Assert.IsTrue(Math.Abs(Convert.ToDouble(sum - 1000f * count)) < 0.00001f);
Assert.AreEqual(count * 10 * 10, detailcount);
Console.WriteLine("test read performance use: {0} ms", ts.TotalMilliseconds);
// single load
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
s.Load(o);
}
ts = DateTime.Now - dt;
Console.WriteLine("test load performance use: {0} ms", ts.TotalMilliseconds);
// update all fields
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
o.Amount = 200f;
s.Update(o, true);
}
ts = DateTime.Now - dt;
Console.WriteLine("test update all fields performance use: {0} ms", ts.TotalMilliseconds);
// update dirty fields only
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
o.Amount = 200f;
s.Update(o);
}
ts = DateTime.Now - dt;
Console.WriteLine("test update dirty fields performance use: {0} ms", ts.TotalMilliseconds);
// delete
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
s.Delete(o);
}
ts = DateTime.Now - dt;
IEntityList list = s.GetList<Order>();
Assert.AreEqual(0, list.Count);
list = s.GetList<OrderItem>();
Assert.AreEqual(0, list.Count);
list = s.GetList<ItemDetail>();
Assert.AreEqual(0, list.Count);
list = s.GetList<Attachment>();
Assert.AreEqual(0, list.Count);
Console.WriteLine("test delete performance use: {0} ms", ts.TotalMilliseconds);
}
public void TestCRUDPerformance()
{
Product p = new Product("CAR001");
Customer c = new Customer(DBContext.CustomerID);
List<Guid> ids = new List<Guid>();
// clear table
s.Delete(Domain.Order.OrderID.IsNotNull);
s.Delete(Domain.OrderItem.ItemID.IsNotNull);
s.Delete(Domain.ItemDetail.DetailID.IsNotNull);
s.Delete(Domain.Attachment.id.IsNotNull);
DateTime dt = DateTime.Now;
int count = 100;
EntityList<Order> ol = null;
// create
for (int i = 0; i < count; i++)
{
Order o = new Order(Guid.NewGuid());
ids.Add(o.OrderID);
o.Amount = 100f;
o.OrderDate = DateTime.Today;
o.OrderNo = "PI_" + i.ToString("000");
o.Remark = "description";
o.Customer = c;
o.CreatedDateTime = DateTime.Now;
for (int j = 0; j < 10; j++)
{
OrderItem item = new OrderItem(Guid.NewGuid());
item.Amount = 100f;
item.Product = p;
item.Quantity = 10;
o.Items.Add(item);
Attachment itematt = new Attachment(Guid.NewGuid());
itematt.desc = "item";
item.Attachment = itematt;
for (int k = 0; k < 10; k++)
{
ItemDetail dtl = new ItemDetail(Guid.NewGuid());
dtl.Remark = "nothing at all";
item.Details.Add(dtl);
Attachment detailatt = new Attachment(Guid.NewGuid());
detailatt.desc = "item";
dtl.Attachment = detailatt;
}
}
s.Insert(o);
}
TimeSpan ts = DateTime.Now - dt;
EntityList<Attachment> attlist = s.GetList<Attachment>();
Assert.AreEqual(110 * count, attlist.Count);
Console.WriteLine("test insert performance use: {0} ms", ts.TotalMilliseconds);
// typed query
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
ol = s.GetList<Order>(
Domain.Order.OrderDate == DateTime.Today &
Domain.Order.OrderID == ids[i] &
Domain.Order.Items.Include(
Domain.OrderItem.Details.Include(
Domain.ItemDetail.Remark.IsNotNull)));
Assert.AreEqual(1, ol.Count);
}
ts = DateTime.Now - dt;
Console.WriteLine("test typed query performance use: {0} ms", ts.TotalMilliseconds);
//enity list read
dt = DateTime.Now;
ol = s.GetList<Order>();
double? sum = 0f;
double? sum2 = 0f;
int detailcount = 0;
foreach (Order o in ol)
{
foreach (OrderItem item in o.Items)
{
sum += item.Amount;
sum2 += item.Order.Amount;
detailcount += item.Details.Count;
}
}
ts = DateTime.Now - dt;
Assert.IsTrue(Math.Abs(Convert.ToDouble(sum - 1000f * count)) < 0.00001f);
Assert.AreEqual(count * 10 * 10, detailcount);
Console.WriteLine("test read performance use: {0} ms", ts.TotalMilliseconds);
// single load
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
s.Load(o);
}
ts = DateTime.Now - dt;
Console.WriteLine("test load performance use: {0} ms", ts.TotalMilliseconds);
// update all fields
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
o.Amount = 200f;
s.Update(o, true);
}
ts = DateTime.Now - dt;
Console.WriteLine("test update all fields performance use: {0} ms", ts.TotalMilliseconds);
// update dirty fields only
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
o.Amount = 200f;
s.Update(o);
}
ts = DateTime.Now - dt;
Console.WriteLine("test update dirty fields performance use: {0} ms", ts.TotalMilliseconds);
// delete
dt = DateTime.Now;
for (int i = 0; i < count; i++)
{
Order o = new Order(ids[i]);
s.Delete(o);
}
ts = DateTime.Now - dt;
IEntityList list = s.GetList<Order>();
Assert.AreEqual(0, list.Count);
list = s.GetList<OrderItem>();
Assert.AreEqual(0, list.Count);
list = s.GetList<ItemDetail>();
Assert.AreEqual(0, list.Count);
list = s.GetList<Attachment>();
Assert.AreEqual(0, list.Count);
Console.WriteLine("test delete performance use: {0} ms", ts.TotalMilliseconds);
}