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