Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import XCTest
  2. import Foundation
  3. import RealmSwift
  4.  
  5. private let n = 100000
  6. private let numberOfLoops = 1000
  7.  
  8. class RealmPerformanceTest: XCTestCase {
  9. func testIndexedFilterPerformance() {
  10. let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "RealmPerformanceTest.testIndexedFilterPerformance"))
  11.  
  12. try! realm.write {
  13. for i in 0..<n {
  14. let value = Indexed()
  15. value.foo = Int(i)
  16. value.bar = value.foo * 2
  17. realm.add(value)
  18. }
  19. }
  20.  
  21. measureBlock {
  22. for _ in 1...numberOfLoops {
  23. let key = Int(arc4random() % UInt32(n))
  24. let results = realm.objects(Indexed.self).filter("foo = \(key)")
  25. XCTAssertEqual(results.first!.bar, key * 2)
  26. }
  27. }
  28. }
  29.  
  30. func testUnIndexedFilterPerformance() {
  31. let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "RealmPerformanceTest.testUnIndexedFilterPerformance"))
  32.  
  33. try! realm.write {
  34. for i in 0..<n {
  35. let value = Unindexed()
  36. value.foo = Int(i)
  37. value.bar = value.foo * 2
  38. realm.add(value)
  39. }
  40. }
  41.  
  42. measureBlock {
  43. for _ in 1...numberOfLoops {
  44. let key = Int(arc4random() % UInt32(n))
  45. let results = realm.objects(Unindexed.self).filter("foo = \(key)")
  46. XCTAssertEqual(results.first!.bar, key * 2)
  47. }
  48. }
  49. }
  50. }
  51.  
  52. class Indexed: Object {
  53. dynamic var foo: Int = 0
  54. dynamic var bar: Int = 0
  55.  
  56. override static func indexedProperties() -> [String] {
  57. return ["foo"]
  58. }
  59. }
  60.  
  61. class Unindexed: Object {
  62. dynamic var foo: Int = 0
  63. dynamic var bar: Int = 0
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement