Skip to content

Commit

Permalink
Merge pull request #47 from erik-sth/improve-distribution
Browse files Browse the repository at this point in the history
Improve distribution
  • Loading branch information
erik-sth authored Oct 28, 2023
2 parents e0bb4b5 + 76454e6 commit c771163
Show file tree
Hide file tree
Showing 16 changed files with 13,386 additions and 223 deletions.
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

0 comments on commit c771163

Please sign in to comment.