Skip to content

Commit

Permalink
Merge pull request #34 from SamYuan1990/orderBySupport
Browse files Browse the repository at this point in the history
add support for orderer by
  • Loading branch information
SamYuan1990 committed Nov 18, 2020
2 parents be5ada9 + d5da722 commit 3eb01b2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 55 deletions.
32 changes: 14 additions & 18 deletions lib/fileIO.js
Expand Up @@ -9,29 +9,25 @@ exports.AbsoluteMaxBytes = 'AbsoluteMaxBytes';
exports.PreferredMaxBytes = 'PreferredMaxBytes';
exports.TPS = 'TPS';

exports.loadRs = function loadRs(lab) {
function keySort(key) {
return function(a, b) {
return a[key] - b[key];
};
}

exports.loadRs = function loadRs(data, orderby) {
const rs = [];
const input = fs.readFileSync('./data/rs.csv', 'utf-8');
const records = parse(input, {
columns: true,
skip_empty_lines: true
});
records.forEach(element => {
if (lab === this.BatchTimeout) {
rs.push(+element.BatchTimeout);
}
if (lab === this.MaxMessageCount) {
rs.push(+element.MaxMessageCount);
}
if (lab === this.AbsoluteMaxBytes) {
rs.push(+element.AbsoluteMaxBytes);
}
if (lab === this.PreferredMaxBytes) {
rs.push(+element.PreferredMaxBytes);
}
if (lab === this.TPS) {
rs.push(+element.TPS);
}
let orderedRecords = records;
if (orderby) {
orderedRecords = records.sort(keySort(orderby));
}
orderedRecords.forEach(element => {
rs.push(+element[data]);
});
return rs;
};
Expand All @@ -54,4 +50,4 @@ exports.appendRS = function appendRS(data) {
/* 处理错误 */
}
return 0;
};
};
6 changes: 5 additions & 1 deletion routes/api.js
Expand Up @@ -15,7 +15,11 @@ router.get('/', function(req, res, next) {

// get?data=?&orderby=?
router.get('/get', function(req, res, next) {
res.send(fileIO.loadRs(req.query.data));
if (!req.query.orderby) {
res.send(fileIO.loadRs(req.query.data));
} else {
res.send(fileIO.loadRs(req.query.data, req.query.orderby));
}
});

function prepareArray(input) {
Expand Down
6 changes: 6 additions & 0 deletions test/app.test.js
Expand Up @@ -56,6 +56,12 @@ describe('# test app.js', function () {
.get('/api/get?data=TPS')
.expect(200, done);
});

it('GET /api/getTPS', function (done) {
request
.get('/api/get?data=TPS&orderby=BatchTimeout')
.expect(200, done);
});
it('GET / 200 result', function (done) {
request
.get('/result')
Expand Down
61 changes: 25 additions & 36 deletions test/fileIO.test.js
Expand Up @@ -4,63 +4,52 @@ const expect = require('chai').expect;
describe('# fileIO', function () {
it('appendRS', function(done) {
fileIO.init();
fileIO.appendRS('sample,0.75,10,2,256, 180.038278,');
fileIO.appendRS('sample,0.75,40,2,256, 291.310916,');
fileIO.appendRS('sample,0.75,80,2,256, 333.041573,');
fileIO.appendRS('sample,0.75,120,2,256, 351.752320,');
fileIO.appendRS('sample,1,10,2,256, 172.872861,');
fileIO.appendRS('sample,1,40,2,256, 291.617799,');
fileIO.appendRS('sample,1,80,2,256, 337.826232,');
fileIO.appendRS('sample,1,120,2,256, 319.039588,');
fileIO.appendRS('sample,2,10,2,256, 182.105577,');
fileIO.appendRS('sample,2,40,2,256, 260.276446,');
fileIO.appendRS('sample,2,80,2,256, 323.542760,');
fileIO.appendRS('sample,2,120,2,256, 323.526945,');
fileIO.appendRS('sample,1.5,10,2,256, 172.745382,');
fileIO.appendRS('sample,1.5,40,2,256, 268.041591,');
fileIO.appendRS('sample,1.5,80,2,256, 348.150198,');
fileIO.appendRS('sample,1.5,120,2,256, 310.915616,');
fileIO.appendRS('sample,1,10,2,256, 110,');
fileIO.appendRS('sample,2,10,2,256, 210,');
fileIO.appendRS('sample,4,10,2,256, 410,');
fileIO.appendRS('sample,8,10,2,256, 810,');
fileIO.appendRS('sample,1,20,2,256, 120,');
fileIO.appendRS('sample,2,20,2,256, 220,');
fileIO.appendRS('sample,4,20,2,256, 420,');
fileIO.appendRS('sample,8,20,2,256, 820,');
done();
});

it('get BatchTimeout', function (done) {
expect([0.75, 0.75, 0.75, 0.75, 1, 1, 1, 1, 2, 2, 2, 2, 1.5, 1.5, 1.5, 1.5]).to.deep.equal(
expect([1, 2, 4, 8, 1, 2, 4, 8]).to.deep.equal(
fileIO.loadRs(fileIO.BatchTimeout));
done();
});
it('get MaxMessageCount', function (done) {
expect([10, 40, 80, 120, 10, 40, 80, 120, 10, 40, 80, 120, 10, 40, 80, 120]).to.deep.equal(
expect([10, 10, 10, 10, 20, 20, 20, 20]).to.deep.equal(
fileIO.loadRs(fileIO.MaxMessageCount));
done();
});
it('get AbsoluteMaxBytes', function (done) {
expect([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]).to.deep.equal(
expect([2, 2, 2, 2, 2, 2, 2, 2]).to.deep.equal(
fileIO.loadRs(fileIO.AbsoluteMaxBytes));
done();
});
it('get PreferredMaxBytes', function (done) {
expect([256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256]).to.deep.equal(
expect([256, 256, 256, 256, 256, 256, 256, 256]).to.deep.equal(
fileIO.loadRs(fileIO.PreferredMaxBytes));
done();
});
it('get TPS', function (done) {
expect([180.038278,
291.310916,
333.041573,
351.752320,
172.872861,
291.617799,
337.826232,
319.039588,
182.105577,
260.276446,
323.542760,
323.526945,
172.745382,
268.041591,
348.150198,
310.915616]).to.deep.equal(
expect([110, 210, 410, 810, 120, 220, 420, 820]).to.deep.equal(
fileIO.loadRs(fileIO.TPS));
done();
});

it('get BatchTimeout order by BatchTimeout', function (done) {
expect([1, 1, 2, 2, 4, 4, 8, 8]).to.deep.equal(
fileIO.loadRs(fileIO.BatchTimeout, fileIO.BatchTimeout));
done();
});

it('get TPS order by BatchTimeout', function (done) {
expect([110, 120, 210, 220, 410, 420, 810, 820]).to.deep.equal(
fileIO.loadRs(fileIO.TPS, fileIO.BatchTimeout));
done();
});
});
13 changes: 13 additions & 0 deletions test/lib.test.js
Expand Up @@ -63,6 +63,19 @@ describe('# libs', function () {
done();
});

it('error handle', function(done) {
const rs = {
DryRun: false,
info: 'echo',
command: 'exit',
tps: true,
args: ['1'],
Path: './s'
};
expect(-1).to.be.equals(libs.executeCommand(rs));
done();
});

it('should success if dry run', function(done) {
const rs = {
DryRun: false,
Expand Down

0 comments on commit 3eb01b2

Please sign in to comment.