<commit_before>gulp = require 'gulp'
coffee = require 'gulp-coffee'
gutil = require 'gulp-util'
concat = require 'gulp-concat'
gulpif = require 'gulp-if'
glob = require 'glob'
async = require 'async'
espruino = require '../../gulp-espruino/src/gulp-espruino'
Table = require 'cli-table'

howMuchMemory = (sourceFile, callback) ->
  gulp.src(['./src/main/lib/almond-*.js',
            './src/performance/memory/before.coffee',
            sourceFile,
            './src/performance/memory/after.coffee'])
      .pipe gulpif(/[.]coffee/, coffee(bare: true).on('error', gutil.log))
      .pipe concat('all.js')
      .pipe gulp.dest('build')
      .pipe espruino.deploy(
        serialNumber: '48DF67773330'
        echoOn: true
        capture:
          input: true
          output: false
        fakePath: '../Espruino/espruino')
      .on 'data', (data) ->
        matches = /memory used: (.*)/.exec(data.contents.toString())

        if matches && matches.length > 1
          callback(null, sourceFile: sourceFile, memoryUsage: parseInt(matches[1]))
        else
          callback("failed to determine memory for #{sourceFile}")

printResults = (results) ->
  table = new Table(head: ['Source File', 'Memory Usage (blocks)'], colWidths: [50, 25])
  total = 0
  for result in results
    table.push([result.sourceFile, result.memoryUsage])
    total += result.memoryUsage

  table.push(['Total', total])

  gutil.log(table.toString())


module.exports = ->
  files = glob.sync('./src/main/**/*.coffee')

  async.mapSeries files, howMuchMemory, (error, results) ->
    if error
      gutil.log("failed!", error)
    else
      printResults(results)<commit_msg>print the memory usage for the AMD loader as well as the source files<commit_after>gulp = require 'gulp'
coffee = require 'gulp-coffee'
gutil = require 'gulp-util'
concat = require 'gulp-concat'
gulpif = require 'gulp-if'
glob = require 'glob'
async = require 'async'
espruino = require '../../gulp-espruino/src/gulp-espruino'
Table = require 'cli-table'

howMuchMemory = (sourceFile, sourceFiles, callback) ->
  gulp.src(sourceFiles)
      .pipe gulpif(/[.]coffee/, coffee(bare: true).on('error', gutil.log))
      .pipe concat('performance.js')
      .pipe gulp.dest('build')
      .pipe espruino.deploy(
        serialNumber: '48DF67773330'
        echoOn: true
        capture:
          input: true
          output: false
        fakePath: '../Espruino/espruino')
      .on 'data', (data) ->
        matches = /memory used: (.*)/.exec(data.contents.toString())

        if matches && matches.length > 1
          callback(null, sourceFile: sourceFile, memoryUsage: parseInt(matches[1]))
        else
          callback("failed to determine memory for #{sourceFile}")

howMuchMemoryForAMD = (callback) ->
  howMuchMemory('./src/main/lib/almond-*.js',
                ['./src/performance/memory/before.coffee',
                 './src/main/lib/almond-*.js',
                 './src/performance/memory/after.coffee'],
                callback)

howMuchMemoryForFile = (sourceFile, callback) ->
  howMuchMemory(sourceFile,
                ['./src/main/lib/almond-*.js',
                 './src/performance/memory/before.coffee',
                 sourceFile,
                 './src/performance/memory/after.coffee'],
                callback)

formatResults = (results) ->
  table = new Table(head: ['Source File', 'Memory Usage (blocks)'], colWidths: [50, 25])
  total = 0
  for result in results
    table.push([result.sourceFile, result.memoryUsage])
    total += result.memoryUsage

  table.push(['Total', total])
  table.toString()

module.exports = ->
  files = glob.sync('./src/main/**/*.coffee')

  howMuchMemoryForAMD (amdError, amdResults) ->
    if amdError
      gutil.log "error!", amdError
    else
      async.mapSeries files, howMuchMemoryForFile, (filesError, fileResults) ->
        if filesError
          gutil.log "error!", filesError
        else
          console.log formatResults(fileResults.concat([amdResults]))
{'fraction_non_alphanumeric': 0.14913814639020734, 'fraction_numerical': 0.00849362977766675, 'mean_word_length': 2.633393829401089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6317447', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1228, 'n_words': 275}