<commit_before>const chai = require('chai');
const testObjects = require('../services/helpers/testObjects');

const { expect } = chai;
const PORT = 5256;

const getCharNumber = (result, ressourceNumbers = 1) => JSON.stringify(result).length / ressourceNumbers;

// TODO: should work for bundled ressources like school+users-> courses + classes -> lessons -> homeworks -> submissions
// TODO: should only call any ressource one time with max number
// TODO: call external request to see every hook, but maybe internal also nice to know
// TODO: call more then one request for each service ..without loop optimization from v8 engine
const configuration = [
	{
		serviceName: 'schools',
		ressources: [
			{ name: 'schools', number: 100 },
			// { name: 'years', number: 10 }, not needed, added in seed data
		],
	},
	{
		serviceName: 'users',
		ressources: [{ name: 'users', number: 1000 }],
	},
	{
		serviceName: 'classes',
		ressources: [
			{
				name: 'classes',
				number: 100,
				ressources: [
					{ name: 'users', number: 30, mappedTo: 'userIds', onlyId: true },
					{ name: 'users', number: 1, mappedTo: 'teacherIds', onlyId: true },
				],
			},
		],
	},
];

// TODO: replace console.log in future but winston do not work locally for me. Must figure out why.
const printInfo = (info) => {
	// eslint-disable-next-line no-console
	console.log(info);
};

// only find supported at the moment
const request = async (service, method, params) => {
	const result = await service[method](params);
	return result;
};

const getExample = (result) => {
	if (Array.isArray(result && result.data)) {
		return result.data[0];
	}
	return null;
};

const buildTest = (app, TestEventEmitter, limits, serviceName) => {
	describe(`[performance] ${serviceName} service`, () => {
		const method = 'find'; // ['get', 'find']; // TODO: get do not work
		const service = app.service(serviceName);

		it(`[p] ${serviceName} ${method}`, async () => {
			let dbCalls = 0;
			// const timeIterations = 100;
			const $limit = (service.paginate && service.paginate.max) || 1000;

			TestEventEmitter.on('mongoose_test_calls', () => {
				dbCalls += 1;
			});

			const params = {
				query: {
					$limit,
				},
			};

			// iterations over the call for time messure do not work like expected
			// TODO: figure out if feathers cache stuff
			const start = Date.now();
			const result = await request(service, method, params);
			const time = Date.now() - start;

			const charNumberByRessource = getCharNumber(result, $limit);

			printInfo({
				service: serviceName,
				method,<commit_msg>cleanup code<commit_after>const chai = require('chai');
const testObjects = require('../services/helpers/testObjects');

const { expect } = chai;
const PORT = 5256;

const getCharNumber = (result, ressourceNumbers = 1) => JSON.stringify(result).length / ressourceNumbers;

// TODO: should work for bundled ressources like school+users-> courses + classes -> lessons -> homeworks -> submissions
// TODO: should only call any ressource one time with max number
// TODO: call external request to see every hook, but maybe internal also nice to know
// TODO: call more then one request for each service ..without loop optimization from v8 engine
const configuration = [
	{
		serviceName: 'schools',
		ressources: [
			{ name: 'schools', number: 100 },
			// { name: 'years', number: 10 }, not needed, added in seed data
		],
	},
	{
		serviceName: 'users',
		ressources: [{ name: 'users', number: 1000 }],
	},
	{
		serviceName: 'classes',
		ressources: [
			{
				name: 'classes',
				number: 100,
				ressources: [
					{ name: 'users', number: 30, mappedTo: 'userIds', onlyId: true },
					{ name: 'users', number: 1, mappedTo: 'teacherIds', onlyId: true },
				],
			},
		],
	},
];

// TODO: replace console.log in future but winston do not work locally for me. Must figure out why.
const printInfo = (info) => {
	// eslint-disable-next-line no-console
	console.log(info);
};

// only find supported at the moment
const request = async (service, method, params) => {
	const result = await service[method](params);
	return result;
};

const getExample = (result) => {
	if (Array.isArray(result && result.data)) {
		return result.data[0];
	}
	return null;
};

const buildTest = (app, TestEventEmitter, limits, serviceName) => {
	describe(`[performance] ${serviceName} service`, () => {
		const method = 'find';
		const service = app.service(serviceName);

		it(`[p] ${serviceName} ${method}`, async () => {
			let dbCalls = 0;
			// const timeIterations = 100;
			const $limit = (service.paginate && service.paginate.max) || 1000;

			TestEventEmitter.on('mongoose_test_calls', () => {
				dbCalls += 1;
			});

			const params = {
				query: {
					$limit,
				},
			};

			// iterations over the call for time messure do not work like expected
			// TODO: figure out if feathers cache stuff
			const start = Date.now();
			const result = await request(service, method, params);
			const time = Date.now() - start;

			const charNumberByRessource = getCharNumber(result, $limit);

			printInfo({
				service: serviceName,
				method,
{'fraction_non_alphanumeric': 0.1435566000783392, 'fraction_numerical': 0.012142577359968664, 'mean_word_length': 3.4995594713656386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1191387', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1553, 'n_words': 509}