Skip to content

Commit 5f9aa29

Browse files
authored
fix RecordRate rate controller (#1292)
Addresses the record rate controller as it doesn't work and causes a node stack overflow closes #1131 Signed-off-by: D <d_kelsey@uk.ibm.com>
1 parent a372f18 commit 5f9aa29

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed

packages/caliper-core/lib/worker/rate-control/recordRate.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ class RecordRateController extends RateInterface {
7878
this.pathTemplate = this.pathTemplate.replace(/<C>/gi, this.workerIndex.toString());
7979
this.pathTemplate = util.resolvePath(this.pathTemplate);
8080

81-
this.rateController = new RateControl(testMessage, stats, workerIndex,);
81+
let rateControllerTestMessage = this.testMessage.clone();
82+
rateControllerTestMessage.setRateControlSpec(this.options.rateController);
83+
84+
this.recordedRateController = new RateControl(rateControllerTestMessage, stats, workerIndex);
8285
}
8386

8487
/**
@@ -128,7 +131,7 @@ class RecordRateController extends RateInterface {
128131
* @async
129132
*/
130133
async applyRateControl() {
131-
await this.rateController.applyRateControl();
134+
await this.recordedRateController.applyRateControl();
132135
this.records[this.stats.getTotalSubmittedTx()] = Date.now() - this.stats.getRoundStartTime();
133136
}
134137

@@ -137,7 +140,7 @@ class RecordRateController extends RateInterface {
137140
* @async
138141
*/
139142
async end() {
140-
await this.rateController.end();
143+
await this.recordedRateController.end();
141144

142145
try {
143146
switch (this.outputFormat) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
class MockRate {
16+
17+
reset() {
18+
MockRate.applyRateControlCalled = false;
19+
}
20+
21+
isApplyRateControlCalled() {
22+
return MockRate.applyRateControlCalled;
23+
}
24+
25+
async applyRateControl() {
26+
MockRate.applyRateControlCalled = true;
27+
}
28+
}
29+
30+
function createRateController(testMessage, stats, workerIndex) {
31+
return new MockRate();
32+
}
33+
34+
module.exports.createRateController = createRateController;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const mockery = require('mockery');
18+
const path = require('path');
19+
const RecordRate = require('../../../lib/worker/rate-control/recordRate');
20+
const TestMessage = require('../../../lib/common/messages/testMessage');
21+
const MockRate = require('./mockRate');
22+
const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector');
23+
24+
const chai = require('chai');
25+
chai.should();
26+
const sinon = require('sinon');
27+
28+
describe('RecordRate controller', () => {
29+
before(() => {
30+
mockery.enable({
31+
warnOnReplace: false,
32+
warnOnUnregistered: false,
33+
useCleanCache: true
34+
});
35+
36+
mockery.registerMock(path.join(__dirname, '../../../lib/worker/rate-control/noRate.js'), MockRate);
37+
});
38+
39+
after(() => {
40+
mockery.deregisterAll();
41+
mockery.disable();
42+
});
43+
44+
it('should apply rate control to the recorded rate controller', async () => {
45+
const msgContent = {
46+
label: 'test',
47+
rateControl: {
48+
"type": "record-rate",
49+
"opts": {
50+
"rateController": {
51+
"type": "zero-rate"
52+
},
53+
"pathTemplate": "../tx_records_client<C>_round<R>.txt",
54+
"outputFormat": "TEXT",
55+
"logEnd": true
56+
}
57+
},
58+
workload: {
59+
module: 'module.js'
60+
},
61+
testRound: 0,
62+
txDuration: 250,
63+
totalWorkers: 2
64+
};
65+
66+
const testMessage = new TestMessage('test', [], msgContent);
67+
const stubStatsCollector = sinon.createStubInstance(TransactionStatisticsCollector);
68+
const rateController = RecordRate.createRateController(testMessage, stubStatsCollector, 0);
69+
const mockRate = MockRate.createRateController();
70+
mockRate.reset();
71+
mockRate.isApplyRateControlCalled().should.equal(false);
72+
await rateController.applyRateControl();
73+
mockRate.isApplyRateControlCalled().should.equal(true);
74+
});
75+
76+
it('should throw an error if the rate controller to record is unknown', async () => {
77+
const msgContent = {
78+
label: 'test',
79+
rateControl: {
80+
"type": "record-rate",
81+
"opts": {
82+
"rateController": {
83+
"type": "nonexistent-rate"
84+
},
85+
"pathTemplate": "../tx_records_client<C>_round<R>.txt",
86+
"outputFormat": "TEXT",
87+
"logEnd": true
88+
}
89+
},
90+
workload: {
91+
module: 'module.js'
92+
},
93+
testRound: 0,
94+
txDuration: 250,
95+
totalWorkers: 2
96+
};
97+
const testMessage = new TestMessage('test', [], msgContent);
98+
99+
const stubStatsCollector = sinon.createStubInstance(TransactionStatisticsCollector);
100+
(() => {
101+
RecordRate.createRateController(testMessage, stubStatsCollector, 0)
102+
}).should.throw(/Module "nonexistent-rate" could not be loaded/);
103+
});
104+
});

0 commit comments

Comments
 (0)