1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as autocannon from 'autocannon'
12 setAccessTokensToServers,
14 } from '@shared/extra-utils'
15 import { Video, VideoPrivacy } from '@shared/models'
16 import { writeJson } from 'fs-extra'
18 let server: ServerInfo
22 const outfile = process.argv[2]
25 .catch(err => console.error(err))
27 if (server) killallServers([ server ])
30 function buildAuthorizationHeader () {
32 Authorization: 'Bearer ' + server.accessToken
36 function buildAPHeader () {
38 Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
42 async function run () {
43 console.log('Preparing server...')
49 title: 'AP - account peertube',
50 path: '/accounts/peertube',
51 headers: buildAPHeader(),
52 expecter: (body, status) => {
53 return status === 200 && body.startsWith('{"type":')
58 path: '/videos/watch/' + video.uuid,
59 headers: buildAPHeader(),
60 expecter: (body, status) => {
61 return status === 200 && body.startsWith('{"type":"Video"')
65 title: 'Misc - webfinger peertube',
66 path: '/.well-known/webfinger?resource=acct:peertube@' + server.host,
67 expecter: (body, status) => {
68 return status === 200 && body.startsWith('{"subject":')
72 title: 'API - unread notifications',
73 path: '/api/v1/users/me/notifications?start=0&count=0&unread=true',
74 headers: buildAuthorizationHeader(),
75 expecter: (_body, status) => {
81 path: '/api/v1/users/me',
82 headers: buildAuthorizationHeader(),
83 expecter: (body, status) => {
84 return status === 200 && body.startsWith('{"id":')
88 title: 'API - videos list',
89 path: '/api/v1/videos',
90 expecter: (body, status) => {
91 return status === 200 && body.startsWith('{"total":10')
95 title: 'API - video get',
96 path: '/api/v1/videos/' + video.uuid,
97 expecter: (body, status) => {
98 return status === 200 && body.startsWith('{"id":')
102 title: 'API - video captions',
103 path: '/api/v1/videos/' + video.uuid + '/captions',
104 expecter: (body, status) => {
105 return status === 200 && body.startsWith('{"total":4')
109 title: 'API - video threads',
110 path: '/api/v1/videos/' + video.uuid + '/comment-threads',
111 expecter: (body, status) => {
112 return status === 200 && body.startsWith('{"total":10')
116 title: 'API - video replies',
117 path: '/api/v1/videos/' + video.uuid + '/comment-threads/' + threadId,
118 expecter: (body, status) => {
119 return status === 200 && body.startsWith('{"comment":{')
123 title: 'HTML - video watch',
124 path: '/videos/watch/' + video.uuid,
125 expecter: (body, status) => {
126 return status === 200 && body.includes('<title>my super')
130 title: 'HTML - video embed',
131 path: '/videos/embed/' + video.uuid,
132 expecter: (body, status) => {
133 return status === 200 && body.includes('embed')
137 title: 'HTML - homepage',
139 expecter: (_body, status) => {
140 return status === 200
144 title: 'API - config',
145 path: '/api/v1/config',
146 expecter: (body, status) => {
147 return status === 200 && body.startsWith('{"instance":')
152 const finalResult: any[] = []
154 for (const test of tests) {
155 console.log('Running against %s.', test.path)
156 const testResult = await runBenchmark(test)
158 Object.assign(testResult, { title: test.title, path: test.path })
159 finalResult.push(testResult)
161 console.log(autocannon.printResult(testResult))
164 if (outfile) await writeJson(outfile, finalResult)
167 function runBenchmark (options: {
169 headers?: { [ id: string ]: string }
172 const { path, expecter, headers } = options
174 return new Promise((res, rej) => {
176 url: server.url + path,
183 onResponse: (status, body) => {
184 if (expecter(body, status) !== true) {
185 console.error('Expected result failed.', { body, status })
186 throw new Error('Invalid expectation')
191 }, (err, result) => {
192 if (err) return rej(err)
199 async function prepare () {
200 server = await flushAndRunServer(1, {
207 await setAccessTokensToServers([ server ])
209 const videoAttributes = {
210 name: 'my super video',
215 privacy: VideoPrivacy.PUBLIC,
216 support: 'please give me a coffee',
217 description: 'my super description'.repeat(10),
218 tags: [ 'tag1', 'tag2', 'tag3' ]
221 for (let i = 0; i < 10; i++) {
222 Object.assign(videoAttributes, { name: 'my super video ' + i })
223 await uploadVideo(server.url, server.accessToken, videoAttributes)
226 const resVideos = await getVideosList(server.url)
227 video = resVideos.body.data.find(v => v.name === 'my super video 1')
229 for (let i = 0; i < 10; i++) {
230 const text = 'my super first comment'
231 const res = await addVideoCommentThread(server.url, server.accessToken, video.id, text)
232 threadId = res.body.comment.id
234 const text1 = 'my super answer to thread 1'
235 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text1)
236 const childCommentId = childCommentRes.body.comment.id
238 const text2 = 'my super answer to answer of thread 1'
239 await addVideoCommentReply(server.url, server.accessToken, video.id, childCommentId, text2)
241 const text3 = 'my second answer to thread 1'
242 await addVideoCommentReply(server.url, server.accessToken, video.id, threadId, text3)
245 for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) {
246 await server.captionsCommand.createVideoCaption({
249 fixture: 'subtitle-good2.vtt'
253 return { server, video, threadId }