1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as autocannon from 'autocannon'
13 setAccessTokensToServers,
15 } from '@shared/extra-utils'
16 import { Video, VideoPrivacy } from '@shared/models'
17 import { writeJson } from 'fs-extra'
19 let server: ServerInfo
23 const outfile = process.argv[2]
26 .catch(err => console.error(err))
28 if (server) killallServers([ server ])
31 function buildAuthorizationHeader () {
33 Authorization: 'Bearer ' + server.accessToken
37 function buildAPHeader () {
39 Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
43 async function run () {
44 console.log('Preparing server...')
50 title: 'AP - account peertube',
51 path: '/accounts/peertube',
52 headers: buildAPHeader(),
53 expecter: (client, statusCode) => {
54 const body = client.resData[0].body
56 return statusCode === 200 && body.startsWith('{"type":')
61 path: '/videos/watch/' + video.uuid,
62 headers: buildAPHeader(),
63 expecter: (client, statusCode) => {
64 const body = client.resData[0].body
66 return statusCode === 200 && body.startsWith('{"type":"Video"')
70 title: 'Misc - webfinger peertube',
71 path: '/.well-known/webfinger?resource=acct:peertube@' + server.host,
72 expecter: (client, statusCode) => {
73 const body = client.resData[0].body
75 return statusCode === 200 && body.startsWith('{"subject":')
79 title: 'API - unread notifications',
80 path: '/api/v1/users/me/notifications?start=0&count=0&unread=true',
81 headers: buildAuthorizationHeader(),
82 expecter: (_client, statusCode) => {
83 return statusCode === 200
88 path: '/api/v1/users/me',
89 headers: buildAuthorizationHeader(),
90 expecter: (client, statusCode) => {
91 const body = client.resData[0].body
93 return statusCode === 200 && body.startsWith('{"id":')
97 title: 'API - videos list',
98 path: '/api/v1/videos',
99 expecter: (client, statusCode) => {
100 const body = client.resData[0].body
102 return statusCode === 200 && body.startsWith('{"total":10')
106 title: 'API - video get',
107 path: '/api/v1/videos/' + video.uuid,
108 expecter: (client, statusCode) => {
109 const body = client.resData[0].body
111 return statusCode === 200 && body.startsWith('{"id":')
115 title: 'API - video captions',
116 path: '/api/v1/videos/' + video.uuid + '/captions',
117 expecter: (client, statusCode) => {
118 const body = client.resData[0].body
120 return statusCode === 200 && body.startsWith('{"total":4')
124 title: 'API - video threads',
125 path: '/api/v1/videos/' + video.uuid + '/comment-threads',
126 expecter: (client, statusCode) => {
127 const body = client.resData[0].body
129 return statusCode === 200 && body.startsWith('{"total":10')
133 title: 'API - video replies',
134 path: '/api/v1/videos/' + video.uuid + '/comment-threads/' + threadId,
135 expecter: (client, statusCode) => {
136 const body = client.resData[0].body
138 return statusCode === 200 && body.startsWith('{"comment":{')
142 title: 'HTML - video watch',
143 path: '/videos/watch/' + video.uuid,
144 expecter: (client, statusCode) => {
145 const body = client.resData[0].body
147 return statusCode === 200 && body.includes('<title>my super')
151 title: 'HTML - video embed',
152 path: '/videos/embed/' + video.uuid,
153 expecter: (client, statusCode) => {
154 const body = client.resData[0].body
156 return statusCode === 200 && body.includes('embed')
160 title: 'HTML - homepage',
162 expecter: (_client, statusCode) => {
163 return statusCode === 200
167 title: 'API - config',
168 path: '/api/v1/config',
169 expecter: (client, statusCode) => {
170 const body = client.resData[0].body
172 return statusCode === 200 && body.startsWith('{"instance":')
177 const finalResult: any[] = []
179 for (const test of tests) {
180 console.log('Running against %s.', test.path)
181 const testResult = await runBenchmark(test)
183 Object.assign(testResult, { title: test.title, path: test.path })
184 finalResult.push(testResult)
186 console.log(autocannon.printResult(testResult))
189 if (outfile) await writeJson(outfile, finalResult)
192 function runBenchmark (options: {
194 headers?: { [ id: string ]: string }
197 const { path, expecter, headers } = options
199 return new Promise((res, rej) => {
200 const instance = autocannon({
201 url: server.url + path,
206 }, (err, result) => {
207 if (err) return rej(err)
212 instance.on('response', (client, statusCode) => {
213 if (expecter(client, statusCode) !== true) {
214 console.error('Expected result failed.', { data: client.resData })
221 async function prepare () {
222 server = await flushAndRunServer(1, {
229 await setAccessTokensToServers([ server ])
231 const videoAttributes = {
232 name: 'my super video',
237 privacy: VideoPrivacy.PUBLIC,
238 support: 'please give me a coffee',
239 description: 'my super description'.repeat(10),
240 tags: [ 'tag1', 'tag2', 'tag3' ]
243 for (let i = 0; i < 10; i++) {
244 Object.assign(videoAttributes, { name: 'my super video ' + i })
245 await uploadVideo(server.url, server.accessToken, videoAttributes)
248 const resVideos = await getVideosList(server.url)
249 video = resVideos.body.data.find(v => v.name === 'my super video 1')
251 for (let i = 0; i < 10; i++) {
252 const text = 'my super first comment'
253 const res = await addVideoCommentThread(server.url, server.accessToken, video.id, text)
254 threadId = res.body.comment.id
256 const text1 = 'my super answer to thread 1'
257 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text1)
258 const childCommentId = childCommentRes.body.comment.id
260 const text2 = 'my super answer to answer of thread 1'
261 await addVideoCommentReply(server.url, server.accessToken, video.id, childCommentId, text2)
263 const text3 = 'my second answer to thread 1'
264 await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text3)
267 for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) {
268 await createVideoCaption({
270 accessToken: server.accessToken,
273 fixture: 'subtitle-good2.vtt'
277 return { server, video, threadId }