Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve distribution #47

Merged
merged 7 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions benchmark/timeDistribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { main } from '../src/alg/TimeDistribution';
import { getItems } from '../src/data/Items';
import { getPolls } from '../src/data/Polls';
import { getProject } from '../src/data/Projects';
import { getStudents } from '../src/data/Students';
import * as fs from 'fs';

interface BenchmarkResults {
algorithm: string;
version: string;
numIterations: number;
executionTimes: number[];
middleValue: number;
timestamp: string;
}

const project = getProject();
const projectId = project._id;
const items = getItems(projectId);
const students = getStudents(projectId);
const polls = getPolls(projectId);

// Benchmark settings
const numIterations = 1000;
const executionTimes: number[] = [];

// Run the function 1000 times and record execution times
for (let i = 0; i < numIterations; i++) {
const startTime = performance.now();
main(items, students, project, polls);
const endTime = performance.now();
const executionTime = endTime - startTime;
executionTimes.push(executionTime);
}

// Sort the execution times in ascending order
executionTimes.sort((a, b) => a - b);

// Calculate the middle value (median)
let middleValue: number;

if (numIterations % 2 === 0) {
// If the number of iterations is even, take the average of the two middle values
const middleIndex1 = numIterations / 2 - 1;
const middleIndex2 = numIterations / 2;
middleValue =
(executionTimes[middleIndex1] + executionTimes[middleIndex2]) / 2;
} else {
// If the number of iterations is odd, take the middle value directly
const middleIndex = Math.floor(numIterations / 2);
middleValue = executionTimes[middleIndex];
}

const benchmarkResults: BenchmarkResults = {
algorithm:
'TimeDistribution impproved distribution proccess + small per updates',
version: 'v3.1',
numIterations,
executionTimes,
middleValue,
timestamp: new Date().toISOString(),
};
const resultsFilePath: string = 'results.json';
let existingResultsArray: BenchmarkResults[] = [];

if (fs.existsSync(resultsFilePath)) {
try {
const existingResults = fs.readFileSync(resultsFilePath, 'utf-8');
existingResultsArray = JSON.parse(existingResults);
if (!Array.isArray(existingResultsArray)) {
existingResultsArray = [];
}
} catch (error) {
console.error('Error parsing existing results:', error);
existingResultsArray = [];
}
}

existingResultsArray.push(benchmarkResults);

fs.writeFileSync(
resultsFilePath,
JSON.stringify(existingResultsArray, null, 2)
);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "tsc --build",
"clean": "tsc --build --clean",
"test": "jest",
"start": "ts-node src/index.ts"
"start": "ts-node src/index.ts",
"benchmark": "ts-node benchmark/timeDistribution"
},
"devDependencies": {
"@types/jest": "^29.5.5",
Expand Down
Loading