Skip to content

Commit

Permalink
rewrote the listener functions
Browse files Browse the repository at this point in the history
  • Loading branch information
annashiheart committed Oct 25, 2024
1 parent 0eb5441 commit 7ad3e07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 72 deletions.
57 changes: 0 additions & 57 deletions iroh/iroh-2

This file was deleted.

52 changes: 37 additions & 15 deletions iroh/iroh-listener.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
'use strict';

require('iroh');
const Iroh = require('iroh');

// Asked co-pilot for a function to monitor the execution time of a function and log it to the console.
// from copilot
function monitorFunction(fn, name) {
console.log(`Setting up monitoring for ${name}`);

return async function monitored(...args) {
console.log(`[START] ${name}`);
console.log(`Setting up Iroh monitoring for ${name}`);

const stage = new Iroh.Stage(`
function monitored() {
try {
return target();
} catch(e) {
console.error(e);
throw e;
}
}
`);

// Using Iroh's listeners
stage.addListener(Iroh.CALL)
.on('before', (e) => {
console.log(`[IROH] ${name} called`);
e.setData('time', process.hrtime());
})
.on('after', (e) => {
const diff = process.hrtime(e.getData('time'));
const time = ((diff[0] * 1e9) + diff[1]) / 1e6;
console.log(`[IROH] ${name} took ${time}ms`);
});

// Return the original function with timing
return async function (...args) {
const start = process.hrtime();
console.log(`[START] ${name}`);

try {
const result = await fn.apply(this, args);
const [seconds, nanoseconds] = process.hrtime(start);
const milliseconds = (seconds * 1000) + (nanoseconds / 1000000);

console.log(`[END] ${name} took ${milliseconds.toFixed(2)}ms`);

if (milliseconds > 100) {
console.warn(`[SLOW] ${name} took ${milliseconds.toFixed(2)}ms`);
}

const [s, ns] = process.hrtime(start);
const ms = (s * 1000) + (ns / 1e6);
console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`);
console.log(`[END] ${name}`);
return result;
} catch (error) {
console.error(`[ERROR] ${name}:`, error);
Expand All @@ -31,4 +50,7 @@ function monitorFunction(fn, name) {

module.exports = {
monitorFunction: monitorFunction,
CALL: Iroh.CALL,
FUNCTION: Iroh.FUNCTION,
TRY: Iroh.TRY,
};

0 comments on commit 7ad3e07

Please sign in to comment.