Skip to content

Commit

Permalink
[ts] add utility function to convert sizes from human format
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhyde committed Aug 14, 2024
1 parent 07b9a6c commit f794c74
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
9 changes: 5 additions & 4 deletions source/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++) {
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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()
Expand Down
32 changes: 32 additions & 0 deletions source/utils/parseSize.ts
Original file line number Diff line number Diff line change
@@ -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<string, number> = {
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])
}

0 comments on commit f794c74

Please sign in to comment.