From f794c7415499f8581f6f16f176a08fff13edaad9 Mon Sep 17 00:00:00 2001 From: Jason Hyde Date: Wed, 14 Aug 2024 03:02:40 +0200 Subject: [PATCH] [ts] add utility function to convert sizes from human format --- source/server.ts | 9 +++++---- source/utils/parseSize.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 source/utils/parseSize.ts diff --git a/source/server.ts b/source/server.ts index 313f818..36eb2c9 100644 --- a/source/server.ts +++ b/source/server.ts @@ -2,6 +2,7 @@ import fastify from 'fastify' import fs from 'node:fs/promises' import os from 'node:os' import path from 'node:path' +import { parseSize } from './utils/parseSize.ts' const PORT = 3000 const MAX_DELAY = 5000 @@ -87,7 +88,7 @@ server.get('/memory', async () => { // Route 5: Create a memory leak server.get<{ Querystring: { size: string; count: string } }>('/memory/leak', async (request) => { - const size = Number.parseInt(request.query.size, 10) || 1024 * 1024 // Default to 1MB + const size = parseSize(request.query.size || '1MB') const count = Number.parseInt(request.query.count, 10) || 1 for (let i = 0; i < count; i++) { @@ -100,7 +101,7 @@ server.get<{ Querystring: { size: string; count: string } }>('/memory/leak', asy // Route 6: Allocate memory server.get<{ Querystring: { size: string } }>('/memory/allocate', async (request) => { - const size = Number.parseInt(request.query.size, 10) || 1024 * 1024 // Default to 1MB + const size = parseSize(request.query.size || '1MB') const buffer = Buffer.alloc(size) @@ -112,7 +113,7 @@ server.get<{ Querystring: { size: string } }>('/memory/allocate', async (request // Route 7: Simulate file read operation server.get<{ Querystring: { size: string } }>('/io/read', async (request) => { - const size = Number.parseInt(request.query.size, 10) || 1024 * 1024 // Default to 1MB + const size = parseSize(request.query.size || '1MB') const filePath = path.join(os.tmpdir(), 'temp_read_file.txt') // Create a file with random data @@ -129,7 +130,7 @@ server.get<{ Querystring: { size: string } }>('/io/read', async (request) => { // Route 8: Simulate file write operation server.get<{ Querystring: { size: string } }>('/io/write', async (request) => { - const size = Number.parseInt(request.query.size, 10) || 1024 * 1024 // Default to 1MB + const size = parseSize(request.query.size || '1MB') const filePath = path.join(os.tmpdir(), 'temp_write_file.txt') const start = Date.now() diff --git a/source/utils/parseSize.ts b/source/utils/parseSize.ts new file mode 100644 index 0000000..dd7df71 --- /dev/null +++ b/source/utils/parseSize.ts @@ -0,0 +1,32 @@ +/** + * Helper function to parse human-readable sizes + * + * @param size - The size string to parse. + * @returns The parsed size in bytes. + * @throws {Error} If the size string is invalid or cannot be parsed. + */ +export const parseSize = (size: string): number => { + const units: Record = { + b: 1, + kb: 1024, + mb: 1024 * 1024, + gb: 1024 * 1024 * 1024 + } + const match = size.toLowerCase().match(/^(\d+(?:\.\d+)?)\s*(b|kb|mb|gb)?$/) + + if (!match || match.length < 2) { + throw new Error('Invalid size format') + } + + const value = Number.parseFloat(match[1] ?? '') + if (Number.isNaN(value)) { + throw new Error('Invalid numeric value') + } + + const unit = match[2] ?? 'b' + if (!(unit in units)) { + throw new Error('Invalid unit') + } + + return Math.floor(value * units[unit]) +}