chore(root): drop stresser

This commit is contained in:
Ben Allfree 2024-06-01 09:57:35 -04:00
parent 7c5e1a0d75
commit 184b883c94
6 changed files with 0 additions and 310 deletions

View File

@ -1,40 +0,0 @@
import { MothershipAdminClientService } from '$services'
import {
INSTANCE_COLLECTION,
InstanceFields,
singletonAsyncExecutionGuard,
} from '$shared'
import Bottleneck from 'bottleneck'
export const deleteInstance = singletonAsyncExecutionGuard(
async (instance: InstanceFields) => {
const { client } = await MothershipAdminClientService()
const { id } = instance
await client.client
.collection(INSTANCE_COLLECTION)
.delete(id)
.catch((e) => {
console.error(`deelte instance erorr`, JSON.stringify(e, null, 2))
throw e
})
console.log(`Instance deleted ${id}`)
},
(instance) => `deleteInstance:${instance.id}`,
)
export const deleteInstancesByFilter = singletonAsyncExecutionGuard(
async (filter: string) => {
const { client } = await MothershipAdminClientService()
const instances = await client.client
.collection(INSTANCE_COLLECTION)
.getFullList<InstanceFields>(0, { filter })
const limiter = new Bottleneck({ maxConcurrent: 50 })
await Promise.all(
instances.map((instance) =>
limiter.schedule(() => deleteInstance(instance)),
),
)
},
(filter) => `deleteInstancesByFilter:${filter}`,
)

View File

@ -1,44 +0,0 @@
import {
MOTHERSHIP_ADMIN_PASSWORD,
MOTHERSHIP_ADMIN_USERNAME,
} from '$constants'
import { MothershipAdminClientService } from '$services'
import { ContextBase, GlobalOptions } from '$src/stresser/types'
import { Command } from 'commander'
import { deleteInstancesByFilter } from './deleteInstance'
export type CleanupOptions = GlobalOptions & {
filter: string
}
export const createCleanup = (context: { program: Command } & ContextBase) => {
const { program } = context
const logger = context.logger.create(`createCleanup`)
const cleanupCmd = program.command('cleanup')
cleanupCmd
.description('Clean up stress artifacts from database')
.option(
`-f, --filter <filter>`,
`Filter to use when deleting instances`,
`stress-%`,
)
.action(async () => {
const options = cleanupCmd.optsWithGlobals<CleanupOptions>()
const { filter } = options
const { dbg } = logger
dbg({ options })
if (!filter.startsWith(`stress-`)) {
throw new Error(`Cleanup filter must begin with 'stress-'`)
}
await MothershipAdminClientService({
url: options.mothershipUrl,
username: MOTHERSHIP_ADMIN_USERNAME(),
password: MOTHERSHIP_ADMIN_PASSWORD(),
})
await deleteInstancesByFilter(`subdomain ~ '${filter}'`)
})
}

View File

@ -1,58 +0,0 @@
import {
MOTHERSHIP_ADMIN_PASSWORD,
MOTHERSHIP_ADMIN_USERNAME,
} from '$constants'
import { MothershipAdminClientService } from '$services'
import { InstanceStatus, serialAsyncExecutionGuard } from '$shared'
import { random, range, shuffle } from '@s-libs/micro-dash'
import { Command } from 'commander'
import { customAlphabet } from 'nanoid'
import { ContextBase, GlobalOptions } from '../types'
const nanoid = customAlphabet(`abcdefghijklmnop`)
const _unsafe_createInstance = async (context: ContextBase) => {
const logger = context.logger.create(`createInstance`)
const { dbg } = logger
const { client } = await MothershipAdminClientService()
const users = await client.client.collection('users').getFullList()
await client.createInstance({
subdomain: `stress-${nanoid()}`,
uid: shuffle(users).pop()!.id,
status: InstanceStatus.Idle,
version: `~0.${random(1, 16)}.0`,
secrets: {},
maintenance: false,
})
}
const createInstance = serialAsyncExecutionGuard(_unsafe_createInstance)
export type SeedOptions = GlobalOptions & { count: number }
export const createSeed = (context: { program: Command } & ContextBase) => {
const { program } = context
const logger = context.logger.create(`createSeed`)
const seedCmd = program.command('seed')
seedCmd
.description('Seed system with new instances')
.option(
`-c, --count`,
`Number of new seed instances to create`,
parseInt,
10,
)
.action(async () => {
const options = seedCmd.optsWithGlobals<SeedOptions>()
const { client } = await MothershipAdminClientService({
url: options.mothershipUrl,
username: MOTHERSHIP_ADMIN_USERNAME(),
password: MOTHERSHIP_ADMIN_PASSWORD(),
})
/** Create instances */
await Promise.all(range(10).map(() => createInstance({ logger })))
})
}

