]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/benchmark.ts
Translated using Weblate (Icelandic)
[github/Chocobozzz/PeerTube.git] / scripts / benchmark.ts
1 import autocannon, { printResult } from 'autocannon'
2 import { program } from 'commander'
3 import { writeJson } from 'fs-extra'
4 import { Video, VideoPrivacy } from '@shared/models'
5 import { createMultipleServers, doubleFollow, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
6
7 let servers: PeerTubeServer[]
8 // First server
9 let server: PeerTubeServer
10 let video: Video
11 let threadId: number
12
13 program
14 .option('-o, --outfile [outfile]', 'Outfile')
15 .option('--grep [string]', 'Filter tests you want to execute')
16 .description('Run API REST benchmark')
17 .parse(process.argv)
18
19 const options = program.opts()
20
21 const outfile = options.outfile
22
23 run()
24 .catch(err => console.error(err))
25 .finally(() => {
26 if (servers) return killallServers(servers)
27 })
28
29 function buildAuthorizationHeader () {
30 return {
31 Authorization: 'Bearer ' + server.accessToken
32 }
33 }
34
35 function buildAPHeader () {
36 return {
37 Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
38 }
39 }
40
41 function buildJSONHeader () {
42 return {
43 'Content-Type': 'application/json'
44 }
45 }
46
47 async function run () {
48 console.log('Preparing server...')
49
50 await prepare()
51
52 const tests = [
53 {
54 title: 'AP - account peertube',
55 path: '/accounts/peertube',
56 headers: buildAPHeader(),
57 expecter: (body, status) => {
58 return status === 200 && body.startsWith('{"@context":')
59 }
60 },
61 {
62 title: 'AP - video',
63 path: '/videos/watch/' + video.uuid,
64 headers: buildAPHeader(),
65 expecter: (body, status) => {
66 return status === 200 && body.startsWith('{"@context":')
67 }
68 },
69 {
70 title: 'Misc - webfinger peertube',
71 path: '/.well-known/webfinger?resource=acct:peertube@' + server.host,
72 expecter: (body, status) => {
73 return status === 200 && body.startsWith('{"subject":')
74 }
75 },
76 {
77 title: 'API - unread notifications',
78 path: '/api/v1/users/me/notifications?start=0&count=0&unread=true',
79 headers: buildAuthorizationHeader(),
80 expecter: (_body, status) => {
81 return status === 200
82 }
83 },
84 {
85 title: 'API - me',
86 path: '/api/v1/users/me',
87 headers: buildAuthorizationHeader(),
88 expecter: (body, status) => {
89 return status === 200 && body.startsWith('{"id":')
90 }
91 },
92 {
93 title: 'API - videos list',
94 path: '/api/v1/videos',
95 expecter: (body, status) => {
96 return status === 200 && body.startsWith('{"total":10')
97 }
98 },
99 {
100 title: 'API - video get',
101 path: '/api/v1/videos/' + video.uuid,
102 expecter: (body, status) => {
103 return status === 200 && body.startsWith('{"id":')
104 }
105 },
106 {
107 title: 'API - video captions',
108 path: '/api/v1/videos/' + video.uuid + '/captions',
109 expecter: (body, status) => {
110 return status === 200 && body.startsWith('{"total":4')
111 }
112 },
113 {
114 title: 'API - video threads',
115 path: '/api/v1/videos/' + video.uuid + '/comment-threads',
116 expecter: (body, status) => {
117 return status === 200 && body.startsWith('{"total":10')
118 }
119 },
120 {
121 title: 'API - video replies',
122 path: '/api/v1/videos/' + video.uuid + '/comment-threads/' + threadId,
123 expecter: (body, status) => {
124 return status === 200 && body.startsWith('{"comment":{')
125 }
126 },
127 {
128 title: 'HTML - video watch',
129 path: '/videos/watch/' + video.uuid,
130 expecter: (body, status) => {
131 return status === 200 && body.includes('<title>my super')
132 }
133 },
134 {
135 title: 'HTML - video embed',
136 path: '/videos/embed/' + video.uuid,
137 expecter: (body, status) => {
138 return status === 200 && body.includes('embed')
139 }
140 },
141 {
142 title: 'HTML - homepage',
143 path: '/',
144 expecter: (_body, status) => {
145 return status === 200
146 }
147 },
148 {
149 title: 'API - config',
150 path: '/api/v1/config',
151 expecter: (body, status) => {
152 return status === 200 && body.startsWith('{"client":')
153 }
154 },
155 {
156 title: 'API - views with token',
157 method: 'PUT',
158 headers: {
159 ...buildAuthorizationHeader(),
160 ...buildJSONHeader()
161 },
162 body: JSON.stringify({ currentTime: 2 }),
163 path: '/api/v1/videos/' + video.uuid + '/views',
164 expecter: (body, status) => {
165 return status === 204
166 }
167 },
168 {
169 title: 'API - views without token',
170 method: 'POST',
171 headers: buildJSONHeader(),
172 body: JSON.stringify({ currentTime: 2 }),
173 path: '/api/v1/videos/' + video.uuid + '/views',
174 expecter: (body, status) => {
175 return status === 204
176 }
177 }
178 ].filter(t => {
179 if (!options.grep) return true
180
181 return t.title.includes(options.grep)
182 })
183
184 const finalResult: any[] = []
185
186 for (const test of tests) {
187 console.log('Running against %s.', test.path)
188 const testResult = await runBenchmark(test)
189
190 Object.assign(testResult, { title: test.title, path: test.path })
191 finalResult.push(testResult)
192
193 console.log(printResult(testResult))
194 }
195
196 if (outfile) await writeJson(outfile, finalResult)
197 }
198
199 function runBenchmark (options: {
200 path: string
201 method?: string
202 body?: string
203 headers?: { [ id: string ]: string }
204 expecter: Function
205 }) {
206 const { method = 'GET', path, body, expecter, headers } = options
207
208 return new Promise((res, rej) => {
209 autocannon({
210 url: server.url + path,
211 method,
212 body,
213 connections: 20,
214 headers,
215 pipelining: 1,
216 duration: 10,
217 requests: [
218 {
219 onResponse: (status, body) => {
220 if (expecter(body, status) !== true) {
221 console.error('Expected result failed.', { body, status })
222 throw new Error('Invalid expectation')
223 }
224 }
225 }
226 ]
227 }, (err, result) => {
228 if (err) return rej(err)
229
230 return res(result)
231 })
232 })
233 }
234
235 async function prepare () {
236 servers = await createMultipleServers(3, {
237 rates_limit: {
238 api: {
239 max: 5_000_000
240 }
241 }
242 })
243 server = servers[0]
244
245 await setAccessTokensToServers(servers)
246 await doubleFollow(servers[0], servers[1])
247 await doubleFollow(servers[0], servers[2])
248
249 const attributes = {
250 name: 'my super video',
251 category: 2,
252 nsfw: true,
253 licence: 6,
254 language: 'fr',
255 privacy: VideoPrivacy.PUBLIC,
256 support: 'please give me a coffee',
257 description: 'my super description\n'.repeat(10) + ' * list1\n * list 2\n * list 3',
258 tags: [ 'tag1', 'tag2', 'tag3' ]
259 }
260
261 for (let i = 0; i < 10; i++) {
262 await server.videos.upload({ attributes: { ...attributes, name: 'my super video ' + i } })
263 }
264
265 const { data } = await server.videos.list()
266 video = data.find(v => v.name === 'my super video 1')
267
268 for (let i = 0; i < 10; i++) {
269 const text = 'my super first comment'
270 const created = await server.comments.createThread({ videoId: video.id, text })
271 threadId = created.id
272
273 const text1 = 'my super answer to thread 1'
274 const child = await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text1 })
275
276 const text2 = 'my super answer to answer of thread 1'
277 await server.comments.addReply({ videoId: video.id, toCommentId: child.id, text: text2 })
278
279 const text3 = 'my second answer to thread 1'
280 await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text3 })
281 }
282
283 for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) {
284 await server.captions.add({
285 language: caption,
286 videoId: video.id,
287 fixture: 'subtitle-good2.vtt'
288 })
289 }
290 }