移动端orm框架性能测评

发表于:2019-7-26 13:41

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:CristianoC    来源:简书

   ios测试代码
  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];
  ios 测试结果及分析
  下面给出测试结果,表格中所有数据的单位是秒,即做10000次操作需要的秒数。


   可以看到,Coredata除了批量插入性能是最好的以外,其他项性能都一般。
  Realm ios和Realm android性能非常相似,批量操作性能优异,但是非批量操作性能一般。可以这样总结,如果代码高内聚,可以把数据操作代码入口都统一使用,Realm性能是最好的,但这对代码质量、模块设计有要求,当操作数据的代码到处都有,不能使用批量接口时,Realm的性能是不好的。
  Luakit没有提供批量接口,但从图中可以看出,Luakit的各项性能指标都是比较好的,而且对代码没有要求,即使操作数据的代码不内聚,也不会对性能有影响。

     上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号