Skip to content

Instantly share code, notes, and snippets.

@glesage
Created May 4, 2015 02:06
Show Gist options
  • Save glesage/0ed40306ade350c9569b to your computer and use it in GitHub Desktop.
Save glesage/0ed40306ade350c9569b to your computer and use it in GitHub Desktop.
Benchmark DB
Utility.benchmarkDB = function (next)
{
Facility = Utility.app.models.Facility;
FacilityDetail = Utility.app.models.FacilityDetail;
Driver = Utility.app.models.Driver;
DriverHour = Utility.app.models.DriverHour;
/**
* TIMES
*/
var createBegin = new Date().getTime();
var getBegin = new Date().getTime();
var updateBegin = new Date().getTime();
var creates = [];
var number = 0;
while (number < 10000)
{
var createFacility = Facility.createAsync({name: "Facility_" + number})
.then(function(facility) {
return FacilityDetail.createAsync({
facilityId: facility.id,
braintreeId: "todo",
braintreeStatus: "valid",
phone: "(888) 253-2613",
laundryPrice: 0.25,
minimumOrder: 20,
website: "http://www.somecleaner.com/"
});
}).then(function(facilityDetail)
{
return Driver.createAsync({ name: "driver_" + number,
facilityId: facilityDetail.facilityId,
userId: 0
});
}).then(function(driver)
{
return DriverHour.createAsync({ driverId: driver.id,
monHoursId: 1,
tueHoursId: 1,
wedHoursId: 1,
thuHoursId: 1,
friHoursId: 1,
satHoursId: 1,
sunHoursId: 1
});
});
number++;
creates.push(createFacility);
}
BPromise.all(creates).then(function(result)
{
var currentTime = new Date().getTime();
var time = currentTime-createBegin;
getBegin = currentTime;
console.log(time + "ms - Create 40000 models");
return DriverHour.findAsync({"include": {"driver": {"facility" : "details"}}});
})
.then(function()
{
var currentTime = new Date().getTime();
var time = currentTime-getBegin;
updateBegin = currentTime;
console.log(time + "ms - Get 40000 models");
return Facility.updateAllAsync({}, {"name": "me"})
.then(function(result)
{
return FacilityDetail.updateAllAsync({}, {"laundryPrice": 0.50})
.then(function(result)
{
return Driver.updateAllAsync({}, {"userId": 1});
}).then(function(result)
{
return DriverHour.updateAllAsync({}, {"monHoursId": 2});
});
});
}).then(function()
{
var currentTime = new Date().getTime();
var time = currentTime-updateBegin;
console.log(time + "ms - Update 40000 models");
next(null, "Created, read & updated facilities");
}).catch(next);
};
loopback.remoteMethod(Utility.benchmarkDB,
{
description: 'Benchmark the DB',
returns: {arg: 'response', type: 'string'},
http: {path: '/benchmarkDB', verb: 'post'}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment