From d029784cde8111f636eedb46fb4537b95fee41fe Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 12 May 2024 00:33:36 +0530 Subject: [PATCH 01/10] Add tests for caliper-core/lib/compositeRate.js Signed-off-by: Abhinav Pandey --- .../test/worker/rate-control/compositeRate.js | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 packages/caliper-core/test/worker/rate-control/compositeRate.js diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js new file mode 100644 index 000000000..05443eb9b --- /dev/null +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -0,0 +1,116 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const chai = require('chai'); +const expect = chai.expect; +const CompositeRateController = require('../../../lib/worker/rate-control/compositeRate.js'); +// const RateControl = require('../../../lib/worker/rate-control/rateControl.js'); +// const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); +const TestMessage = require('../../../lib/common/messages/testMessage.js'); + +/** + * Encapsulates a controller and its scheduling information. + * + * @property {boolean} isLast Indicates whether the controller is the last in the round. + * @property {RateControl} controller The controller instance. + * @property {number} lastTxIndex The last TX index associated with the controller based on its weight. Only used in Tx number-based rounds. + * @property {number} relFinishTime The finish time of the controller based on its weight, relative to the start time of the round. Only used in duration-based rounds. + * @property {TransactionStatisticsCollector} txStatSubCollector The TX stat (sub-)collector associated with the sub-controller. + */ + +describe('CompositeRateController', () => { + let testMessage; + beforeEach(() => { + let msgContent = { + label: 'query2', + rateControl: { + type: 'fixed-rate', + opts: {}, + }, + workload: { + module: './../queryByChannel.js', + }, + testRound: 0, + txDuration: 250, + totalWorkers: 2, + weights: [1], + rateControllers: ['composite-rate'], + }; + testMessage = new TestMessage('test', [], msgContent); + }); + + describe('Initialization', () => { + it('should correctly initialize with default settings', () => { + const testMessage = new TestMessage('test', [], { + weights: [1], + rateControllers: ['fixed-rate'] + }); + const controller = new CompositeRateController(testMessage, {}, 0); + expect(controller.activeControllerIndex).to.equal(0); + expect(controller.controllers.length).to.equal(1); + }); + }); + + describe('applyRateControl', () => { + it('should apply rate control correctly', async () => { + expect(() => CompositeRateController.applyRateControl()).be.a('function'); + }); + }); + + describe('#_prepareControllers', () => { + it('should throw error when weights and rateControllers are not arrays', async () => { + testMessage.content.weights = 'not an array'; + testMessage.content.rateControllers = 'not an array'; + expect(() => + CompositeRateController.createRateController(testMessage, {}, 0) + ).to.throw('Weight and controller definitions must be arrays.'); + }); + + it('should throw error when weights and rateControllers lengths are not the same', async () => { + testMessage.content.weights = [1, 2]; + testMessage.content.rateControllers = ['composite-rate']; + expect(() => + CompositeRateController.createRateController(testMessage, {}, 0) + ).to.throw( + 'The number of weights and controllers must be the same.' + ); + }); + + it('should throw error when weights contains non-numeric value', async () => { + testMessage.content.weights = [1, 'not a number']; + testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; + expect(() => + CompositeRateController.createRateController(testMessage, {}, 0) + ).to.throw('Not-a-number element among weights: not a number'); + }); + + it('should throw error when weights contains negative number', async () => { + testMessage.content.weights = [1, -2]; + testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; + expect(() => + CompositeRateController.createRateController(testMessage, {}, 0) + ).to.throw('Negative element among weights: -2'); + }); + + it('should throw error when all weights are zero', async () => { + testMessage.content.weights = [0, 0]; + testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; + expect(() => + CompositeRateController.createRateController(testMessage, {}, 0) + ).to.throw('Every weight is zero.'); + }); + }); +}); \ No newline at end of file From c8a609f885b064cc0ec86dbc4ab7e35f186b474d Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 12 May 2024 01:31:24 +0530 Subject: [PATCH 02/10] Update compositeRate.js Signed-off-by: Abhinav Pandey Update compositeRate.js Signed-off-by: Abhinav Pandey Revert unnecessary logs Signed-off-by: Abhinav Pandey Revert lib changes. Add changes to testMessage Signed-off-by: Abhinav Pandey Update testMessage for compositeRate.js Signed-off-by: Abhinav Pandey Update tsxStats for compositeRate.js Signed-off-by: Abhinav Pandey Update LICENSE Signed-off-by: Abhinav Pandey Update Imports for compositeRate.js Missing Functions in codebase causing errors Signed-off-by: Abhinav Pandey --- .../test/worker/rate-control/compositeRate.js | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 05443eb9b..2550f4cb9 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -16,23 +16,16 @@ const chai = require('chai'); const expect = chai.expect; -const CompositeRateController = require('../../../lib/worker/rate-control/compositeRate.js'); +const { createRateController } = require('../../../lib/worker/rate-control/compositeRate.js'); // const RateControl = require('../../../lib/worker/rate-control/rateControl.js'); -// const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); +const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); -/** - * Encapsulates a controller and its scheduling information. - * - * @property {boolean} isLast Indicates whether the controller is the last in the round. - * @property {RateControl} controller The controller instance. - * @property {number} lastTxIndex The last TX index associated with the controller based on its weight. Only used in Tx number-based rounds. - * @property {number} relFinishTime The finish time of the controller based on its weight, relative to the start time of the round. Only used in duration-based rounds. - * @property {TransactionStatisticsCollector} txStatSubCollector The TX stat (sub-)collector associated with the sub-controller. - */ describe('CompositeRateController', () => { let testMessage; + let CompositeRateController; + let stats; beforeEach(() => { let msgContent = { label: 'query2', @@ -48,19 +41,18 @@ describe('CompositeRateController', () => { totalWorkers: 2, weights: [1], rateControllers: ['composite-rate'], + workerArgs: 0, + numb: 0 }; testMessage = new TestMessage('test', [], msgContent); + stats = new TransactionStatisticsCollector(0, 0, 'query2'); + CompositeRateController = createRateController(testMessage, stats, 0); }); - describe('Initialization', () => { + describe('#constructor', () => { it('should correctly initialize with default settings', () => { - const testMessage = new TestMessage('test', [], { - weights: [1], - rateControllers: ['fixed-rate'] - }); - const controller = new CompositeRateController(testMessage, {}, 0); - expect(controller.activeControllerIndex).to.equal(0); - expect(controller.controllers.length).to.equal(1); + expect(CompositeRateController.activeControllerIndex).to.equal(0); + expect(CompositeRateController.controllers.length).to.equal(0); }); }); @@ -75,7 +67,7 @@ describe('CompositeRateController', () => { testMessage.content.weights = 'not an array'; testMessage.content.rateControllers = 'not an array'; expect(() => - CompositeRateController.createRateController(testMessage, {}, 0) + CompositeRateController.createRateController(testMessage, stats, 0) ).to.throw('Weight and controller definitions must be arrays.'); }); @@ -83,7 +75,7 @@ describe('CompositeRateController', () => { testMessage.content.weights = [1, 2]; testMessage.content.rateControllers = ['composite-rate']; expect(() => - CompositeRateController.createRateController(testMessage, {}, 0) + CompositeRateController.createRateController(testMessage, stats, 0) ).to.throw( 'The number of weights and controllers must be the same.' ); @@ -93,7 +85,7 @@ describe('CompositeRateController', () => { testMessage.content.weights = [1, 'not a number']; testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; expect(() => - CompositeRateController.createRateController(testMessage, {}, 0) + CompositeRateController.createRateController(testMessage, stats, 0) ).to.throw('Not-a-number element among weights: not a number'); }); @@ -101,7 +93,7 @@ describe('CompositeRateController', () => { testMessage.content.weights = [1, -2]; testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; expect(() => - CompositeRateController.createRateController(testMessage, {}, 0) + CompositeRateController.createRateController(testMessage, stats, 0) ).to.throw('Negative element among weights: -2'); }); @@ -109,7 +101,7 @@ describe('CompositeRateController', () => { testMessage.content.weights = [0, 0]; testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; expect(() => - CompositeRateController.createRateController(testMessage, {}, 0) + CompositeRateController.createRateController(testMessage, stats, 0) ).to.throw('Every weight is zero.'); }); }); From b0f32b77dbbef6cf20ded76868120e32ff1d0198 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 12 May 2024 03:31:18 +0530 Subject: [PATCH 03/10] Updated `rateControllers` in compositeRate.js Somehow, recursive calling occurs when these changes were made Signed-off-by: Abhinav Pandey --- .../caliper-core/test/worker/rate-control/compositeRate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 2550f4cb9..6c280f136 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -30,7 +30,7 @@ describe('CompositeRateController', () => { let msgContent = { label: 'query2', rateControl: { - type: 'fixed-rate', + type: 'composite-rate', opts: {}, }, workload: { @@ -40,7 +40,7 @@ describe('CompositeRateController', () => { txDuration: 250, totalWorkers: 2, weights: [1], - rateControllers: ['composite-rate'], + rateControllers: [{type: 'composite-rate', opts: {}}], workerArgs: 0, numb: 0 }; From 67bfd397757d923aa269915aa60bc64a0faa7020 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 16 May 2024 15:26:33 +0530 Subject: [PATCH 04/10] Update 'weights' and 'rateControllers' in compositeRate.js Signed-off-by: Abhinav Pandey --- .../lib/worker/rate-control/compositeRate.js | 6 +- .../test/worker/rate-control/compositeRate.js | 62 +++++++++++-------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/packages/caliper-core/lib/worker/rate-control/compositeRate.js b/packages/caliper-core/lib/worker/rate-control/compositeRate.js index 52d739b9d..c455ac949 100644 --- a/packages/caliper-core/lib/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/lib/worker/rate-control/compositeRate.js @@ -73,8 +73,10 @@ class CompositeRateController extends RateInterface{ * @private */ _prepareControllers() { - let weights = this.options.weights; - let rateControllers = this.options.rateControllers; + // let weights = this.options.weights; + // let rateControllers = this.options.rateControllers; + let weights = this.testMessage.content.weights; + let rateControllers = this.testMessage.content.rateControllers; if (!Array.isArray(weights) || !Array.isArray(rateControllers)) { let msg = 'Weight and controller definitions must be arrays.'; diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 6c280f136..5b5e800e6 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -16,12 +16,13 @@ const chai = require('chai'); const expect = chai.expect; -const { createRateController } = require('../../../lib/worker/rate-control/compositeRate.js'); +const { + createRateController, +} = require('../../../lib/worker/rate-control/compositeRate.js'); // const RateControl = require('../../../lib/worker/rate-control/rateControl.js'); const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); - describe('CompositeRateController', () => { let testMessage; let CompositeRateController; @@ -30,7 +31,7 @@ describe('CompositeRateController', () => { let msgContent = { label: 'query2', rateControl: { - type: 'composite-rate', + type: 'fixed-rate', opts: {}, }, workload: { @@ -40,9 +41,9 @@ describe('CompositeRateController', () => { txDuration: 250, totalWorkers: 2, weights: [1], - rateControllers: [{type: 'composite-rate', opts: {}}], + rateControllers: [{ type: 'fixed-rate', opts: {} }], workerArgs: 0, - numb: 0 + numb: 0, }; testMessage = new TestMessage('test', [], msgContent); stats = new TransactionStatisticsCollector(0, 0, 'query2'); @@ -52,13 +53,15 @@ describe('CompositeRateController', () => { describe('#constructor', () => { it('should correctly initialize with default settings', () => { expect(CompositeRateController.activeControllerIndex).to.equal(0); - expect(CompositeRateController.controllers.length).to.equal(0); + expect(CompositeRateController.controllers.length).to.equal(1); }); }); describe('applyRateControl', () => { it('should apply rate control correctly', async () => { - expect(() => CompositeRateController.applyRateControl()).be.a('function'); + expect(() => CompositeRateController.applyRateControl()).be.a( + 'function' + ); }); }); @@ -66,43 +69,50 @@ describe('CompositeRateController', () => { it('should throw error when weights and rateControllers are not arrays', async () => { testMessage.content.weights = 'not an array'; testMessage.content.rateControllers = 'not an array'; - expect(() => - CompositeRateController.createRateController(testMessage, stats, 0) - ).to.throw('Weight and controller definitions must be arrays.'); + expect(() => createRateController(testMessage, stats, 0)).to.throw( + 'Weight and controller definitions must be arrays.' + ); }); it('should throw error when weights and rateControllers lengths are not the same', async () => { testMessage.content.weights = [1, 2]; testMessage.content.rateControllers = ['composite-rate']; - expect(() => - CompositeRateController.createRateController(testMessage, stats, 0) - ).to.throw( + expect(() => createRateController(testMessage, stats, 0)).to.throw( 'The number of weights and controllers must be the same.' ); }); it('should throw error when weights contains non-numeric value', async () => { testMessage.content.weights = [1, 'not a number']; - testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; - expect(() => - CompositeRateController.createRateController(testMessage, stats, 0) - ).to.throw('Not-a-number element among weights: not a number'); + testMessage.content.rateControllers = [ + 'composite-rate', + 'composite-rate', + ]; + expect(() => createRateController(testMessage, stats, 0)).to.throw( + 'Not-a-number element among weights: not a number' + ); }); it('should throw error when weights contains negative number', async () => { testMessage.content.weights = [1, -2]; - testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; - expect(() => - CompositeRateController.createRateController(testMessage, stats, 0) - ).to.throw('Negative element among weights: -2'); + testMessage.content.rateControllers = [ + 'composite-rate', + 'composite-rate', + ]; + expect(() => createRateController(testMessage, stats, 0)).to.throw( + 'Negative element among weights: -2' + ); }); it('should throw error when all weights are zero', async () => { testMessage.content.weights = [0, 0]; - testMessage.content.rateControllers = ['composite-rate', 'composite-rate']; - expect(() => - CompositeRateController.createRateController(testMessage, stats, 0) - ).to.throw('Every weight is zero.'); + testMessage.content.rateControllers = [ + 'composite-rate', + 'composite-rate', + ]; + expect(() => createRateController(testMessage, stats, 0)).to.throw( + 'Every weight is zero.' + ); }); }); -}); \ No newline at end of file +}); From 945db2f76dc0b28d36d4d99b87740b63d13fb8cc Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 16 May 2024 15:27:34 +0530 Subject: [PATCH 05/10] Tests developed for compositeRate.js Final cleanup completed Signed-off-by: Abhinav Pandey --- packages/caliper-core/test/worker/rate-control/compositeRate.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 5b5e800e6..05088f9c4 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -19,7 +19,6 @@ const expect = chai.expect; const { createRateController, } = require('../../../lib/worker/rate-control/compositeRate.js'); -// const RateControl = require('../../../lib/worker/rate-control/rateControl.js'); const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); From 3bc033bf4256cda17eac41728441feed459977ae Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sat, 18 May 2024 00:31:32 +0530 Subject: [PATCH 06/10] Implemented tests for compositeRate.js Signed-off-by: Abhinav Pandey --- .../lib/worker/rate-control/compositeRate.js | 6 +-- .../test/worker/rate-control/compositeRate.js | 53 ++++++++++--------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/packages/caliper-core/lib/worker/rate-control/compositeRate.js b/packages/caliper-core/lib/worker/rate-control/compositeRate.js index c455ac949..52d739b9d 100644 --- a/packages/caliper-core/lib/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/lib/worker/rate-control/compositeRate.js @@ -73,10 +73,8 @@ class CompositeRateController extends RateInterface{ * @private */ _prepareControllers() { - // let weights = this.options.weights; - // let rateControllers = this.options.rateControllers; - let weights = this.testMessage.content.weights; - let rateControllers = this.testMessage.content.rateControllers; + let weights = this.options.weights; + let rateControllers = this.options.rateControllers; if (!Array.isArray(weights) || !Array.isArray(rateControllers)) { let msg = 'Weight and controller definitions must be arrays.'; diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 05088f9c4..f57455a2b 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -1,16 +1,16 @@ /* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -30,8 +30,11 @@ describe('CompositeRateController', () => { let msgContent = { label: 'query2', rateControl: { - type: 'fixed-rate', - opts: {}, + type: 'composite-rate', + opts: { + weights: [1], + rateControllers: [{ type: 'fixed-rate', opts: {} }] + }, }, workload: { module: './../queryByChannel.js', @@ -39,8 +42,6 @@ describe('CompositeRateController', () => { testRound: 0, txDuration: 250, totalWorkers: 2, - weights: [1], - rateControllers: [{ type: 'fixed-rate', opts: {} }], workerArgs: 0, numb: 0, }; @@ -66,24 +67,24 @@ describe('CompositeRateController', () => { describe('#_prepareControllers', () => { it('should throw error when weights and rateControllers are not arrays', async () => { - testMessage.content.weights = 'not an array'; - testMessage.content.rateControllers = 'not an array'; + testMessage.content.rateControl.opts.weights = 'not an array'; + testMessage.content.rateControl.opts.rateControllers = 'not an array'; expect(() => createRateController(testMessage, stats, 0)).to.throw( 'Weight and controller definitions must be arrays.' ); }); it('should throw error when weights and rateControllers lengths are not the same', async () => { - testMessage.content.weights = [1, 2]; - testMessage.content.rateControllers = ['composite-rate']; + testMessage.content.rateControl.opts.weights = [1, 2]; + testMessage.content.rateControl.opts.rateControllers = ['composite-rate']; expect(() => createRateController(testMessage, stats, 0)).to.throw( 'The number of weights and controllers must be the same.' ); }); it('should throw error when weights contains non-numeric value', async () => { - testMessage.content.weights = [1, 'not a number']; - testMessage.content.rateControllers = [ + testMessage.content.rateControl.opts.weights = [1, 'not a number']; + testMessage.content.rateControl.opts.rateControllers = [ 'composite-rate', 'composite-rate', ]; @@ -93,8 +94,8 @@ describe('CompositeRateController', () => { }); it('should throw error when weights contains negative number', async () => { - testMessage.content.weights = [1, -2]; - testMessage.content.rateControllers = [ + testMessage.content.rateControl.opts.weights = [1, -2]; + testMessage.content.rateControl.opts.rateControllers = [ 'composite-rate', 'composite-rate', ]; @@ -104,8 +105,8 @@ describe('CompositeRateController', () => { }); it('should throw error when all weights are zero', async () => { - testMessage.content.weights = [0, 0]; - testMessage.content.rateControllers = [ + testMessage.content.rateControl.opts.weights = [0, 0]; + testMessage.content.rateControl.opts.rateControllers = [ 'composite-rate', 'composite-rate', ]; From 2c589430a30467eb59a03257a475d45a70a08b09 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sat, 18 May 2024 00:34:45 +0530 Subject: [PATCH 07/10] Update LICENSE Signed-off-by: Abhinav Pandey --- .../test/worker/rate-control/compositeRate.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index f57455a2b..9839f1d71 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -1,16 +1,16 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ 'use strict'; From fa203b84d2f6791ef3ff750c312dfe04d1736596 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sat, 18 May 2024 01:15:28 +0530 Subject: [PATCH 08/10] Add test suites for controllerSwitchForDuration Signed-off-by: Abhinav Pandey --- .../test/worker/rate-control/compositeRate.js | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 9839f1d71..383b781b5 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -15,10 +15,9 @@ 'use strict'; const chai = require('chai'); +const sinon = require('sinon'); const expect = chai.expect; -const { - createRateController, -} = require('../../../lib/worker/rate-control/compositeRate.js'); +const {createRateController} = require('../../../lib/worker/rate-control/compositeRate.js'); const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); @@ -26,6 +25,7 @@ describe('CompositeRateController', () => { let testMessage; let CompositeRateController; let stats; + let clock; beforeEach(() => { let msgContent = { label: 'query2', @@ -115,4 +115,35 @@ describe('CompositeRateController', () => { ); }); }); + + describe('#_controllerSwitchForDuration', () => { + beforeEach(() => { + CompositeRateController.controllers = [ + { isLast: false, relFinishTime: 100, txStatSubCollector: { deactivate: () => {}, activate: () => {} }, controller: { end: async () => {} } }, + { isLast: true, relFinishTime: 200, txStatSubCollector: { deactivate: () => {}, activate: () => {} }, controller: { end: async () => {} } } + ]; + CompositeRateController.activeControllerIndex = 0; + CompositeRateController.stats = { + getRoundStartTime: () => 1000, + getTotalSubmittedTx: () => 10, + }; + stats = new TransactionStatisticsCollector(0, 0, 'query2'); + clock = sinon.useFakeTimers(); + }); + + afterEach(() => { + clock.restore(); + }); + it('should not switch if current controller is last', async () => { + CompositeRateController.activeControllerIndex = 1; + await CompositeRateController._controllerSwitchForDuration(); + expect(CompositeRateController.activeControllerIndex).to.equal(1); + }); + + it('should not switch if it is not time yet', async () => { + global.Date.now = () => 100; + await CompositeRateController._controllerSwitchForDuration(); + expect(CompositeRateController.activeControllerIndex).to.equal(0); + }); + }); }); From 4714b2b9b5e826fff6d4c8eeaf43cda558cf863a Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Tue, 21 May 2024 23:54:55 +0530 Subject: [PATCH 09/10] Temporarily removed applyRateControl for further changes Signed-off-by: Abhinav Pandey --- .../test/worker/rate-control/compositeRate.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 383b781b5..c2ed8ba11 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -21,7 +21,7 @@ const {createRateController} = require('../../../lib/worker/rate-control/composi const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); -describe('CompositeRateController', () => { +describe('CompositeRateController implementation', () => { let testMessage; let CompositeRateController; let stats; @@ -57,13 +57,6 @@ describe('CompositeRateController', () => { }); }); - describe('applyRateControl', () => { - it('should apply rate control correctly', async () => { - expect(() => CompositeRateController.applyRateControl()).be.a( - 'function' - ); - }); - }); describe('#_prepareControllers', () => { it('should throw error when weights and rateControllers are not arrays', async () => { From 40fa67555db6d8697bf72525c873170380e97469 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 29 May 2024 08:14:24 +0530 Subject: [PATCH 10/10] Update beforeEach Calls Signed-off-by: Abhinav Pandey --- .../caliper-core/test/worker/rate-control/compositeRate.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/caliper-core/test/worker/rate-control/compositeRate.js b/packages/caliper-core/test/worker/rate-control/compositeRate.js index 383b781b5..faa40bd52 100644 --- a/packages/caliper-core/test/worker/rate-control/compositeRate.js +++ b/packages/caliper-core/test/worker/rate-control/compositeRate.js @@ -17,6 +17,7 @@ const chai = require('chai'); const sinon = require('sinon'); const expect = chai.expect; +// const CompositeRate = rewire('../../../lib/worker/rate-control/compositeRate.js'); const {createRateController} = require('../../../lib/worker/rate-control/compositeRate.js'); const TransactionStatisticsCollector = require('../../../lib/common/core/transaction-statistics-collector'); const TestMessage = require('../../../lib/common/messages/testMessage.js'); @@ -47,11 +48,11 @@ describe('CompositeRateController', () => { }; testMessage = new TestMessage('test', [], msgContent); stats = new TransactionStatisticsCollector(0, 0, 'query2'); - CompositeRateController = createRateController(testMessage, stats, 0); }); describe('#constructor', () => { it('should correctly initialize with default settings', () => { + CompositeRateController = createRateController(testMessage, stats, 0); expect(CompositeRateController.activeControllerIndex).to.equal(0); expect(CompositeRateController.controllers.length).to.equal(1); });