Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#368): added utility function for elapsed time tracking #389

Merged
merged 20 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/commands/assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
const rel = require('relative');
const path = require('path');
const {mvnw, flags} = require('../mvnw');
const {elapsed} = require('../elapsed');

/**
* Command to assemble .XMIR files.
Expand All @@ -33,8 +34,10 @@ const {mvnw, flags} = require('../mvnw');
*/
module.exports = function(opts) {
const target = path.resolve(opts.target);
return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => {
console.info('EO program assembled in %s', rel(target));
return r;
return elapsed((tracked) => {
return mvnw(['eo:assemble'].concat(flags(opts)), opts.target, opts.batch).then((r) => {
tracked.print('EO program assembled in %s', rel(target));
return r;
});
});
};
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack this change is unnecessary

4 changes: 4 additions & 0 deletions src/commands/java/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const path = require('path');
*/
module.exports = function(opts) {
const target = path.resolve(opts.target);
/**
* @todo #368
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack it's a wrong format for todo, should be:

@todo #368:30min Description of todo...
 more description with space at the beginning

* It is necessary to use 'elapsed' in all logging cases that require output of elapsed time
*/
return mvnw(['compiler:compile'].concat(flags(opts)), opts.target, opts.batch).then((r) => {
console.info('Java .class files compiled into %s', rel(target));
return r;
Expand Down
69 changes: 69 additions & 0 deletions src/elapsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* @todo #368
* Consider if this method belong is in the right place.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack see above about todo format

* It might belong in a utility module.
* For now, it remains here.
*
* Also, review whether the test file for this method is located appropriately.
* It’s unclear if its current location is the best fit.
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack you have two comments section here stated with /** and ended with */. The first comment is on lines 25-28, the second is on 30-44 and there's an empty line 29 between them. We don't need two comments here, it should one single comment started on line 25 and ended on line 43 or 44 with no empty lines. Also please move your todo under the function description

/**
* A utility function to measure the elapsed time of a task and provide
* detailed timing information.
*
* This function wraps a given task (callback function) and provides it with
* a `tracked` object that includes a `print` method. The `print` method can
* be used within the task to log messages along with the elapsed time
* since the task started execution. The elapsed time is formatted in milliseconds,
* seconds, or minutes, based on the duration.
*
* @param {Function} task - A callback function to be measured. The function
* is invoked with a `tracked` object as an argument.
* @return {*} Result of the wrapped callback function. The result of the
* `task` callback will be returned unchanged.
*/
module.exports.elapsed = function elapsed(task) {
const startTime = Date.now();
const tracked = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack let's inline this variable into task call

return task({
  print: (message) => {
    // code here
  }
})

print: (message) => {
const duration = Date.now() - startTime;
let extended;
if (duration < 1000) {
extended = `${duration}ms`;
} else if (duration < 60 * 1000) {
extended = `${Math.ceil(duration / 1000)}s`;
} else {
extended = `${Math.ceil(duration / 3600000)}min`;
}
let msg = `${message} in ${extended}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack I think msg can be const here

console.info(msg);
return msg;
}
}
return task(tracked);
}
76 changes: 76 additions & 0 deletions test/test_elapsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const {elapsed} = require('../src/elapsed')
const assert = require("assert");

describe('elapsed', function(){
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack let's follow a single code style and wrap lambda arguments with braces:

const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

it('measures time correctly', async () => {
return elapsed(async (tracked) => {
await snooze(300);
return tracked.print("task");
}).then(
(actual)=> assert(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack space after ) is missed here

/task in 30\d+ms/.test(actual),
`Expected "${actual}" to match /task in 30\\d+ms/`
)
)
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack there's no need for space here

it('measures short time correctly', async () => {
return elapsed(async (tracked) => {
await snooze(10);
return tracked.print("short task");
}).then(
(actual) => assert(
/short task in 1\d+ms/.test(actual),
`Expected "${actual}" to match /short task in 1\\d+ms/`
)
);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack there's no need for space here

it('measures long time correctly', async () => {
return elapsed(async (tracked) => {
await snooze(1200);
return tracked.print("long task");
}).then(
(actual) => assert(
/long task in 2s/.test(actual),
`Expected "${actual}" to match /long task in 2s/`
)
);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack there's no need for space here

it('handles errors in task correctly', async () => {
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack I think the test can be simplified with assert.throws()

await elapsed(async (tracked) => {
throw new Error("task error");
});
assert.fail("Expected an error to be thrown");
} catch (error) {
assert.equal(error.message, "task error");
}
});
})
Loading