Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
fix curl and nginx to handle large messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylarizi committed Feb 4, 2024
1 parent c67513e commit b73ad10
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 47 deletions.
16 changes: 14 additions & 2 deletions curl/scripts/run-curl-loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ nginx_host="$1"
nginx_port="$2"
iteration_count="$3"
algorithm="$4"
payload="$5"
message_size="$5"
num_processes=$(($(getconf _NPROCESSORS_ONLN) * 2))

seq ${iteration_count} | xargs -P $num_processes -n 1 -I % curl https://${nginx_host}:${nginx_port} -k --curves ${algorithm} -XPOST -d "$payload" -H "Content-Type: text/plain" -o /dev/null
# Generate random message with specified size
generate_message() {
local size="$1"
tr -dc '[:print:]' </dev/urandom | head -c "$size" > /tmp/message.txt
}

# Generate the message
generate_message "$message_size"

# Perform curl requests using xargs
#seq "$iteration_count" | xargs -P "$num_processes" -n 1 -I % sh -c "curl 'https://${nginx_host}:${nginx_port}' -k --curves '${algorithm}' -XPOST -d @/tmp/message.txt -H 'Content-Type: text/plain' -o /dev/null"
#seq ${iteration_count} | xargs -P $num_processes -n 1 -I % curl https://${nginx_host}:${nginx_port} -k --curves ${algorithm} -XPOST -d "$payload" -H "Content-Type: text/plain" -o /dev/null
seq "$iteration_count" | xargs -P "$num_processes" -n 1 -I % curl "https://${nginx_host}:${nginx_port}" -k --curves "${algorithm}" -XPOST -d "@/tmp/message.txt" -H "Content-Type: text/plain" -o /dev/null
9 changes: 4 additions & 5 deletions curl/src/curl/curl.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ConfigModule, ConfigService } from '@nestjs/config';

import { HttpException, HttpStatus } from '@nestjs/common';
import * as shellJS from 'shelljs';
import {MessageGenerator} from "../utils/message.generator";
jest.mock('shelljs', () => ({
exec: jest.fn(),
}));
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('CurlService', () => {
const runCurlsSpy = jest.spyOn<any, any>(curlService, 'runCurls').mockResolvedValue(undefined);
await curlService.run(curlRequest);
expect(validateSpy).toHaveBeenCalledWith(curlRequest);
expect(runCurlsSpy).toHaveBeenCalledWith(curlRequest.iterationsCount, curlRequest.algorithm, expect.any(String));
expect(runCurlsSpy).toHaveBeenCalledWith(curlRequest.iterationsCount, curlRequest.algorithm, curlRequest.messageSize);
});

it('should throw an HttpException with status INTERNAL_SERVER_ERROR when runCurls throws an error', async () => {
Expand Down Expand Up @@ -124,10 +123,10 @@ describe('CurlService', () => {
it('should call execAsync with the correct command', async () => {
const iterationsCount = 1000;
const algorithm = 'kyber512';
const message = MessageGenerator.generate(8);
const messageSize = 1024;
const execAsyncSpy = jest.spyOn<any, any>(curlService, 'execAsync').mockResolvedValue(undefined);
await curlService['runCurls'](iterationsCount, algorithm, message);
const expectedCommand = curlService['format'](`./scripts/run-curl-loop.sh ${configService.get('nginx.host')} ${configService.get('nginx.port')} ${iterationsCount} ${algorithm} ${message}`);
await curlService['runCurls'](iterationsCount, algorithm, messageSize);
const expectedCommand = curlService['format'](`./scripts/run-curl-loop.sh ${configService.get('nginx.host')} ${configService.get('nginx.port')} ${iterationsCount} ${algorithm} ${messageSize}`);
expect(execAsyncSpy).toHaveBeenCalledWith(expectedCommand);
});
// Add more test cases for error handling in runCurls.
Expand Down
8 changes: 3 additions & 5 deletions curl/src/curl/curl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import * as shellJS from 'shelljs';
import { CurlRequest } from '../dto/curl-request.dto';
import { ConfigService } from '@nestjs/config';
import {MessageGenerator} from '../utils/message.generator';

@Injectable()
export class CurlService {
Expand All @@ -20,8 +19,7 @@ export class CurlService {
async run(curlRequest: CurlRequest): Promise<void> {
this.validate(curlRequest);
try {
const message = MessageGenerator.generate(curlRequest.messageSize);
await this.runCurls(curlRequest.iterationsCount, curlRequest.algorithm, message);
await this.runCurls(curlRequest.iterationsCount, curlRequest.algorithm, curlRequest.messageSize);
} catch (err) {
this.processIsRunning = false;
console.error('[CurlService:run] Error occurred: ', err);
Expand All @@ -40,8 +38,8 @@ export class CurlService {
}
}

private async runCurls(iterationsCount: number, algorithm: string, message: string) {
const curlCommand = this.format(`${this.CURL_SCRIPT_PATH} ${this.configService.get('nginx.host')} ${this.configService.get('nginx.port')} ${iterationsCount} ${algorithm} ${message}`);
private async runCurls(iterationsCount: number, algorithm: string, messageSize: number) {
const curlCommand = this.format(`${this.CURL_SCRIPT_PATH} ${this.configService.get('nginx.host')} ${this.configService.get('nginx.port')} ${iterationsCount} ${algorithm} ${messageSize}`);
this.processIsRunning = true;
await this.execAsync(curlCommand);
console.log('[CurlService:run] Finished taking all curl samples');
Expand Down
26 changes: 0 additions & 26 deletions curl/src/utils/message.generator.spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions curl/src/utils/message.generator.ts

This file was deleted.

1 change: 1 addition & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ http {

keepalive_timeout 65;

client_max_body_size 0; # Allow unlimited size for the entire request body

server {
listen 8080;
Expand Down

0 comments on commit b73ad10

Please sign in to comment.