Skip to content

Commit

Permalink
Add install size benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Sep 17, 2023
1 parent 532ca44 commit db25c94
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
4 changes: 3 additions & 1 deletion bench/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ function truncate (num) {
return `${bits[0]}.${bits[1].substring(0, 2)}`
}

let roundHalf = num => Math.round(num * 2) / 2

function formatSize (num) {
// Measure in KB
if (num < MB) return `${truncate(num / KB)} KB`
Expand All @@ -20,4 +22,4 @@ let names = {
'aws-sdk-v3': 'AWS SDK v3',
}

module.exports = { formatSize, names }
module.exports = { formatSize, names, roundHalf }
93 changes: 78 additions & 15 deletions bench/benchmark-size.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,97 @@
#! /usr/bin/env node
let { mkdirSync, statSync } = require('fs')
let { mkdirSync, rmSync, statSync, writeFileSync } = require('fs')
let { join } = require('path')
let { execSync } = require('child_process')
let { build } = require('esbuild')
let folderSize = require('fast-folder-size/sync')

// eslint-disable-next-line
let { formatSize, names } = require('./_helpers')
let { formatSize, names, roundHalf } = require('./_helpers')

let installCommands = {
'aws-lite': '@aws-lite/client', // TODO: install '@aws-lite/dynamodb' when ready!
'aws-sdk-v2': 'aws-sdk',
'aws-sdk-v2-client': null,
'aws-sdk-v3': '@aws-sdk/client-dynamodb',
}

let awsLiteUnbundledSize
let awsLiteBundledSize
let v2installTime
let v2size

async function main () {
// TODO: run unbundled size on disk benchmarks
console.log(`---------- Running bundle size benchmarks ---------- `)
let entryFileFolder = join(__dirname, 'entry-files')
for (let entry of Object.keys(names)) {
let name = entry.split('.js')[0]
let friendlyName = names[name]
console.log(`[${friendlyName}]`)
for (let [ name, friendly ] of Object.entries(names)) {
let isAWSLite = name === 'aws-lite'
let time, unbundledSize, bundledSize

console.log(`[${friendly}]`)

/**
* Install
*/
if (name !== 'aws-sdk-v2-client') {
let startInstall = Date.now()
let installDir = join(__dirname, 'tmp', name + '-install')
rmSync(installDir, { recursive: true, force: true })
mkdirSync(installDir, { recursive: true })

// Stub in a package.json
writeFileSync(join(installDir, 'package.json'), '{}')

console.log(`Installing ${installCommands[name]}...`)
let cmd = `npm i --omit=dev ${installCommands[name]}`
try {
execSync(cmd, { cwd: installDir, stdio: [] })
}
catch (err) {
console.log(`Installation error`, err)
}

unbundledSize = folderSize(join(installDir, 'node_modules'))
time = Date.now() - startInstall

let outDir = join(__dirname, 'tmp', name)
mkdirSync(outDir, { recursive: true })
let outfile = join(outDir, entry)
if (isAWSLite) {
awsLiteUnbundledSize = unbundledSize
}
if (name === 'aws-sdk-v2') {
v2size = unbundledSize
}
console.log(`- Installation time: ${time} ms`)
}
else {
unbundledSize = v2size
console.log(`- Installation time: ${v2installTime} ms`)
}
let larger = ''
if (!isAWSLite) larger = ` (${roundHalf(unbundledSize / awsLiteUnbundledSize)}x larger)`
console.log(`- Unbundled size: ${formatSize(unbundledSize)}${larger}`)

let start = Date.now()
/**
* Bundle
*/
let bundleDir = join(__dirname, 'tmp', name + '-bundle')
mkdirSync(bundleDir, { recursive: true })
let outfile = join(bundleDir, `${name}-bundle.js`)
await build({
entryPoints: [ join(entryFileFolder, entry) ],
entryPoints: [ join(entryFileFolder, `${name}.js`) ],
bundle: true,
platform: 'node',
format: 'cjs',
outfile,
})
console.log(`esbuild completed bundling in ${Date.now() - start} ms`)
let { size } = statSync(outfile)
console.log(`Bundle size: ${formatSize(size)}`)
let stat = statSync(outfile)
bundledSize = stat.size
if (isAWSLite) {
awsLiteBundledSize = bundledSize
larger = ''
}
else {
larger = ` (${roundHalf(bundledSize / awsLiteBundledSize)}x larger)`
}
console.log(`- Bundled size: ${formatSize(bundledSize)}${larger}`)
console.log('')
}
}
Expand Down
1 change: 1 addition & 0 deletions bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@aws-sdk/client-dynamodb": "^3.413.0",
"aws-sdk": "^2.1458.0",
"esbuild": "^0.19.0",
"fast-folder-size": "^2.2.0",
"percentile": "^1.6.0"
}
}

0 comments on commit db25c94

Please sign in to comment.