simulate-many-viewers.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Bluebird from 'bluebird'
  2. import { wait } from '@peertube/peertube-core-utils'
  3. import {
  4. createSingleServer,
  5. doubleFollow,
  6. killallServers,
  7. PeerTubeServer,
  8. setAccessTokensToServers,
  9. waitJobs
  10. } from '@peertube/peertube-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. log_http_requests: false
  30. },
  31. rates_limit: {
  32. api: {
  33. max: 5_000_000
  34. }
  35. },
  36. views: {
  37. videos: {
  38. local_buffer_update_interval: '30 minutes',
  39. ip_view_expiration: '1 hour'
  40. }
  41. }
  42. }
  43. const env = { PRODUCTION_CONSTANTS: 'true' }
  44. servers = await Promise.all([
  45. createSingleServer(1, config, { env, nodeArgs: [ '--inspect' ] }),
  46. createSingleServer(2, config, { env }),
  47. createSingleServer(3, config, { env })
  48. ])
  49. await setAccessTokensToServers(servers)
  50. await doubleFollow(servers[0], servers[1])
  51. await doubleFollow(servers[0], servers[2])
  52. const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
  53. videoId = uuid
  54. await waitJobs(servers)
  55. const THOUSAND_VIEWERS = 2
  56. for (let i = 2; i < 252; i++) {
  57. for (let j = 2; j < 6; j++) {
  58. for (let k = 2; k < THOUSAND_VIEWERS + 2; k++) {
  59. viewers.push({ xForwardedFor: `0.${k}.${j}.${i},127.0.0.1` })
  60. }
  61. }
  62. }
  63. console.log('Servers preparation finished.')
  64. }
  65. async function runViewers () {
  66. console.log('Will run views of %d viewers.', viewers.length)
  67. const before = new Date().getTime()
  68. await Bluebird.map(viewers, viewer => {
  69. return servers[0].views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor })
  70. }, { concurrency: 500 })
  71. console.log('Finished to run views in %d seconds.', (new Date().getTime() - before) / 1000)
  72. await wait(5000)
  73. }