Skip to content

Instantly share code, notes, and snippets.

@sbrichardson
Created December 1, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrichardson/694cb62df2f076aac5af600a45662567 to your computer and use it in GitHub Desktop.
Save sbrichardson/694cb62df2f076aac5af600a45662567 to your computer and use it in GitHub Desktop.
RLog Benchmark Class
class RLog {
contructor(title) {
this.kind = 'Method Log'
this.name = title
this.book = {
new: true,
errors: [],
successes: [],
errorCount: 0,
successCount: 0,
start: null,
finish: null,
benchStart: null,
benchFinish: null,
duration: null
}
}
}
RLog.prototype.log = (type, entry) => {
check(type, String)
check(entry, Match.OneOf(String, Date, Number, Boolean, Object, Array))
if (this.book.new) {
this.book.benchStart = process.hrtime()
this.book.new = false
this.book.start = new Date()
}
if (type === "err") {
this.book.errors.push(entry)
this.book.errorCount++
}
if (type === "good") {
this.book.successes.push(entry)
this.book.successCount++
}
return
}
RLog.prototype.start = () => {
this.book.benchStart = process.hrtime()
this.book.new = false
this.book.start = new Date()
return
}
RLog.prototype.finish = () => {
this.book.benchFinish = process.hrtime(this.book.benchStart)
this.book.finish = new Date()
if (this.book.benchFinish) {
this.book.duration = ((this.book.benchFinish[0] || 0) + ' seconds and ' + ((this.book.benchFinish[1] || 0)/1000000) + ' ms') || "Benchmark Error"
}
return
}
RLog.prototype.duration = () => {
if(this.book.benchFinish) {
console.info('\n** Benchmark Results **\nName: '+ this.name + '\nTime: ' + (this.book.benchFinish[0] || 0) + ' seconds and ' + ((this.book.benchFinish[1] || 0)/1000000) + ' ms')
}
return
}
RLog.prototype.print = () => {
console.info('\n** Current LogBook **\nName: ' + this.name + '\nLog:\n' + util.inspect(this.book, {depth: null, colors: true}))
return
}
RLog.prototype.rError = ops => {
this.book.name = ops.name
this.book.type = ops.type
this.book.message = ops.message
this.book.detail = ops.detail
this.book.extendedInfo = ops.extendedInfo
this.book.errorCode = ops.errorCode
}
@sbrichardson
Copy link
Author

sbrichardson commented Dec 1, 2017

Older Version that included a command line chart output:

screen shot 2016-09-15 at 1 34 17 am

usage

//make a new book/b class
const b = new _R.book('testname');

//put splits wherever after functions etc, 
b.split('name of function/short note')

when done do b.benchReport(); it will log out it's graph/times etc

import _R from './_R.js';
import util from 'util';
import Chart from 'cli-chart';

_R.Book = class {
  constructor(title) {
    this.kind = 'Method Log';
    this.name = title;
    this.book = {
      new: true,
      errors: [],
      successes: [],
      errorCount: 0,
      successCount: 0,
      duration: this.getDuration()
    };
    this.bench = {
      start: null,
      finish: null,
      benchStart: process.hrtime(),
      benchFinish: null,
      lastDuration: null
    };
    this.splits = [{name: 'initial', split: process.hrtime(), prevCalc: null}];
  }
};
_R.Book.prototype.log = function(type, entry) {
  check(type, String);
  check(entry, Match.OneOf(String, Date, Number, Boolean, Object, Array));
  if (this.book.new) {
    this.start();
  }
  let entryObj = {
    split: this.getSplit(),
    duration: this.getDuration(),
    entry: entry
  };
  if (type === "err") {
    this.book.errors.push(entryObj);
    this.book.errorCount++;
  }
  if (type === "good") {
    this.book.successes.push(entryObj);
    this.book.successCount++;
  }
  this.setSplit();
  return;
};
_R.Book.prototype.getDuration = function() {
  try {
    let currentTime = process.hrtime(this.bench.benchStart);
    return (currentTime[0] || 0) + ' seconds and ' + (((currentTime[1] || 0)/1000000) + ' ms') || "Benchmark Error";
  }
  catch(e) {
    return e;
  }
};
_R.Book.prototype.split = function(name) {
  this.splits.push({
    name: name,
    split: process.hrtime(),
    prevCalc: process.hrtime(this.splits[ this.splits.length - 1 ].split)
  });
};

_R.Book.prototype.setSplit = function() {
  this.bench.split = process.hrtime();
};

_R.Book.prototype.getSplit = function() {
  try {
    if (!this.bench.split) {
      throw new Error("Previous split not available");
    }
    const split = process.hrtime(this.bench.split);
    const splitInfo = (split[0] || 0) + ' seconds and ' + (((split[1] || 0)/1000000) + ' ms') || "Benchmark Error";
    return splitInfo;
  }
  catch(e) {
    return e;
  }
};
_R.Book.prototype.benchReport = function() {
  const chart = new Chart({
      xlabel: 'splits',
      ylabel: 'split time',
      direction: 'y',
      width: 90,
      height: 20,
      lmargin: 10,
      step: 3
  });
  let fastest = null, slowest = null;

  lo_.each(this.splits, x => {
    if (x.prevCalc) {
      const info = (x.prevCalc[0] || 0) + ' secs, ' + (((x.prevCalc[1] || 0)/1000000) + ' ms') || "bench err"
      let nano = x.prevCalc[0] * 1e9 + x.prevCalc[1];
      console.log(`${x.name}:  ${info}`);
      if (fastest === null) {
        fastest = {time: nano, name: x.name};
      } else if (nano < fastest.time) {
        fastest.time = nano;
        fastest.name = x.name;
        fastest.info = info;
      }
      if (slowest === null) {
        slowest ={time: nano, name: x.name};
      } else if (nano > slowest.time) {
        slowest.time = nano;
        slowest.name = x.name;
        slowest.info = info;
      }
      // add bars
      chart.addBar(nano, 'red');
    }
  });
  console.log('\n\n*********\n')
  console.log(`slowest split:  ${slowest.name}  | ${slowest.info}`);
  console.log(`fastest split:  ${fastest.name}  | ${fastest.info}`);
  console.log('\n*********\n\n')
  chart.draw(); 

}
_R.Book.prototype.start = function() {
  let time = process.hrtime();
  this.bench.benchStart = time,
  this.bench.split = time,
  this.book.new = false;
  this.bench.start = new Date();
  return;
};
_R.Book.prototype.finish = function() {
  this.bench.benchFinish = process.hrtime(this.bench.benchStart);
  this.bench.finish = new Date();
  if (this.bench.benchFinish) {
    this.book.duration = ((this.bench.benchFinish[0] || 0) + ' seconds and ' + ((this.bench.benchFinish[1] || 0)/1000000) + ' ms') || "Benchmark Error";
  }
  return;
};
_R.Book.prototype.print = function(options) {
  this.book.duration = this.getDuration();
  console.log('\n** Current LogBook **\nName: ' + this.name + '\nLog:\n' + util.inspect(this.book, {depth: null, colors: false}));
  return;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment