1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import autocannon, { printResult } from 'autocannon'
5 import { writeJson } from 'fs-extra'
6 import { createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
7 import { Video, VideoPrivacy } from '@shared/models'
9 let server: PeerTubeServer
13 const outfile = process.argv[2]
16 .catch(err => console.error(err))
18 if (server) return killallServers([ server ])
21 function buildAuthorizationHeader () {
23 Authorization: 'Bearer ' + server.accessToken
27 function buildAPHeader () {
29 Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
33 async function run () {
34 console.log('Preparing server...')
40 title: 'AP - account peertube',
41 path: '/accounts/peertube',
42 headers: buildAPHeader(),
43 expecter: (body, status) => {
44 return status === 200 && body.startsWith('{"type":')
49 path: '/videos/watch/' + video.uuid,
50 headers: buildAPHeader(),
51 expecter: (body, status) => {
52 return status === 200 && body.startsWith('{"type":"Video"')
56 title: 'Misc - webfinger peertube',
57 path: '/.well-known/webfinger?resource=acct:peertube@' + server.host,
58 expecter: (body, status) => {
59 return status === 200 && body.startsWith('{"subject":')
63 title: 'API - unread notifications',
64 path: '/api/v1/users/me/notifications?start=0&count=0&unread=true',
65 headers: buildAuthorizationHeader(),
66 expecter: (_body, status) => {
72 path: '/api/v1/users/me',
73 headers: buildAuthorizationHeader(),
74 expecter: (body, status) => {
75 return status === 200 && body.startsWith('{"id":')
79 title: 'API - videos list',
80 path: '/api/v1/videos',
81 expecter: (body, status) => {
82 return status === 200 && body.startsWith('{"total":10')
86 title: 'API - video get',
87 path: '/api/v1/videos/' + video.uuid,
88 expecter: (body, status) => {
89 return status === 200 && body.startsWith('{"id":')
93 title: 'API - video captions',
94 path: '/api/v1/videos/' + video.uuid + '/captions',
95 expecter: (body, status) => {
96 return status === 200 && body.startsWith('{"total":4')
100 title: 'API - video threads',
101 path: '/api/v1/videos/' + video.uuid + '/comment-threads',
102 expecter: (body, status) => {
103 return status === 200 && body.startsWith('{"total":10')
107 title: 'API - video replies',
108 path: '/api/v1/videos/' + video.uuid + '/comment-threads/' + threadId,
109 expecter: (body, status) => {
110 return status === 200 && body.startsWith('{"comment":{')
114 title: 'HTML - video watch',
115 path: '/videos/watch/' + video.uuid,
116 expecter: (body, status) => {
117 return status === 200 && body.includes('<title>my super')
121 title: 'HTML - video embed',
122 path: '/videos/embed/' + video.uuid,
123 expecter: (body, status) => {
124 return status === 200 && body.includes('embed')
128 title: 'HTML - homepage',
130 expecter: (_body, status) => {
131 return status === 200
135 title: 'API - config',
136 path: '/api/v1/config',
137 expecter: (body, status) => {
138 return status === 200 && body.startsWith('{"client":')
143 const finalResult: any[] = []
145 for (const test of tests) {
146 console.log('Running against %s.', test.path)
147 const testResult = await runBenchmark(test)
149 Object.assign(testResult, { title: test.title, path: test.path })
150 finalResult.push(testResult)
152 console.log(printResult(testResult))
155 if (outfile) await writeJson(outfile, finalResult)
158 function runBenchmark (options: {
160 headers?: { [ id: string ]: string }
163 const { path, expecter, headers } = options
165 return new Promise((res, rej) => {
167 url: server.url + path,
174 onResponse: (status, body) => {
175 if (expecter(body, status) !== true) {
176 console.error('Expected result failed.', { body, status })
177 throw new Error('Invalid expectation')
182 }, (err, result) => {
183 if (err) return rej(err)
190 async function prepare () {
191 server = await createSingleServer(1, {
198 await setAccessTokensToServers([ server ])
201 name: 'my super video',
206 privacy: VideoPrivacy.PUBLIC,
207 support: 'please give me a coffee',
208 description: 'my super description'.repeat(10),
209 tags: [ 'tag1', 'tag2', 'tag3' ]
212 for (let i = 0; i < 10; i++) {
213 await server.videos.upload({ attributes: { ...attributes, name: 'my super video ' + i } })
216 const { data } = await server.videos.list()
217 video = data.find(v => v.name === 'my super video 1')
219 for (let i = 0; i < 10; i++) {
220 const text = 'my super first comment'
221 const created = await server.comments.createThread({ videoId: video.id, text })
222 threadId = created.id
224 const text1 = 'my super answer to thread 1'
225 const child = await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text1 })
227 const text2 = 'my super answer to answer of thread 1'
228 await server.comments.addReply({ videoId: video.id, toCommentId: child.id, text: text2 })
230 const text3 = 'my second answer to thread 1'
231 await server.comments.addReply({ videoId: video.id, toCommentId: threadId, text: text3 })
234 for (const caption of [ 'ar', 'fr', 'en', 'zh' ]) {
235 await server.captions.add({
238 fixture: 'subtitle-good2.vtt'
242 return { server, video, threadId }