simulate-many-viewers.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Bluebird from 'bluebird'
  2. import { wait } from '@shared/core-utils'
  3. import {
  4. createSingleServer,
  5. doubleFollow,
  6. killallServers,
  7. PeerTubeServer,
  8. setAccessTokensToServers,
  9. waitJobs
  10. } from '@shared/server-commands'
  11. let servers: PeerTubeServer[]
  12. const viewers: { xForwardedFor: string }[] = []
  13. let videoId: string
  14. run()
  15. .then(() => process.exit(0))
  16. .catch(err => console.error(err))
  17. .finally(() => killallServers(servers))
  18. async function run () {
  19. await prepare()
  20. while (true) {
  21. await runViewers()
  22. }
  23. }
  24. async function prepare () {
  25. console.log('Preparing servers...')
  26. const config = {
  27. log: {
  28. level: 'info'
  29. },
  30. rates_limit: {
  31. api: {
  32. max: 5_000_000
  33. }
  34. },
  35. views: {
  36. videos: {
  37. local_buffer_update_interval: '30 minutes',
  38. ip_view_expiration: '1 hour'
  39. }
  40. }
  41. }
  42. const env = { PRODUCTION_CONSTANTS: 'true' }
  43. servers = await Promise.all([
  44. createSingleServer(1, config, { env, nodeArgs: [ '--inspect' ] }),
  45. createSingleServer(2, config, { env }),
  46. createSingleServer(3, config, { env })
  47. ])
  48. await setAccessTokensToServers(servers)
  49. await doubleFollow(servers[0], servers[1])
  50. await doubleFollow(servers[0], servers[2])
  51. const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
  52. videoId = uuid
  53. await waitJobs(servers)
  54. const THOUSAND_VIEWERS = 2
  55. for (let i = 2; i < 252; i++) {
  56. for (let j = 2; j < 6; j++) {
  57. for (let k = 2; k < THOUSAND_VIEWERS + 2; k++) {
  58. viewers.push({ xForwardedFor: `0.${k}.${j}.${i},127.0.0.1` })
  59. }
  60. }
  61. }
  62. console.log('Servers preparation finished.')
  63. }
  64. async function runViewers () {
  65. console.log('Will run views of %d viewers.', viewers.length)
  66. const before = new Date().getTime()
  67. await Bluebird.map(viewers, viewer => {
  68. return servers[0].views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor })
  69. }, { concurrency: 500 })
  70. console.log('Finished to run views in %d seconds.', (new Date().getTime() - before) / 1000)
  71. await wait(5000)
  72. }