perf.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Sequelize = require './index'
  2. async = require 'async'
  3. AREAS = ['area1', 'area2', 'area3']
  4. DAMAGE_MAX = 3
  5. sequelize = new Sequelize 'sequelize_test', 'sequelize_test', '',
  6. dialect: 'postgres'
  7. host: '172.17.0.2'
  8. Report = sequelize.define 'Report',
  9. area:
  10. type: Sequelize.STRING
  11. allowNull: false
  12. validate:
  13. isIn: [AREAS]
  14. damageLevel:
  15. type: Sequelize.INTEGER
  16. allowNull: false
  17. defaultValue: 0
  18. validate:
  19. isInt: msg: "Must be an integer"
  20. min: 0
  21. max: DAMAGE_MAX
  22. description:
  23. type: Sequelize.TEXT
  24. allowNull: true
  25. validate:
  26. notEmpty: true
  27. Location = sequelize.define 'Location',
  28. latitude:
  29. type: Sequelize.FLOAT(53) # double
  30. allowNull: false
  31. validate:
  32. isFloat: msg: "Must be a floating point number"
  33. min: -90.0
  34. max: 90.0
  35. longitude:
  36. type: Sequelize.FLOAT(53) # double
  37. allowNull: false
  38. validate:
  39. isFloat: msg: "Must be a floating point number"
  40. min: -180.0
  41. max: 180.0
  42. altitude:
  43. type: Sequelize.FLOAT(53) # double
  44. allowNull: true
  45. defaultValue: null
  46. Employee = sequelize.define 'Employee',
  47. name:
  48. type: Sequelize.STRING,
  49. allowNull: false
  50. Location.hasMany Report
  51. Report.belongsTo Location
  52. Employee.hasMany Report
  53. Report.hasMany Employee
  54. process.argv.shift()
  55. process.argv.shift()
  56. arg = process.argv[0]
  57. if arg is 'seed'
  58. sequelize.sync(force: true).success ->
  59. console.log "synced database"
  60. async.timesSeries 2000, (n, done) ->
  61. location = null
  62. async.series [
  63. (done) ->
  64. Location.create
  65. latitude: Math.random() * 180 - 90
  66. longitude: Math.random() * 360 - 180
  67. .success (obj) ->
  68. location = obj
  69. done()
  70. .error (error) ->
  71. console.error(error)
  72. (done) ->
  73. async.times 2, (n, done) ->
  74. Report.create
  75. area: AREAS[n]
  76. damageLevel: String Math.floor Math.random() * DAMAGE_MAX
  77. description: if Math.random() < 0.1 then "this is a description #{Math.random()}" else null
  78. .success (report) ->
  79. report.setLocation(location).done done
  80. .error (error) ->
  81. console.error(error)
  82. , done
  83. ], ->
  84. console.log n
  85. done()
  86. , (error) ->
  87. throw error if error?
  88. console.log "done"
  89. .error (error) ->
  90. throw error
  91. else if arg is 'run'
  92. console.log "start"
  93. start = new Date()
  94. ###
  95. Report.findAll
  96. include: [
  97. Location
  98. ]
  99. .done (err, damageReports) ->
  100. console.log "finish, took #{new Date() - start}"
  101. .on 'sql', (sql) ->
  102. console.log sql
  103. ###
  104. Location.findAll
  105. include: [
  106. Report
  107. ]
  108. .done (err, damageReports) ->
  109. console.log "finish, took #{new Date() - start}"
  110. .on 'sql', (sql) ->
  111. console.log sql
  112. else
  113. console.log "expected to be run with 'seed' or 'run' argument"