It has been more than eight years since the beginning of contact with ORM, and has used a lot of ORM framework to understand a lot of ORM framework, see N kinds of ORM Framework related materials and comments, all kinds of speech is difficult to choose. The most prominent issue in ORM's many issues was the performance issue, so I was inspired after reading a foreign article (Dapper vs Entity Framework vs. ADO Performance Benchmarking), In this article based on the expansion of test cases to share to everyone.
- Model Preparation
- Data initialization
- Test Case Description
- Test results
- Results analysis
Model Preparation
For testing is the model is based on an order system, as shown in the main customer, product, warehouse, order and order details. The data table before the relationship can be seen, in addition to product, order details are self-added columns, the warehouse is a two primary key composition of the composite primary key table. Although there are only 5 tables, it already contains the common case of the data tables.
Initialization of data
We use the above code to create a database, initialize the data table structure, generate test data, and generate the amount of data as shown in the following table.
1 Static voidinitialdatabase ()2 {3 using(varEF =NewModels.efcontext ())4 {5 if(!EF. Database.exists ())6 {7 EF. Database.create ();8 9 using(vardb =NewModels.megocontext ())Ten { One db. Initialtable (); A } - } - } the using(vardb =NewModels.megocontext ()) - { - db. Initialdata (); - } +}
Table name |
Description |
Number of rows |
Customers |
Customer table |
10000 |
Products |
Product table |
9000 |
Warehouses |
Warehouse Table |
30176 |
Orders |
Order Form |
100000 |
OrderDetails |
Order Details |
600271 |
The amount of data is approaching the amount of data in a small system, and if you need to do it locally, change the connection string in the app. Config file to your database name and run it directly.
Test Case Description
The current framework for participating in the tests is as follows:
- ado
- EntityFramework
- Entityframeworkcore
- Sqlsugar
- Dapper
- Mego
The example in this test is not limited to the currently specified frameworks, where an interface is defined, and if you want to add more test frameworks, you can refer to the code yourself.
1 /// <summary>2 ///Performance Test Project3 /// </summary>4 Public Interfaceiperformancetest5 {6 /// <summary>7 ///The frame name. 8 /// </summary>9 stringFramework {Get; }Ten /// <summary> One ///randomly get a customer A /// </summary> - /// <param name= "id" ></param> - /// <returns></returns> the LongGetcustomerbyid (intID); - /// <summary> - ///randomly get all the details of an order - /// </summary> + /// <param name= "OrderId" ></param> - /// <returns></returns> + LongGetdetailsbyorder (intorderId); A /// <summary> at ///randomly get an order and all the details - /// </summary> - /// <param name= "OrderId" ></param> - /// <returns></returns> - LongGetorderanddetails (intorderId); - /// <summary> in ///Insert a discrete n customer. - /// </summary> to /// <returns></returns> + LongInsertdiscretecustomers (customer[] customers); - /// <summary> the ///Insert a discrete n product, and self-increment the primary key. * /// </summary> $ /// <returns></returns>Panax Notoginseng Longinsertdiscreteproducts (product[] products); - /// <summary> the ///update a discrete n customer. + /// </summary> A /// <returns></returns> the LongUpdatediscretecustomers (customer[] customers); + /// <summary> - ///Delete the discrete n details. $ /// </summary> $ /// <returns></returns> - Longdeletediscretedetails (orderdetail[] details); - /// <summary> the ///Delete the discrete n warehouses, multiple primary keys. - /// </summary>Wuyi /// <returns></returns> the Longdeletediscretewarehouses (warehouse[] warehouses); -}
View Code
We have a few tests on the following table (output headers are used to display in the results):
Method name |
The amount of each test |
Output title |
Test instructions |
Getcustomerbyid |
Random Query 100 times. |
SELECT1 |
Randomly use the primary key to get the specified customer data. |
Getdetailsbyorder |
Random Query 100 times. |
SELECT2 |
Randomly use order key to get related order detail data. |
Getorderanddetails |
Random Query 100 times. |
SELECT3 |
The order key is randomly used to obtain the current order and all order detail data. |
Insertdiscretecustomers |
Insert 500 data. |
INSERT1 |
Inserts a specified number of customers. |
Insertdiscreteproducts |
Insert 500 data. |
INSERT2 |
Inserts the specified number of products (the primary key of the product is the self-increment column). |
Updatediscretecustomers |
Updated 500 data. |
UPDATE |
Updates the specified number of customers. |
Deletediscretedetails |
Delete 500 data. |
DELETE1 |
Deletes the specified number of order details. |
Deletediscretewarehouses |
Delete 500 data. |
DELETE2 |
Deletes the specified number of warehouses (the warehouse is a composite primary key). |
Here we have tested a framework of additions and deletions, a single primary key, composite primary key, the self-increment primary key are covered. In order to be fair we will execute the tests for each framework sequentially and then repeat them several times.
Test Results
As shown in the results of the test, this test was run 4 times for each frame, and the results were first output for each frame, and the average time for all frames was output in the final summary.
Results Analysis
The first thing we need to emphasize is that the above test ignores the advantages of some advanced ORM frameworks, such as when bulk inserting self-contained data, the resulting number is returned to the original object. From the test results can be seen in the query aspects of the framework are relatively close, but with the increase in data volume this difference will rise, in the INSERT, update and delete operations in the difference is relatively large. The main reason is that in bulk submissions, each framework is a statement-by-sentence, or a combination of the differences in batch submissions, here is the difference between the implementation of the different SQL statements. In terms of query Ado.net+automapper is the highest performance, this is not controversial, if there are questions can be referred to the beginning of the example, this article did not test this project because the modification operation is not ideal.
Post-meter
This article only shows the test results, without any conclusions, interested friends can download the code from Github to run the view results, and subsequent updates will continue to add more frameworks for testing.
Performance comparison of mainstream ORM frameworks in Mego (1)-Net