diff options
Diffstat (limited to 'scripts/simulate-many-viewers.ts')
-rw-r--r-- | scripts/simulate-many-viewers.ts | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/simulate-many-viewers.ts b/scripts/simulate-many-viewers.ts new file mode 100644 index 000000000..fb666c318 --- /dev/null +++ b/scripts/simulate-many-viewers.ts | |||
@@ -0,0 +1,89 @@ | |||
1 | import Bluebird from 'bluebird' | ||
2 | import { wait } from '@shared/core-utils' | ||
3 | import { | ||
4 | createSingleServer, | ||
5 | doubleFollow, | ||
6 | killallServers, | ||
7 | PeerTubeServer, | ||
8 | setAccessTokensToServers, | ||
9 | waitJobs | ||
10 | } from '@shared/server-commands' | ||
11 | |||
12 | let servers: PeerTubeServer[] | ||
13 | const viewers: { xForwardedFor: string }[] = [] | ||
14 | let videoId: string | ||
15 | |||
16 | run() | ||
17 | .then(() => process.exit(0)) | ||
18 | .catch(err => console.error(err)) | ||
19 | .finally(() => killallServers(servers)) | ||
20 | |||
21 | async function run () { | ||
22 | await prepare() | ||
23 | |||
24 | while (true) { | ||
25 | await runViewers() | ||
26 | } | ||
27 | } | ||
28 | |||
29 | async function prepare () { | ||
30 | console.log('Preparing servers...') | ||
31 | |||
32 | const config = { | ||
33 | log: { | ||
34 | level: 'info' | ||
35 | }, | ||
36 | rates_limit: { | ||
37 | api: { | ||
38 | max: 5_000_000 | ||
39 | } | ||
40 | }, | ||
41 | views: { | ||
42 | videos: { | ||
43 | local_buffer_update_interval: '30 minutes', | ||
44 | ip_view_expiration: '1 hour' | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | servers = await Promise.all([ | ||
50 | createSingleServer(1, config, { nodeArgs: [ '--inspect' ] }), | ||
51 | createSingleServer(2, config), | ||
52 | createSingleServer(3, config) | ||
53 | ]) | ||
54 | |||
55 | await setAccessTokensToServers(servers) | ||
56 | await doubleFollow(servers[0], servers[1]) | ||
57 | await doubleFollow(servers[0], servers[2]) | ||
58 | |||
59 | const { uuid } = await servers[0].videos.quickUpload({ name: 'video' }) | ||
60 | videoId = uuid | ||
61 | |||
62 | await waitJobs(servers) | ||
63 | |||
64 | const THOUSAND_VIEWERS = 2 | ||
65 | |||
66 | for (let i = 2; i < 252; i++) { | ||
67 | for (let j = 2; j < 6; j++) { | ||
68 | for (let k = 2; k < THOUSAND_VIEWERS + 2; k++) { | ||
69 | viewers.push({ xForwardedFor: `0.${k}.${j}.${i},127.0.0.1` }) | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | console.log('Servers preparation finished.') | ||
75 | } | ||
76 | |||
77 | async function runViewers () { | ||
78 | console.log('Will run views of %d viewers.', viewers.length) | ||
79 | |||
80 | const before = new Date().getTime() | ||
81 | |||
82 | await Bluebird.map(viewers, viewer => { | ||
83 | return servers[0].views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor }) | ||
84 | }, { concurrency: 100 }) | ||
85 | |||
86 | console.log('Finished to run views in %d seconds.', (new Date().getTime() - before) / 1000) | ||
87 | |||
88 | await wait(5000) | ||
89 | } | ||