不要怂,就是干,撸起袖子干!

Commit d75829c1 by Sascha Depold

refactored example

1 parent 85149825
Showing with 29 additions and 29 deletions
...@@ -2,44 +2,43 @@ var Sequelize = require(__dirname + "/../../index") ...@@ -2,44 +2,43 @@ var Sequelize = require(__dirname + "/../../index")
, config = require("../../test/config") , config = require("../../test/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false, host: config.host}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false, host: config.host})
, QueryChainer = Sequelize.Utils.QueryChainer , QueryChainer = Sequelize.Utils.QueryChainer
, sys = require("sys")
var Person = sequelize.define('person', { name: Sequelize.STRING }) var Person = sequelize.define('person', { name: Sequelize.STRING })
Person.sync({force: true}).on('success', function() { Person.sync({force: true}).on('success', function() {
var start = Date.now() var start = Date.now()
, count = 10000 , count = 10000
, offset = 0 , done = 0
, stepWidth = 50
, queries = []
, chainer = new QueryChainer
var perform = function(cb) { var createPeople = function(callback) {
console.log("Begin to create " + offset + " - " + (offset+stepWidth) + " items!") Person.create({name: 'someone'}).on('success', callback).on('failure', function(err) { console.log(err) })
}
var createPeopleBatch = function(batchSize, callback) {
var done = 0
for(var i = 0; i < batchSize; i++)
createPeople(function() { (++done == batchSize) && callback() })
}
var batchCallback = function() {
sys.print(".");
for(var i = offset; i < (offset + stepWidth); i++) if((done += 50) != count)
chainer.add(Person.create({name: 'someone'})) createPeopleBatch(50, batchCallback)
else {
console.log("\nFinished creation of " + count + " people. Took: " + (Date.now() - start) + "ms")
start = Date.now()
console.log("Will now read them from the database:")
chainer.run().on('success', function() { Person.findAll().on('success', function(people) {
if(count - offset > 0) { console.log("Reading " + people.length + " items took: " + (Date.now() - start) + "ms")
offset += stepWidth })
perform(cb) }
} else {
console.log("Saving " + count + " items took: " + (Date.now() - start) + "ms")
cb && cb.call()
}
}).on('failure', function(err) {
console.log(err)
})
} }
perform(function() { console.log('Creating people :)')
start = Date.now() createPeopleBatch(50, batchCallback)
console.log("Will now read them from the database:")
Person.findAll().on('success', function(people) {
console.log("Reading " + people.length + " items took: " + (Date.now() - start) + "ms")
})
})
}).on('failure', function(err) { }).on('failure', function(err) {
console.log(err) console.log(err)
}) })
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!