-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (91 loc) · 2.91 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
require('dotenv').config()
const _H = require('highland')
const { orderlessParallel } = require('@wmakeev/highland-tools')
const { getInstance } = require('moysklad-instance')
function getAuths() {
const auths = []
let index = 1
while (true) {
const auth = process.env[`MS_INSTANCE_AUTH_${index}`]
if (auth) {
const [login, password] = auth.split(';')
if (!login || !password) {
throw new Error(`Авторизиция ${index} указана некорретно`)
}
auths.push({ login, password })
index++
} else {
return auths
}
}
}
function getParams() {
const params = process.argv.slice(2).reduce((res, param) => {
const [key, value] = param.split('=')
res[key] = value
return res
}, {})
if (!params.entity) {
throw new Error('Не указана сущность для загрузки (entity)')
}
const entity = params.entity
if (!params.accounts) {
throw new Error('Не кол-во параллельно используемых аккаунтов (accounts)')
}
const accounts = Number.parseInt(params.accounts)
if (!params.pages) {
throw new Error('Не указано кол-во страниц (pages)')
}
const pages = Number.parseInt(params.pages)
return { entity, accounts, pages, expand: params.expand }
}
async function testEntityCollectionFetchRate() {
const auths = getAuths()
if (auths.length === 0) {
throw new Error('Не указана авторизация')
}
const { entity, accounts, pages, expand } = getParams()
if (accounts > auths.length) {
throw new Error(
`Недостаточное кол-во авторизаций (${auths.length})` +
` для указанного кол-ва аккаунтов (${accounts})`
)
}
const instances = auths
.slice(0, accounts)
.map((auth, index) => getInstance(`instance${index + 1}`, auth))
const POOL_LEN = instances.length
const LIMIT = 100
function* pageGen(pages) {
for (let i = 0; i < pages; i++) {
yield i
}
}
const getEntities = (ms, page) =>
ms
.GET(`entity/${entity}`, {
expand,
offset: page * LIMIT,
limit: LIMIT
})
.then(({ rows }) => rows)
const startTime = Date.now()
const totalLoaded = await _H(pageGen(pages))
.map(page => {
const instanceIndex = page % POOL_LEN
console.log(`Page ${page + 1} queued (account ${instanceIndex + 1})`)
return _H(getEntities(instances[instanceIndex], page))
})
.through(orderlessParallel(6 * POOL_LEN + 1))
.sequence()
.reduce(0, res => res + 1)
.toPromise(Promise)
const endTime = Date.now()
const rate = (endTime - startTime) / totalLoaded
console.log(`===`)
console.log(`Total loaded ${totalLoaded} entities`)
console.log(`Rate ${rate} ms/entity`)
}
testEntityCollectionFetchRate().catch(err => {
console.error(err.message)
})