-
Notifications
You must be signed in to change notification settings - Fork 1
/
Profiler.js
165 lines (150 loc) · 4.43 KB
/
Profiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const Ack = require('./Ack');
const KeyRing = require('./KeyRing');
// Ensure unique identity for this test run.
let keyBase;
let keyChain = {};
let __gun;
let recurse = (target, callback, done) => {
setImmediate( ()=>{
callback(target, () => {
if (target === 0) {
done();
} else {
target--;
recurse(target, callback, done);
}
});
});
};
let sequence = function(target, node, allDone) {
let ring = new KeyRing(keyBase + "_MEDIUM");
// An easy way to re-create gun as needed
let __gun;
let getGun = function(run) {
__gun = null;
__gun = new Gun({
'file-name': 'yourData.json',
'file-pretty': false,
'file-delay': 500,
db: { file:"gun.db" }
//db: { file:"$sack@Mount$gun.db" }
});
run(__gun);
}
let profileWrite = function() {
getGun(function($gun) {
let res = new Ack(`Write ${target} nodes: `);
recurse(
target,
(i, next) => {
$gun.get(ring.make(i)).put(node, at => {
if( i % 100 == 0 )console.log(i);
res.ack(at);
next();
});
},
() => {
res.done();
profileRead();
}
);
});
}
// Write 10K nodes
let profileRead = function() {
getGun(function($gun) {
let res = new Ack(`Read ${target} nodes: `);
let nodeKeys = Object.keys(node);
recurse(
target,
(i, next) => {
$gun.get(ring.make(i)).on((val, key, ctx, at) => {
// remove metadata
delete val._;
// Check for node completeness
if (Object.keys(val).length === nodeKeys.length) {
at.off();
res.ack();
next();
}
});
},
() => {
res.done();
upsert();
}
);
});
}
// Update 10K nodes
let upsert = function() {
getGun(function($gun) {
let res = new Ack(`Update ${target} nodes: `);
recurse(
target,
(i, next) => $gun.get(ring.make(i)).put(node, at => {
res.ack(at);
next();
}),
() => {
res.done();
updateSingleProperty();
}
);
});
}
// Update 10K nodes
let updateSingleProperty = function() {
getGun(function($gun) {
let res = new Ack(`Update single field on ${target} nodes: `);
recurse(
target,
(i, next) => $gun.get(ring.make(i)).put({one: 'two'}, at => {
res.ack(at);
next();
}),
() => {
res.done();
allDone();
}
);
});
}
// Read 10K node fields
// let readFields = function() {
// getGun($gun => {
// let res = new Ack(`Read ${target} nodes: `);
// let node = node;
// recurse(
// target,
// (i, next) => {
// let propName = `prop_${i}`;
// $gun.get(ring.make(i)).get(propName).on((val, key, ctx, at) => {
// if (val[propName] === 'medium_property') {
// at.off();
// res.ack();
// }
// });
// },
// () => {
// res.done();
// allDone();
// }
// );
// });
// }
// start the chain
profileWrite();
}
module.exports = class Profiler {
constructor(desc, node, target) {
this.desc = desc;
this.target = target;
this.node = node;
keyBase = Date.now() + "";
}
run(done) {
console.log(this.desc);
sequence(this.target, this.node, done);
}
}