Przeglądaj źródła

performance measurements on 250k

Ivan Asmer 6 lat temu
rodzic
commit
d76751a27f
2 zmienionych plików z 102 dodań i 18 usunięć
  1. 83 5
      index.js
  2. 19 13
      mm.js

+ 83 - 5
index.js

@@ -22,12 +22,90 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
             return {
                 children: "parent",
                 parent: "children",
+                friends: "friends",
                 notebook: "owner",
             }
         }
     }
     Savable.addClass(Notebook)
     Savable.addClass(User)
+
+
+    let names    = ['Ivan', 'Petro', 'Mykola', 'Sashko']
+    let surnames = ['Ivanopulo', 'Petrov', 'Mykolyiv', 'Alexandrov']
+
+    let rndItem  = arr => arr[Math.floor(Math.random()*arr.length)]
+
+
+
+    let stamp = (new Date()).getTime()
+    let prevI = 0;
+    const persons = []
+    //for (var i=0;i<1e10;i++){
+        //let person = new User({
+            //name: rndItem(names),
+            //surname: rndItem(surnames),
+            //phones: ['105', '1'],
+            //friends: persons.slice(-(Math.random()*80))
+        //})
+
+        //await person.save(true)
+        
+        //persons.push(person)
+        //if (persons.length > 200){
+            //await (Math.random() > 0.5 ? persons.shift() : persons.pop()).save(true)
+        //}
+        
+
+        //let now = (new Date()).getTime()
+        //if (stamp < now - 1000){
+            ////results:
+            ////objects w/o relations: 2500 writes per second
+            ////objects w relations: pessimistic backrelations sync, 0..80 friends of 200 latest created, ~25 per second due 0..80 saves of other friends with new one relation
+            ////objects w relations: ~500 per second, save w/o backref save (but it updated in object), than, when object removed from random buffer, re-save it with updated relations
+            //console.log(i, i - prevI)
+            //prevI = i
+            //stamp = now
+        //}
+    //}
+
+
+    async function walker(limit=10000) {
+        let start = (new Date()).getTime()
+        let stamp = start
+        let now   = start
+
+        let prevI = 0
+        let person = await Savable.m.User.findOne()
+        for (var i=0;i<limit;i++){
+            person = await rndItem(person.friends) //walking in graph: go to random friend
+            //if (persons.includes(person)){
+                //console.log('WAS HERE',person._id, person.name, person.surname, person.createdAt)
+            //}
+            ////for (let friend of person.friends){
+                ////await friend
+            ////}
+            //persons.push(person)
+
+            now = (new Date()).getTime()
+            if (stamp < now - 1000){
+                //results:
+                //walking: 100-200 per second, not so fun...
+                //loops in graph: near 0-5 on 100 steps between nodes in graph
+                console.log(i, i - prevI, person._id, person.name, person.surname, person.createdAt)
+                prevI = i
+                stamp = now
+            }
+        }
+        return now - start
+    }
+
+
+    console.log(await Promise.all([walker(), walker()]))
+    console.log(await Promise.all([walker(), walker()]))
+
+
+
     
 
     //for (let child of father.children){
@@ -35,11 +113,11 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         //console.log(child.name, child.dirty)
     //}
 
-    let father = await Savable.m.User.findOne(ObjectID("5c9571219be797377361c65a"))
-    console.log(father);
-    (await father.children[0]).parent = null;
-    await (await father.children[0]).save();
-    console.log(father);
+    //let father = await Savable.m.User.findOne(ObjectID("5c9571219be797377361c65a"))
+    //console.log(father);
+    //(await father.children[0]).parent = null;
+    //await (await father.children[0]).save();
+    //console.log(father);
     
 
 

+ 19 - 13
mm.js

@@ -8,9 +8,9 @@ module.exports = db => {
     class Savable {
         constructor(obj, ref, empty=false){
             //TODO check type for return right class 
-            if (obj && obj._id){
-                console.log('savable...')
-            }
+            //if (obj && obj._id){
+                //console.log('savable...')
+            //}
 
 
             this._id    = null
@@ -26,6 +26,13 @@ module.exports = db => {
             }
         }
 
+        saveRelations(){
+            this._loadRelations = {};
+            for (const relation in this.__proto__.constructor.relations){
+                this._loadRelations[relation] = this[relation] instanceof Array ? [...this[relation]] : this[relation]
+            }
+        }
+
 
 
         populate(obj){
@@ -48,10 +55,8 @@ module.exports = db => {
 
             convertSavables(this)
 
-            this._loadRelations = {};
-            for (const relation in this.__proto__.constructor.relations){
-                this._loadRelations[relation] = this[relation] instanceof Array ? [...this[relation]] : this[relation]
-            }
+            this.saveRelations()
+
         }
 
         get _empty(){
@@ -97,7 +102,7 @@ module.exports = db => {
 
             const syncRelations = async () => {
                 //TODO: remove refs if some ref detached since load from db
-                if (noRefs) return
+                //if (noRefs) return
 
                 if (!(this && this.__proto__ && this.__proto__.constructor && this.__proto__.constructor.relations)) return 
 
@@ -113,22 +118,22 @@ module.exports = db => {
                 }
 
                 let setBackRef = async (backRef, foreignSavable) => {
-                    console.log('BACKREF for', backRef, foreignSavable.name)
+                    //console.log('BACKREF for', backRef, foreignSavable.name)
                     const {value: backRefValue, 
                             obj: backRefObj, 
                         lastKey: backRefKey} = await getValueByField(backRef, foreignSavable)
 
                     if (backRefValue instanceof Array){
-                        console.log('backref -to-many array')
+                        //console.log('backref -to-many array')
                         if (!backRefValue.includes(this)){
                             backRefValue.push(this)
                         }
                     }
                     else {
-                        console.log('backref -to-one')
+                        //console.log('backref -to-one')
                         backRefObj[backRefKey] = this
                     }
-                    await foreignSavable.save(true)
+                    noRefs || await foreignSavable.save(true)
                 }
 
 
@@ -151,7 +156,7 @@ module.exports = db => {
                             else {
                                 ref[backRef] = null
                             }
-                            await ref.save(true)
+                            noRefs || await ref.save(true)
                         }
                     }
                     if (valueAsArray){
@@ -196,6 +201,7 @@ module.exports = db => {
             }
 
             await syncRelations()
+            this.saveRelations()
         }