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'
6 let server: PeerTubeServer
10 const outfile = process.argv[2]
13 .catch(err => console.error(err))
15 if (server) return killallServers([ server ])
18 function buildAuthorizationHeader () {
20 Authorization: 'Bearer ' + server.accessToken
24 function buildAPHeader () {
26 Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
30 async function run () {
31 console.log('Preparing server...')
37 title: 'AP - account peertube',
38 path: '/accounts/peertube',
39 headers: buildAPHeader(),
40 expecter: (body, status) => {
41 return status === 200 && body.startsWith('{"type":')
46 path: '/videos/watch/' + video.uuid,
47 headers: buildAPHeader(),
48 expecter: (body, status) => {
49 return status === 200 && body.startsWith('{"type":"Video"')
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":')
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) => {
69 path: '/api/v1/users/me',
70 headers: buildAuthorizationHeader(),
71 expecter: (body, status) => {
72 return status === 200 && body.startsWith('{"id":')
76 title: 'API - videos list',
77 path: '/api/v1/videos',
78 expecter: (body, status) => {
79 return status === 200 && body.startsWith('{"total":10')
83 title: 'API - video get',
84 path: '/api/v1/videos/' + video.uuid,
85 expecter: (body, status) => {
86 return status === 200 && body.startsWith('{"id":')
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')
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')
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":{')
111 title: 'HTML - video watch',
112 path: '/videos/watch/' + video.uuid,
113 expecter: (body, status) => {
114 return status === 200 && body.includes('<title>my super')
118 title: 'HTML - video embed',
119 path: '/videos/embed/' + video.uuid,
120 expecter: (body, status) => {
121 return status === 200 && body.includes('embed')
125 title: 'HTML - homepage',
127 expecter: (_body, status) => {
128 return status === 200
132 title: 'API - config',
133 path: '/api/v1/config',
134 expecter: (body, status) => {
135 return status === 200 && body.startsWith('{"client":')
140 const finalResult: any[] = []
142 for (const test of tests) {
143 console.log('Running against %s.', test.path)
144 const testResult = await runBenchmark(test)
146 Object.assign(testResult, { title: test.title, path: test.path })
147 finalResult.push(testResult)
149 console.log(printResult(testResult))
152 if (outfile) await writeJson(outfile, finalResult)
155 function runBenchmark (options: {
157 headers?: { [ id: string ]: string }
160 const { path, expecter, headers } = options
162 return new Promise((res, rej) => {
164 url: server.url + path,
171 onResponse: (status, body) => {
172 if (expecter(body, status) !== true) {
173 console.error('Expected result failed.', { body, status })
174 throw new Error('Invalid expectation')
179 }, (err, result) => {
180 if (err) return rej(err)
187 async function prepare () {
188 server = await createSingleServer(1, {
195 await setAccessTokensToServers([ server ])
198 name: 'my super video',
203 privacy: VideoPrivacy.PUBLIC,
204 support: 'please give me a coffee',
205 description: 'my super description'.repeat(10),
206 tags: [ 'tag1', 'tag2', 'tag3' ]
209 for (let i = 0; i < 10; i++) {
210 await server.videos.upload({ attributes: { ...attributes, name: 'my super video ' + i } })
213 const { data } = await server.videos.list()
214 video = data.find(v => v.name === 'my super video 1')
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
221 const text1 = 'my super answer to thread 1'
222 const child = await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text1 })
224 const text2 = 'my super answer to answer of thread 1'
225 await server.comments.addReply({ videoId: video.id, toCommentId: child.id, text: text2 })
227 const text3 = 'my second answer to thread 1'
228 await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text3 })
231 for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) {
232 await server.captions.add({
235 fixture: 'subtitle-good2.vtt'
239 return { server, video, threadId }