real-world.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // /!\ Before imports /!\
  2. process.env.NODE_ENV = 'test'
  3. import * as program from 'commander'
  4. import { Video, VideoFile, VideoRateType } from '../../../shared'
  5. import { JobState } from '../../../shared/models'
  6. import {
  7. flushAndRunMultipleServers,
  8. flushTests, follow,
  9. getVideo,
  10. getVideosList, getVideosListPagination,
  11. killallServers,
  12. removeVideo,
  13. ServerInfo as DefaultServerInfo,
  14. setAccessTokensToServers,
  15. updateVideo,
  16. uploadVideo, viewVideo,
  17. wait
  18. } from '../../../shared/extra-utils'
  19. import { getJobsListPaginationAndSort } from '../../../shared/extra-utils/server/jobs'
  20. interface ServerInfo extends DefaultServerInfo {
  21. requestsNumber: number
  22. }
  23. program
  24. .option('-c, --create [weight]', 'Weight for creating videos')
  25. .option('-r, --remove [weight]', 'Weight for removing videos')
  26. .option('-u, --update [weight]', 'Weight for updating videos')
  27. .option('-v, --view [weight]', 'Weight for viewing videos')
  28. .option('-l, --like [weight]', 'Weight for liking videos')
  29. .option('-s, --dislike [weight]', 'Weight for disliking videos')
  30. .option('-p, --servers [n]', 'Number of servers to run (3 or 6)', /^3|6$/, 3)
  31. .option('-i, --interval-action [interval]', 'Interval in ms for an action')
  32. .option('-I, --interval-integrity [interval]', 'Interval in ms for an integrity check')
  33. .option('-f, --flush', 'Flush data on exit')
  34. .option('-d, --difference', 'Display difference if integrity is not okay')
  35. .parse(process.argv)
  36. const createWeight = program['create'] !== undefined ? parseInt(program['create'], 10) : 5
  37. const removeWeight = program['remove'] !== undefined ? parseInt(program['remove'], 10) : 4
  38. const updateWeight = program['update'] !== undefined ? parseInt(program['update'], 10) : 4
  39. const viewWeight = program['view'] !== undefined ? parseInt(program['view'], 10) : 4
  40. const likeWeight = program['like'] !== undefined ? parseInt(program['like'], 10) : 4
  41. const dislikeWeight = program['dislike'] !== undefined ? parseInt(program['dislike'], 10) : 4
  42. const flushAtExit = program['flush'] || false
  43. const actionInterval = program['intervalAction'] !== undefined ? parseInt(program['intervalAction'], 10) : 500
  44. const integrityInterval = program['intervalIntegrity'] !== undefined ? parseInt(program['intervalIntegrity'], 10) : 60000
  45. const displayDiffOnFail = program['difference'] || false
  46. const numberOfServers = 6
  47. console.log(
  48. 'Create weight: %d, update weight: %d, remove weight: %d, view weight: %d, like weight: %d, dislike weight: %d.',
  49. createWeight, updateWeight, removeWeight, viewWeight, likeWeight, dislikeWeight
  50. )
  51. if (flushAtExit) {
  52. console.log('Program will flush data on exit.')
  53. } else {
  54. console.log('Program will not flush data on exit.')
  55. }
  56. if (displayDiffOnFail) {
  57. console.log('Program will display diff on failure.')
  58. } else {
  59. console.log('Program will not display diff on failure')
  60. }
  61. console.log('Interval in ms for each action: %d.', actionInterval)
  62. console.log('Interval in ms for each integrity check: %d.', integrityInterval)
  63. console.log('Run servers...')
  64. start()
  65. // ----------------------------------------------------------------------------
  66. async function start () {
  67. const servers = await runServers(numberOfServers)
  68. process.on('exit', async () => {
  69. await exitServers(servers, flushAtExit)
  70. return
  71. })
  72. process.on('SIGINT', goodbye)
  73. process.on('SIGTERM', goodbye)
  74. console.log('Servers ran')
  75. initializeRequestsPerServer(servers)
  76. let checking = false
  77. setInterval(async () => {
  78. if (checking === true) return
  79. const rand = getRandomInt(0, createWeight + updateWeight + removeWeight + viewWeight + likeWeight + dislikeWeight)
  80. const numServer = getRandomNumServer(servers)
  81. servers[numServer].requestsNumber++
  82. if (rand < createWeight) {
  83. await upload(servers, numServer)
  84. } else if (rand < createWeight + updateWeight) {
  85. await update(servers, numServer)
  86. } else if (rand < createWeight + updateWeight + removeWeight) {
  87. await remove(servers, numServer)
  88. } else if (rand < createWeight + updateWeight + removeWeight + viewWeight) {
  89. await view(servers, numServer)
  90. } else if (rand < createWeight + updateWeight + removeWeight + viewWeight + likeWeight) {
  91. await like(servers, numServer)
  92. } else {
  93. await dislike(servers, numServer)
  94. }
  95. }, actionInterval)
  96. // The function will check the consistency between servers (should have the same videos with same attributes...)
  97. setInterval(function () {
  98. if (checking === true) return
  99. console.log('Checking integrity...')
  100. checking = true
  101. const waitingInterval = setInterval(async () => {
  102. const pendingRequests = await isTherePendingRequests(servers)
  103. if (pendingRequests === true) {
  104. console.log('A server has pending requests, waiting...')
  105. return
  106. }
  107. // Even if there are no pending request, wait some potential processes
  108. await wait(2000)
  109. await checkIntegrity(servers)
  110. initializeRequestsPerServer(servers)
  111. checking = false
  112. clearInterval(waitingInterval)
  113. }, 10000)
  114. }, integrityInterval)
  115. }
  116. function initializeRequestsPerServer (servers: ServerInfo[]) {
  117. servers.forEach(server => server.requestsNumber = 0)
  118. }
  119. function getRandomInt (min, max) {
  120. return Math.floor(Math.random() * (max - min)) + min
  121. }
  122. function getRandomNumServer (servers) {
  123. return getRandomInt(0, servers.length)
  124. }
  125. async function runServers (numberOfServers: number) {
  126. const servers: ServerInfo[] = (await flushAndRunMultipleServers(numberOfServers))
  127. .map(s => Object.assign({ requestsNumber: 0 }, s))
  128. // Get the access tokens
  129. await setAccessTokensToServers(servers)
  130. for (let i = 0; i < numberOfServers; i++) {
  131. for (let j = 0; j < numberOfServers; j++) {
  132. if (i === j) continue
  133. await follow(servers[i].url, [ servers[j].url ], servers[i].accessToken)
  134. }
  135. }
  136. return servers
  137. }
  138. async function exitServers (servers: ServerInfo[], flushAtExit: boolean) {
  139. killallServers(servers)
  140. if (flushAtExit) await flushTests()
  141. }
  142. function upload (servers: ServerInfo[], numServer: number) {
  143. console.log('Uploading video to server ' + numServer)
  144. const videoAttributes = {
  145. name: Date.now() + ' name',
  146. category: 4,
  147. nsfw: false,
  148. licence: 2,
  149. language: 'en',
  150. description: Date.now() + ' description',
  151. tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
  152. fixture: 'video_short1.webm'
  153. }
  154. return uploadVideo(servers[numServer].url, servers[numServer].accessToken, videoAttributes)
  155. }
  156. async function update (servers: ServerInfo[], numServer: number) {
  157. const res = await getVideosList(servers[numServer].url)
  158. const videos = res.body.data.filter(video => video.isLocal === true)
  159. if (videos.length === 0) return undefined
  160. const toUpdate = videos[getRandomInt(0, videos.length)].id
  161. const attributes = {
  162. name: Date.now() + ' name',
  163. description: Date.now() + ' description',
  164. tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ]
  165. }
  166. console.log('Updating video of server ' + numServer)
  167. return updateVideo(servers[numServer].url, servers[numServer].accessToken, toUpdate, attributes)
  168. }
  169. async function remove (servers: ServerInfo[], numServer: number) {
  170. const res = await getVideosList(servers[numServer].url)
  171. const videos = res.body.data.filter(video => video.isLocal === true)
  172. if (videos.length === 0) return undefined
  173. const toRemove = videos[getRandomInt(0, videos.length)].id
  174. console.log('Removing video from server ' + numServer)
  175. return removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove)
  176. }
  177. async function view (servers: ServerInfo[], numServer: number) {
  178. const res = await getVideosList(servers[numServer].url)
  179. const videos = res.body.data
  180. if (videos.length === 0) return undefined
  181. const toView = videos[getRandomInt(0, videos.length)].id
  182. console.log('Viewing video from server ' + numServer)
  183. return viewVideo(servers[numServer].url, toView)
  184. }
  185. function like (servers: ServerInfo[], numServer: number) {
  186. return rate(servers, numServer, 'like')
  187. }
  188. function dislike (servers: ServerInfo[], numServer: number) {
  189. return rate(servers, numServer, 'dislike')
  190. }
  191. async function rate (servers: ServerInfo[], numServer: number, rating: VideoRateType) {
  192. const res = await getVideosList(servers[numServer].url)
  193. const videos = res.body.data
  194. if (videos.length === 0) return undefined
  195. const toRate = videos[getRandomInt(0, videos.length)].id
  196. console.log('Rating (%s) video from server %d', rating, numServer)
  197. return getVideo(servers[numServer].url, toRate)
  198. }
  199. async function checkIntegrity (servers: ServerInfo[]) {
  200. const videos: Video[][] = []
  201. const tasks: Promise<any>[] = []
  202. // Fetch all videos and remove some fields that can differ between servers
  203. for (const server of servers) {
  204. const p = getVideosListPagination(server.url, 0, 1000000, '-createdAt')
  205. .then(res => videos.push(res.body.data))
  206. tasks.push(p)
  207. }
  208. await Promise.all(tasks)
  209. let i = 0
  210. for (const video of videos) {
  211. const differences = areDifferences(video, videos[0])
  212. if (differences !== undefined) {
  213. console.error('Integrity not ok with server %d!', i + 1)
  214. if (displayDiffOnFail) {
  215. console.log(differences)
  216. }
  217. process.exit(-1)
  218. }
  219. i++
  220. }
  221. console.log('Integrity ok.')
  222. }
  223. function areDifferences (videos1: Video[], videos2: Video[]) {
  224. // Remove some keys we don't want to compare
  225. videos1.concat(videos2).forEach(video => {
  226. delete video.id
  227. delete video.isLocal
  228. delete video.thumbnailPath
  229. delete video.updatedAt
  230. delete video.views
  231. })
  232. if (videos1.length !== videos2.length) {
  233. return `Videos length are different (${videos1.length}/${videos2.length}).`
  234. }
  235. for (const video1 of videos1) {
  236. const video2 = videos2.find(video => video.uuid === video1.uuid)
  237. if (!video2) return 'Video ' + video1.uuid + ' is missing.'
  238. for (const videoKey of Object.keys(video1)) {
  239. const attribute1 = video1[videoKey]
  240. const attribute2 = video2[videoKey]
  241. if (videoKey === 'tags') {
  242. if (attribute1.length !== attribute2.length) {
  243. return 'Tags are different.'
  244. }
  245. attribute1.forEach(tag1 => {
  246. if (attribute2.indexOf(tag1) === -1) {
  247. return 'Tag ' + tag1 + ' is missing.'
  248. }
  249. })
  250. } else if (videoKey === 'files') {
  251. if (attribute1.length !== attribute2.length) {
  252. return 'Video files are different.'
  253. }
  254. attribute1.forEach((videoFile1: VideoFile) => {
  255. const videoFile2: VideoFile = attribute2.find(videoFile => videoFile.magnetUri === videoFile1.magnetUri)
  256. if (!videoFile2) {
  257. return `Video ${video1.uuid} has missing video file ${videoFile1.magnetUri}.`
  258. }
  259. if (videoFile1.size !== videoFile2.size || videoFile1.resolution.label !== videoFile2.resolution.label) {
  260. return `Video ${video1.uuid} has different video file ${videoFile1.magnetUri}.`
  261. }
  262. })
  263. } else {
  264. if (attribute1 !== attribute2) {
  265. return `Video ${video1.uuid} has different value for attribute ${videoKey}.`
  266. }
  267. }
  268. }
  269. }
  270. return undefined
  271. }
  272. function goodbye () {
  273. return process.exit(-1)
  274. }
  275. async function isTherePendingRequests (servers: ServerInfo[]) {
  276. const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
  277. const tasks: Promise<any>[] = []
  278. let pendingRequests = false
  279. // Check if each server has pending request
  280. for (const server of servers) {
  281. for (const state of states) {
  282. const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt')
  283. .then(res => {
  284. if (res.body.total > 0) pendingRequests = true
  285. })
  286. tasks.push(p)
  287. }
  288. }
  289. await Promise.all(tasks)
  290. return pendingRequests
  291. }