perf.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Generated by CoffeeScript 1.7.1
  2. (function() {
  3. var AREAS, DAMAGE_MAX, Employee, Location, Report, Sequelize, arg, async, sequelize, start;
  4. Sequelize = require('./index');
  5. async = require('async');
  6. AREAS = ['area1', 'area2', 'area3'];
  7. DAMAGE_MAX = 3;
  8. sequelize = new Sequelize('sequelize_test', 'sequelize_test', '', {
  9. dialect: 'postgres',
  10. host: '172.17.0.2',
  11. logging: false
  12. });
  13. Report = sequelize.define('Report', {
  14. area: {
  15. type: Sequelize.STRING,
  16. allowNull: false,
  17. validate: {
  18. isIn: [AREAS]
  19. }
  20. },
  21. damageLevel: {
  22. type: Sequelize.INTEGER,
  23. allowNull: false,
  24. defaultValue: 0,
  25. validate: {
  26. isInt: {
  27. msg: "Must be an integer"
  28. },
  29. min: 0,
  30. max: DAMAGE_MAX
  31. }
  32. },
  33. description: {
  34. type: Sequelize.TEXT,
  35. allowNull: true,
  36. validate: {
  37. notEmpty: true
  38. }
  39. }
  40. });
  41. Location = sequelize.define('Location', {
  42. latitude: {
  43. type: Sequelize.FLOAT(53),
  44. allowNull: false,
  45. validate: {
  46. isFloat: {
  47. msg: "Must be a floating point number"
  48. },
  49. min: -90.0,
  50. max: 90.0
  51. }
  52. },
  53. longitude: {
  54. type: Sequelize.FLOAT(53),
  55. allowNull: false,
  56. validate: {
  57. isFloat: {
  58. msg: "Must be a floating point number"
  59. },
  60. min: -180.0,
  61. max: 180.0
  62. }
  63. },
  64. altitude: {
  65. type: Sequelize.FLOAT(53),
  66. allowNull: true,
  67. defaultValue: null
  68. }
  69. });
  70. Employee = sequelize.define('Employee', {
  71. name: {
  72. type: Sequelize.STRING,
  73. allowNull: false
  74. }
  75. });
  76. Location.hasMany(Report);
  77. Report.belongsTo(Location);
  78. Employee.hasMany(Report);
  79. Report.hasMany(Employee);
  80. process.argv.shift();
  81. process.argv.shift();
  82. arg = process.argv[0];
  83. if (arg === 'seed') {
  84. sequelize.sync({
  85. force: true
  86. }).success(function() {
  87. console.log("synced database");
  88. Employee.bulkCreate([
  89. {name: 'A'},
  90. {name: 'B'},
  91. {name: 'C'}
  92. ]).then(function () {
  93. Employee.findAll().then(function (employees) {
  94. return async.timesSeries(2000, function(n, done) {
  95. var location;
  96. location = null;
  97. return async.series([
  98. function(done) {
  99. return Location.create({
  100. latitude: Math.random() * 180 - 90,
  101. longitude: Math.random() * 360 - 180
  102. }).success(function(obj) {
  103. location = obj;
  104. return done();
  105. }).error(function(error) {
  106. return console.error(error);
  107. });
  108. }, function(done) {
  109. return async.times(2, function(n, done) {
  110. return Report.create({
  111. area: AREAS[n],
  112. damageLevel: String(Math.floor(Math.random() * DAMAGE_MAX)),
  113. description: Math.random() < 0.1 ? "this is a description " + (Math.random()) : null
  114. }).success(function(report) {
  115. return report.setLocation(location).done(function (err) {
  116. if (err) return done(err);
  117. report.setEmployees(employees).done(done);
  118. });
  119. }).error(function(error) {
  120. return console.error(error);
  121. });
  122. }, done);
  123. }
  124. ], function() {
  125. console.log(n);
  126. return done();
  127. });
  128. }, function(error) {
  129. if (error != null) {
  130. throw error;
  131. }
  132. return console.log("done");
  133. });
  134. });
  135. });
  136. }).error(function(error) {
  137. throw error;
  138. });
  139. } else if (arg === 'run') {
  140. console.log("start");
  141. start = new Date();
  142. /*
  143. Report.findAll
  144. include: [
  145. Location
  146. ]
  147. .done (err, damageReports) ->
  148. console.log "finish, took #{new Date() - start}"
  149. .on 'sql', (sql) ->
  150. console.log sql
  151. */
  152. Location.findAll({
  153. include: [
  154. {model: Report, include: [Employee]}
  155. ]
  156. }).done(function(err, locations) {
  157. if (err) return console.error(err.toString());
  158. return console.log("finish, took " + (new Date() - start));
  159. }).on('sql', function(sql) {
  160. return console.log(sql);
  161. });
  162. } else {
  163. console.log("expected to be run with 'seed' or 'run' argument");
  164. }
  165. }).call(this);