Skip to content

Instantly share code, notes, and snippets.

@barberdt
Created February 1, 2012 21:59
Show Gist options
  • Save barberdt/b8dd9ab032fec3a7b876 to your computer and use it in GitHub Desktop.
Save barberdt/b8dd9ab032fec3a7b876 to your computer and use it in GitHub Desktop.
var confApi = require('bugswarm-cfg');
var partApi = require('bugswarm-prt');
var ApiKeyService = confApi.ApiKeyService;
var SwarmService = confApi.SwarmService;
var ResourceService = confApi.ResourceService;
var Swarm = partApi.Swarm;
describe('Throughput testing', function() {
var confKey,
partKey,
apiKeyService,
swarmService,
resourceService,
swarmId;
before(function(done) {
apiKeyService = new ApiKeyService('librarytest', 'test');
apiKeyService.generate(function(err, keys) {
for (var i=0,len=keys.length; i<len; i++) {
if (keys[i].type === 'configuration') {
confKey = keys[i].key;
} else if (keys[i].type === 'participation') {
partKey = keys[i].key;
}
}
swarmService = new SwarmService(confKey);
resourceService = new ResourceService(confKey);
var swarm = {
name: 'Throughput Swarm',
public: false,
description: 'The swarm used for throughput testing'
};
swarmService.create(swarm, function(err, swarm) {
swarmId = swarm.id;
var resource = {
name: 'Init Resource',
description: 'The first resource',
machine_type: 'pc'
};
resourceService.create(resource, function(err, resource) {
var addResource = {
swarm_id: swarmId,
resource_id: resource.id,
resource_type: 'producer'
};
swarmService.addResource(addResource, function(err) {
addResource.resource_type = 'consumer';
swarmService.addResource(addResource, function(err) {
var options = {
apikey: partKey,
resource: resource.id,
swarms: swarmId
};
var initParticipant = new Swarm(options);
initParticipant.on('connect', function(err) {
console.log('Init Participant Connected');
done();
});
initParticipant.on('message', function(message) {
console.log(message);
});
initParticipant.connect();
});
});
});
});
});
});
it('should connect 5 new resources to the swarm', function(done) {
var connections = 5;
var connCount = 0;
var disConnCount = 0;
for (var i=0; i<connections; i++) {
var resource = {
name: 'Resource ' + i,
description: 'This is resource ' + i,
machine_type: 'pc'
};
resourceService.create(resource, function(err, resource) {
var addResource = {
swarm_id: swarmId,
resource_id: resource.id,
resource_type: 'producer'
};
swarmService.addResource(addResource, function(err) {
if (err) {
console.log(err);
}
var options = {
apikey: partKey,
resource: resource.id,
swarms: swarmId
};
var producer = new Swarm(options);
producer.on('connect', function(err) {
connCount++;
if (connCount%10 === 0) {
producer.send('Count: ' + connCount);
}
producer.disconnect();
});
producer.on('disconnect', function() {
disConnCount++;
if (disConnCount === connections) {
done();
}
});
producer.connect();
});
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment