-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (135 loc) · 3.86 KB
/
index.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
var pull = require('pull-stream')
var paramap = require('pull-paramap')
var TIME = 5e3
var a = []
while(a.length < 1000) {
var v = {seq: a.length, random: Math.random(), time: new Date().toString(), foo: {bar: Math.random() < 0.5, length: 0}}
v.length = JSON.stringify(v).length
v.length = JSON.stringify(v).length
a.push(Buffer.from(JSON.stringify(v)))
}
function generate () {
return pull.infinite(function (e) {
return a[~~(Math.random()*a.length)]
//return {seq: e, random: Math.random(), time: new Date().toString(), foo: {bar: Math.random() < 0.5}}
})
}
var MB = 1024*1024
function length(data) {
return data.length || data.value.length
}
function print (str) {
str = [].slice.call(arguments).map(function (s) {
return 'string' === typeof s || Number.isInteger(s) ? s : Math.floor(s*1000)/1000
}).join(', ')
if('undefined' !== typeof window && document.body) {
var pre = document.createElement('pre')
pre.textContent = str
document.body.appendChild(pre)
}
console.log(str)
}
module.exports = function (createLog, N, T, encode) {
var start = Date.now()
var log = createLog()
var seqs = []
encode = encode || function (e) { return e }
print('name, ops/second, mb/second, ops, total-mb, seconds')
log.since.once(function (v) {
//how many items can you append in 10 seconds.
next()
})
function next () {
var start = Date.now(), c = 0, total = 0
pull(
generate(),
paramap(function (data, cb) {
c ++
total += length(data)
// return cb()
// console.log('append', c)
log.append(encode(data), cb)
}, 16),
pull.drain(function () {
if(Date.now() - start > TIME) return false
}, function (err) {
if(err && err != true) throw err
var time = (Date.now() - start)/1000
print('append', c/time, (total/MB)/time, c, total/MB, time)
next2()
})
)
}
function next2 () {
var total = 0, c = 0, start = Date.now()
pull(
log.stream(),
pull.drain(function (d) {
c++
total += length(d)
seqs.push(d.seq)
if(Date.now() - start > TIME) return false
}, function (err) {
if(err && err!=true) throw err
var time = (Date.now() - start)/1000
print('stream', c/time, (total/MB)/time, c, total/MB, time)
next2nocache()
})
)
}
function next2nocache () {
var total = 0, c = 0, start = Date.now()
pull(
log.stream({cache: false}),
pull.drain(function (d) {
c++
total += length(d)
seqs.push(d.seq)
if(Date.now() - start > TIME) return false
}, function () {
var time = (Date.now() - start)/1000
print('stream no cache', c/time, (total/MB)/time, c, total/MB, time)
next_para()
})
)
}
function next_para () {
var total = 0, c = 0, start = Date.now()
var N = 10, i = 0
var n = N
for(var i = 0; i < N; i++)
pull(
log.stream(),
pull.drain(function item (d) {
c++;
total += length(d)
seqs.push(d.seq)
if(Date.now() - start > TIME) return false
}, function () {
if(--n) return
var time = (Date.now() - start)/1000
print('stream10', (c/time), ((total/MB)/time), c, (total/MB), time)
next3()
})
)
}
function next3 () {
var total = 0, c = 0, start = Date.now()
pull(
pull.infinite(function (e) {
return seqs[~~(Math.random()*seqs.length)]
}),
paramap(function (seq, cb) {
log.get(seq, cb)
}, 1024),
pull.drain(function (d) {
c++
total += length(d)
if(Date.now() - start > TIME) return false
}, function () {
var time = (Date.now() - start)/1000
print('random', c/time, (total/MB)/time, c, total/MB, time)
})
)
}
}