flutter_orm_plugin 发布以来,很多团队试用了,我发现你们对这类数据库相关的库,第一反应就是性能如何,以前确实没作太多行业对比,最近以为仍是有必要作一下性能测试,给你们一个交代的。java
在ios端,业界比较经常使用的orm框架应该是苹果官方推出的coredata,还有就是realm了。在android端orm框架我挑了三个比较经常使用的,greendao,realm和activeandroid。我会用flutter_orm_plugin跟上面提到的ios和android端orm框架作对比。 下面我会分别给出测试用例,测试代码,还有最终数据比较的结果。android
测试用例我列了如下这些ios
为何会有普通插入数据和使用批量接口插入数据的区别,大部分orm框架都会对批量操做有必定的优化,因此须要对批量操做进行测试,可是在平时使用,不必定都能用上批量接口(例如屡次数据操做不在同一代码块,或者在不一样的模块中都要操做数据),因此咱们会分别对普通操做和批量操做进行测试。git
首先咱们给出flutter_orm_plugin 的测试代码,因为不想由于flutter和原生channel通信产生偏差,咱们直接用Luakit来写lua代码作测试(greendao、realm、activeandroid、coredata都不涉及flutter和原生channel通信),flutter_orm_plugin其实底层就是luakit的orm框架,这个不影响测试准确性。github
Luakit定义orm模型结构并作10000次插入,下面的代码是ios和android通用的。数据库
local Student = { __dbname__ = "test.db", __tablename__ = "Student", studentId = {"TextField",{primary_key = true}}, name = {"TextField",{}}, claName = {"TextField",{}}, teacherName = {"TextField",{}}, score = {"RealField",{}}, } local params = { name = "Student", args = Student, } Table.addTableInfo(params,function () local studentTable = Table("Student”) for i=1,10000 do local s = { studentId = "studentId"..i, name = "name"..i, claName = "claName"..i, teacherName = "teacherName"..i, score = 90, } studentTable(s):save() end end) 复制代码
activeandroid定义orm模型结构并作10000次插入bash
@Table(name = "Students") public class Student extends Base { @Column(name = "studentId") public String studentId; @Column(name = "name") public String name; @Column(name = "claName") public String claName; @Column(name = "teacherName") public String teacherName; @Column(name = "score") public float score; public Student() { super(); } @Override public String toString() { return this.studentId; } } for (int i=0 ; i<10000 ;i++) { ActiveAndroid.beginTransaction(); Student s = new Student(); s.studentId = "studentId"+i; s.name = "name"+i; s.teacherName = "teacherName"+i; s.claName = "claName"+i; s.score = 90; s.save(); ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); } 复制代码
realm android 定义orm模型结构并作10000次插入markdown
public class StudentRealm extends RealmObject { @PrimaryKey private String studentId; @Required private String name; @Required private String teacherName; @Required private String claName; private float score; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClaName() { return claName; } public void setClaName(String ClaName) { this.claName = claName; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public String getStudentId() { return studentId; } public void setStudentId(String id) { this.studentId = id; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } } for (int i=0 ; i<10000 ;i++) { realm.beginTransaction(); StudentRealm realmStudent = realm.createObject(StudentRealm.class,"studentId"+i); realmStudent.setName("name"+i); realmStudent.setTeacherName("setTeacherName"+i); realmStudent.setClaName("setClaName"+i); realmStudent.setScore(90); realm.commitTransaction(); } 复制代码
GreenDao定义orm模型结构并作10000次插入框架
@Entity() public class Student { @Id private String studentId; @NotNull private String name; private String claName; private String teacherName; private float score; @Generated(hash = 1491230551) public Student(String studentId, @NotNull String name, String claName, String teacherName, float score) { this.studentId = studentId; this.name = name; this.claName = claName; this.teacherName = teacherName; this.score = score; } @Generated(hash = 1556870573) public Student() { } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } @NotNull public String getName() { return name; } /** Not-null value; ensure this value is available before it is saved to the database. */ public void setName(@NotNull String name) { this.name = name; } public String getClaName() { return claName; } public void setClaName(String claName) { this.claName = claName; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } } DaoSession daoSession = ((App) getApplication()).getDaoSession(); StudentDao sd = daoSession.getStudentDao(); for (int i = 0; i < 10000; i++) { Student s = new Student(); s.setStudentId("StudentId"+i); s.setClaName("getClaName"+i); s.setScore(90); s.setName("name"+i); s.setTeacherName("tn"+i); sd.insertOrReplace(s); } 复制代码
Luakit没有提供批量插入接口。ide
active android批量插入10000条数据。
ActiveAndroid.beginTransaction(); for (int i=0 ; i<10000 ;i++) { Student s = new Student(); s.studentId = "studentId"+i; s.name = "name"+i; s.teacherName = "teacherName"+i; s.claName = "claName"+i; s.score = 90; s.save(); } ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); 复制代码
realm android批量插入10000条数据。
Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); for (int i=0 ; i<10000 ;i++) { StudentRealm realmStudent = realm.createObject(StudentRealm.class,"studentId"+i); realmStudent.setName("name"+i); realmStudent.setTeacherName("setTeacherName"+i); realmStudent.setClaName("setClaName"+i); realmStudent.setScore(90); } realm.commitTransaction(); 复制代码
GreenDao批量插入10000条数据
DaoSession daoSession = ((App) getApplication()).getDaoSession(); StudentDao sd = daoSession.getStudentDao(); ArrayList<Student> ss = new ArrayList<Student>(); for (int i = 0; i < 10000; i++) { Student s = new Student(); s.setStudentId("StudentId"+i); s.setClaName("getClaName"+i); s.setScore(90); s.setName("name"+i); s.setTeacherName("tn"+i); ss.add(s); } sd.insertOrReplaceInTx(ss); 复制代码
###数据查询
Luakit作10000次查询,下面的代码是ios和android通用的。
local studentTable = Table("Student") for i=1,10000 do local result = studentTable.get:where({"studentId"..i},"studentId = ?"):all() end 复制代码
active android作10000次查询。
for (int i=0 ; i<10000 ;i++) { List<Student> student = new Select() .from(Student.class) .where("studentId = ?", "studentId"+i) .execute(); } 复制代码
realm android 作10000次查询。
for (int i=0 ; i<10000 ;i++) { RealmResults<StudentRealm> students = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findAll(); List<StudentRealm> list = realm.copyFromRealm(students); } 复制代码
GreenDao 作10000次查询
DaoSession daoSession = ((App) getApplication()).getDaoSession(); StudentDao sd = daoSession.getStudentDao(); for (int i = 0; i < 10000; i++) { List<Student> s = sd.queryBuilder() .where(StudentDao.Properties.StudentId.eq("StudentId"+i)) .list(); } 复制代码
###循环更新
Luakit作10000次更新。
local studentTable = Table("Student") for i=1,10000 do local result = studentTable.get:where({"studentId"..i},"studentId = ?"):update({name = "name2”}) end 复制代码
active android作10000次更新。
for (int i=0 ; i<10000 ;i++) { ActiveAndroid.beginTransaction(); Update update = new Update(Student.class); update.set("name = ?","name2") .where("studentId = ?", "studentId"+i) .execute(); ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); } 复制代码
realm android作10000次更新。
for (int i=0 ; i<10000 ;i++) { realm.beginTransaction(); StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst(); student.setClaName("ClaName"+(i+1)); realm.copyToRealmOrUpdate(student); realm.commitTransaction(); } 复制代码
GreenDao作10000次更新。
for (int i = 0; i < 10000; i++) { List<Student> s = sd.queryBuilder() .where(StudentDao.Properties.StudentId.eq("StudentId"+i)) .list(); s.get(0).setName("name2"); sd.update(s.get(0)); } 复制代码
###批量更新
Luakit没有批量更新接口。
active android批量更新10000条数据。
ActiveAndroid.beginTransaction(); for (int i=0 ; i<10000 ;i++) { Update update = new Update(Student.class); update.set("name = ?","name2") .where("studentId = ?", "studentId"+i) .execute(); } ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); 复制代码
realm android批量更新10000条数据。
realm.beginTransaction(); for (int i=0 ; i<10000 ;i++) { StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst(); student.setClaName("ClaName"+(i+1)); realm.copyToRealmOrUpdate(student); } realm.commitTransaction(); 复制代码
GreenDao批量更新10000条数据
ArrayList<Student> ss = new ArrayList<Student>(); for (int i = 0; i < 10000; i++) { List<Student> s = sd.queryBuilder() .where(StudentDao.Properties.StudentId.eq("StudentId"+i)) .list(); s.get(0).setName("name2"); ss.add(s.get(0)); } sd.updateInTx(ss); 复制代码
###循环删除
Luakit作10000次删除操做。
local studentTable = Table("Student") for i=1,10000 do studentTable.get:where({"studentId"..i},"studentId = ?"):delete() end 复制代码
active android作10000次删除操做。
for (int i=0 ; i<10000 ;i++) { ActiveAndroid.beginTransaction(); new Delete().from(Student.class).where("studentId = ?", "studentId"+i).execute(); ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); } 复制代码
realm android作10000次删除操做。
for (int i=0 ; i<10000 ;i++) { realm.beginTransaction(); StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst(); student.deleteFromRealm(); realm.commitTransaction(); } 复制代码
GreenDao作10000次删除操做。
for (int i = 0; i < 10000; i++) { List<Student> s = sd.queryBuilder() .where(StudentDao.Properties.StudentId.eq("StudentId"+i)) .list(); s.get(0).setName("name2"); sd.delete(s.get(0)); } 复制代码
###批量删除
Luakit没有批量删除接口。
active android批量删除10000条数据。
ActiveAndroid.beginTransaction(); for (int i=0 ; i<10000 ;i++) { new Delete().from(Student.class).where("studentId = ?", "studentId"+i).execute(); } ActiveAndroid.setTransactionSuccessful(); ActiveAndroid.endTransaction(); 复制代码
realm android批量删除10000条数据。
realm.beginTransaction(); for (int i=0 ; i<10000 ;i++) { StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst(); student.deleteFromRealm(); } realm.commitTransaction(); 复制代码
GreenDao批量删除10000条数据。
ArrayList<Student> ss = new ArrayList<Student>(); for (int i = 0; i < 10000; i++) { List<Student> s = sd.queryBuilder() .where(StudentDao.Properties.StudentId.eq("StudentId"+i)) .list(); ss.add(s.get(0)); } sd.deleteInTx(ss); 复制代码
下面给出测试结果,表格中全部数据的单位是秒,即作10000次操做须要的秒数。
能够看到,active android各项性能都通常。
在使用批量接口的状况下GreenDao和Realm的性能比较好。
在使用批量接口的状况下Realm的性能尤为好,批量插入、查询、批量更改、批量删除都是Realm的性能最好,可是Realm的非批量接口性能较差,全部能够这样总结,若是代码高内聚,能够把数据操做代码入口都统一使用,Realm性能是最好的,但这对代码质量、模块设计有要求,当操做数据的代码处处都有,不能使用批量接口时,Realm的性能是很差的。
Luakit没有提供批量接口,但从图中能够看出,Luakit的各项性能指标都是比较好的,并且对代码没有要求,即便操做数据的代码不内聚,也不会对性能有影响。
Luakit是跨平台的,代码跟android同样,下面就不列了,只给出Coredata和 Realm ios
Coredata 定义orm模型结构并作10000次插入
@interface Student (CoreDataProperties) + (NSFetchRequest<Student *> *)fetchRequest; @property (nullable, nonatomic, copy) NSString *claName; @property (nullable, nonatomic, copy) NSString *name; @property (nonatomic) float score; @property (nullable, nonatomic, copy) NSString *studentId; @property (nullable, nonatomic, copy) NSString *teacherName; @end self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; for (int i=0; i<10000; i++) { Student *s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context]; s.studentId = [NSString stringWithFormat:@"studentId%d",i]; s.name = [NSString stringWithFormat:@"name%d",i]; s.teacherName = [NSString stringWithFormat:@"teacherName%d",i]; s.claName = [NSString stringWithFormat:@"claName%d",i]; s.score = 90; NSError *error = nil; [self.context save:&error]; } 复制代码
Realm ios定义orm模型结构并作10000次插入
@interface StudentRLM : RLMObject @property NSString *studentId; @property NSString *name; @property NSString *teacherName; @property NSString *claName; @property float score; @end for (int i=0; i<10000; i++) { [realm beginWriteTransaction]; StudentRLM *s = [[StudentRLM alloc] init]; s.studentId = [NSString stringWithFormat:@"studentId%d",i];; s.name = [NSString stringWithFormat:@"name%d",i]; s.teacherName = [NSString stringWithFormat:@"teacherName%d",i]; s.claName = [NSString stringWithFormat:@"claName%d",i]; s.score = 90; [realm addOrUpdateObject:s]; [realm commitWriteTransaction]; [realm beginWriteTransaction]; } 复制代码
Coredata 批量插入10000条数据。
for (int i=0; i<10000; i++) { Student *s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context]; s.studentId = [NSString stringWithFormat:@"studentId%d",i]; s.name = [NSString stringWithFormat:@"name%d",i]; s.teacherName = [NSString stringWithFormat:@"teacherName%d",i]; s.claName = [NSString stringWithFormat:@"claName%d",i]; s.score = 90; } NSError *error = nil; [self.context save:&error]; 复制代码
Realm ios批量插入10000条数据。
[realm beginWriteTransaction]; for (int i=0; i<10000; i++) { StudentRLM *s = [[StudentRLM alloc] init]; s.studentId = [NSString stringWithFormat:@"studentId%d",i];; s.name = [NSString stringWithFormat:@"name%d",i]; s.teacherName = [NSString stringWithFormat:@"teacherName%d",i]; s.claName = [NSString stringWithFormat:@"claName%d",i]; s.score = 90; [realm addOrUpdateObject:s]; } [realm commitWriteTransaction]; [realm beginWriteTransaction]; 复制代码
Coredata 作10000次查询。
for (int i=0; i<10000; i++) { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"]; NSArray *objs = [self.context executeFetchRequest:request error:&error]; } 复制代码
Realm ios作10000次查询。
for (int i=0; i<10000; i++) { RLMResults *results = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]]; StudentRLM *s = results.firstObject; } 复制代码
###循环更新
Coredata 作10000次更新。
for (int i=0; i<10000; i++) { NSBatchUpdateRequest *batchUpdateRequest = [[NSBatchUpdateRequest alloc] initWithEntityName:@"Student"]; batchUpdateRequest.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"]; batchUpdateRequest.propertiesToUpdate = @{@"name" : @"name2"}; batchUpdateRequest.resultType = NSUpdatedObjectsCountResultType; NSBatchUpdateResult *batchResult = [self.context executeRequest:batchUpdateRequest error:&error]; NSError *error = nil; [self.context save:&error]; } 复制代码
Realm ios作10000次更新。
for (int i=0; i<10000; i++) { [realm beginWriteTransaction]; RLMResults *results = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]]; NSLog(@"results %lu",(unsigned long)[results count]); StudentRLM *s = results.firstObject; [s setName:@"name"]; [realm addOrUpdateObject:s]; [realm commitWriteTransaction]; } 复制代码
###批量更新
Coredata 批量更新10000条数据。
for (int i=0; i<10000; i++) { NSBatchUpdateRequest *batchUpdateRequest = [[NSBatchUpdateRequest alloc] initWithEntityName:@"Student"]; batchUpdateRequest.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"]; batchUpdateRequest.propertiesToUpdate = @{@"name" : @"name2"}; batchUpdateRequest.resultType = NSUpdatedObjectsCountResultType; NSBatchUpdateResult *batchResult = [self.context executeRequest:batchUpdateRequest error:&error]; } NSError *error = nil; [self.context save:&error]; 复制代码
Realm ios批量更新10000条数据。
[realm beginWriteTransaction];
for (int i=0; i<10000; i++) {
RLMResults *results = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
NSLog(@"results %lu",(unsigned long)[results count]);
StudentRLM *s = results.firstObject;
[s setName:@"name”];
[realm addOrUpdateObject:s];
}
[realm commitWriteTransaction];
复制代码
###循环删除
Coredata 作10000次删除操做。
for (int i=0; i<10000; i++) { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"]; NSBatchDeleteRequest *batchRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request]; batchRequest.resultType = NSUpdatedObjectsCountResultType; NSBatchUpdateResult *batchResult = [self.context executeRequest:batchRequest error:&error]; NSError *error = nil; [self.context save:&error]; } 复制代码
Realm ios作10000次删除操做。
for (int i=0; i<10000; i++) { [realm beginWriteTransaction]; RLMResults *results = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]]; StudentRLM *s = results.firstObject; [s setName:@"name"]; [realm deleteObject:s]; [realm commitWriteTransaction]; } 复制代码
###批量删除
Coredata 批量删除10000条数据。
for (int i=0; i<10000; i++) { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"]; NSBatchDeleteRequest *batchRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request]; batchRequest.resultType = NSUpdatedObjectsCountResultType; NSBatchUpdateResult *batchResult = [self.context executeRequest:batchRequest error:&error]; } NSError *error = nil; [self.context save:&error]; 复制代码
Realm ios批量删除10000条数据。
[realm beginWriteTransaction]; for (int i=0; i<10000; i++) { RLMResults *results = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]]; StudentRLM *s = results.firstObject; [s setName:@"name"]; [realm deleteObject:s]; } [realm commitWriteTransaction]; 复制代码
下面给出测试结果,表格中全部数据的单位是秒,即作10000次操做须要的秒数。
能够看到,Coredata除了批量插入性能是最好的之外,其余项性能都通常。
Realm ios和Realm android性能很是类似,批量操做性能优异,可是非批量操做性能通常。能够这样总结,若是代码高内聚,能够把数据操做代码入口都统一使用,Realm性能是最好的,但这对代码质量、模块设计有要求,当操做数据的代码处处都有,不能使用批量接口时,Realm的性能是很差的。
Luakit没有提供批量接口,但从图中能够看出,Luakit的各项性能指标都是比较好的,并且对代码没有要求,即便操做数据的代码不内聚,也不会对性能有影响。