View File

@ -1,127 +0,0 @@
import {
MOTHERSHIP_ADMIN_PASSWORD,
MOTHERSHIP_ADMIN_USERNAME,
} from '$constants'
import { MothershipAdminClientService } from '$services'
import { InstanceId, serialAsyncExecutionGuard } from '$shared'
import { random, range, shuffle, values } from '@s-libs/micro-dash'
import { Command } from 'commander'
import fetch from 'node-fetch'
import { ContextBase, GlobalOptions } from '../types'
export type StressOptions = GlobalOptions & {
instanceCount: number
requestsPerInstance: number
minDelay: number
maxDelay: number
}
export const createStress = (context: { program: Command } & ContextBase) => {
const { program } = context
const logger = context.logger.create(`createStress`)
const { dbg, error } = logger
const seedCmd = program.command('stress')
seedCmd
.description('Seed system with new instances')
.description('Stress the system')
.option(
'-ic, --instance-count <number>',
`Number of simultaneous instances to hit`,
parseInt,
100,
)
.option(
'-rc, --requests-per-instance <number>',
`Number of simultaneous requests per instance`,
parseInt,
50,
)
.option(
'-mind, --min-delay <number>',
`Minimum number of milliseconds to delay before sending another request`,
parseInt,
50,
)
.option(
'-maxd, --max-delay <number>',
`Maximum number of milliseconds to delay before sending another request`,
parseInt,
500,
)
.action(async () => {
const options = seedCmd.optsWithGlobals<StressOptions>()
const { client } = await MothershipAdminClientService({
url: options.mothershipUrl,
username: MOTHERSHIP_ADMIN_USERNAME(),
password: MOTHERSHIP_ADMIN_PASSWORD(),
})
const users = await client.client.collection('users').getFullList()
dbg(users)
const { instanceCount, requestsPerInstance, minDelay, maxDelay } = options
const excluded: { [_: string]: boolean } = {}
const resetInstance = serialAsyncExecutionGuard(
async (instanceId: InstanceId) => {
if (excluded[instanceId]) return
await client.updateInstance(instanceId, { maintenance: false })
},
(id) => `reset:${id}`,
)
const instances = await client.getInstances()
dbg(`Instances ${instances.length}`)
/** Stress test */
const stress = async () => {
try {
const instance = shuffle(instances)
.filter((v) => !excluded[v.id])
.pop()
dbg(
`There are ${instances.length} instances and ${
values(excluded).length
} excluded`,
)
if (!instance) throw new Error(`No instance to grab`)
{
const { subdomain, id } = instance
await resetInstance(id)
const thisLogger = logger.create(subdomain)
thisLogger.breadcrumb(id)
await Promise.all(
range(requestsPerInstance).map(async (i) => {
const requestLogger = thisLogger.create(`${i}`)
const { dbg } = requestLogger
const url = `https://${subdomain}.pockethost.test/_`
dbg(`Fetching ${url}`)
const res = await fetch(url)
if (res.status !== 200) {
const body = res.body?.read().toString()
dbg(`${url} response error ${res.status} ${body}`)
if (body?.match(/maintenance/i)) {
dbg(`Maintenance mode detected. Excluding`)
excluded[id] = true
}
if (res.status === 403 && !!body?.match(/Timeout/)) {
return // Timeout
}
}
}),
)
}
} catch (e) {
error(`failed with: ${e}`, e)
} finally {
setTimeout(stress, random(minDelay, maxDelay))
}
}
range(Math.min(instances.length, instanceCount)).forEach(() => {
stress()
})
})
}

View File

@ -1,33 +0,0 @@
import { LoggerService } from '$shared'
import { LogLevelName } from '$shared'
import { Command } from 'commander'
import { createCleanup } from './commands/cleanup'
import { createSeed } from './commands/seed'
import { createStress } from './commands/stress'
const program = new Command()
LoggerService({ level: LogLevelName.Debug })
const logger = LoggerService().create(`stresser`)
const { dbg, error, info, warn } = logger
// npm install eventsource --save
//@ts-ignore
global.EventSource = require('eventsource')
program
.name('stresser')
.description('CLI tool to stress the hell out of PocketBase')
.version('0.1.0')
.option(
'-u, --mothership-url',
'URL to central database',
'http://127.0.0.1:8090',
)
createCleanup({ program, logger })
createStress({ program, logger })
createSeed({ program, logger })
program.parse()

View File

@ -1,8 +0,0 @@
import { Logger } from '$shared'
export type ContextBase = {
logger: Logger
}
export type GlobalOptions = {
mothershipUrl: string
}