Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as fs from 'fs';
- import * as util from 'util';
- import { EventTimestamp } from './eventTimestamp';
- import { Counts, SubCounts } from './genericCounts';
- import { checkTestIntegrity } from './integrityCheck';
- import { EventType, ILogEvent } from './logEvent';
- import { info } from './logger';
- import { LogResults } from './logResults';
- import { INode, INodeMap } from './node';
- import { Pod } from './pod';
- import { TestConfig } from './testConfig';
- import { ITx, ITxMap } from './tx';
- const localTestConfig = new TestConfig(60, 2, true, 1);
- enum ValidatorType {
- CURRENT = 0,
- PREVIOUS = 1,
- SNAPSHOT = 2,
- }
- enum LogType {
- FIREBASE = 0,
- WINSTON = 1,
- MONGODB = 2,
- }
- const getGlobalResults = (): LogResults => globalResults;
- let startTime: number;
- let endTime: number;
- let expectedEndTime: number = Date.now() * 2;
- let globalResults = {} as LogResults;
- const parseResults = (results: any, type: LogType, printResults: boolean): void => {
- type === LogType.WINSTON && (parseWinstonResults(results, printResults));
- type === LogType.FIREBASE && (parseFirebaseResults(results, printResults));
- type === LogType.MONGODB && (parseMongoDBResults(results, printResults));
- };
- const parseWinstonResults = (results: any, printResults: boolean): void => {
- const firstEntry: ILogEvent = results.file[0];
- const lastEntry: ILogEvent = results.file[results.file.length - 1];
- startTime = Date.parse(<string>firstEntry.timestamp);
- endTime = Date.parse(<string>lastEntry.timestamp);
- parseCommonResults(results.file, printResults, startTime, endTime);
- };
- const parseFirebaseResults = (results: any, printResults: boolean): void => {
- const keys = Object.keys(results);
- const firstEntry: ILogEvent = results[keys[0]];
- const lastEntry: ILogEvent = results[keys[keys.length - 1]];
- startTime = <number>firstEntry.timestamp;
- endTime = <number>lastEntry.timestamp;
- const resultArray = [];
- for (let i = 0; i < keys.length; i += 1) {
- const key = keys[i];
- resultArray.push(results[key]);
- }
- parseCommonResults(resultArray, printResults, startTime, endTime);
- };
- const parseMongoDBResults = (results: any, printResults: boolean): void => {
- const firstEntry: ILogEvent = results[0];
- const lastEntry: ILogEvent = results[results.length - 1];
- startTime = <number>firstEntry.timestamp;
- endTime = <number>lastEntry.timestamp;
- info(`Log Start Time: ${startTime}`);
- info(`Log End Time: ${endTime}`);
- parseCommonResults(results, printResults, startTime, endTime);
- // const _results = JSON.parse(results);
- // const keys = Object.keys(_results);
- // const firstEntry: ILogEvent = _results[keys[0]];
- // const lastEntry: ILogEvent = _results[keys[keys.length - 1]];
- // startTime = <number>firstEntry.timestamp;
- // endTime = <number>lastEntry.timestamp;
- // const resultArray = [];
- // for (let i = 0; i < keys.length; i += 1) {
- // const key = keys[i];
- // resultArray.push(_results[key])
- // }
- // parseCommonResults(resultArray, printResults, startTime, endTime);
- };
- const parseCommonResults = (results: any, printResults: boolean, startTime: number, endTime: number): void => {
- const txMap: ITxMap = {};
- const nodeMap: INodeMap = {};
- for (let i = 0; i < results.length; i += 1) {
- const result: ILogEvent = results[i];
- // console.dir(result);
- const {
- sender,
- receiver,
- connectionTo,
- owner,
- timestamp,
- transactionId,
- eventType,
- testConfig,
- }: ILogEvent = result;
- const nTimestamp: number = typeof timestamp === 'string' ? Date.parse(<string>timestamp) : timestamp;
- const tx = getTx(transactionId, txMap);
- const _sender = getNode(sender, nodeMap);
- const _receiver = getNode(receiver, nodeMap);
- const _owner = getNode(owner, nodeMap);
- const _connectionTo = getNode(connectionTo, nodeMap);
- if (eventType === EventType.TEST_START) {
- Object.assign(localTestConfig, testConfig);
- expectedEndTime = startTime + localTestConfig.duration;
- continue;
- }
- if (nTimestamp < expectedEndTime) {
- switch (eventType) {
- case EventType.TRANSACTION_START:
- tx.txData = {
- from: _sender.pod.address,
- to: _receiver.pod.address,
- };
- tx.transaction.lifetime.start = nTimestamp;
- break;
- case EventType.TRANSACTION_END:
- tx.transaction.lifetime.end = nTimestamp;
- _sender.counts.sender += 1;
- break;
- case EventType.REQUEST_VALIDATION_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- _owner.counts.currentValidator += 1;
- tx.currentValidators[_owner.pod.address].validation.lifetime.start = nTimestamp;
- tx.currentValidators[_owner.pod.address].validation.requestValidation.start = nTimestamp;
- break;
- case EventType.REQUEST_VALIDATION_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.lifetime.end = nTimestamp;
- tx.currentValidators[_owner.pod.address].validation.requestValidation.end = nTimestamp;
- break;
- case EventType.CONNECT_TO_VALIDATOR_START:
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_connectionTo.pod.address].connection.start = nTimestamp;
- break;
- case EventType.CONNECT_TO_VALIDATOR_END:
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_connectionTo.pod.address].connection.end = nTimestamp;
- break;
- case EventType.CONNECT_TO_PREVIOUS_VALIDATOR_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx, () => {
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.PREVIOUS, tx, () => {
- tempDoesValidatorExist(_owner.pod.address, ValidatorType.PREVIOUS, _connectionTo.pod.address, tx);
- _connectionTo.counts.previousValidator += 1;
- tx.previousValidators[_connectionTo.pod.address].connection[_owner.pod.address].start = nTimestamp;
- });
- });
- break;
- case EventType.CONNECT_TO_PREVIOUS_VALIDATOR_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx, () => {
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.PREVIOUS, tx, () => {
- tempDoesValidatorExist(_owner.pod.address, ValidatorType.PREVIOUS, _connectionTo.pod.address, tx);
- tx.previousValidators[_connectionTo.pod.address].connection[_owner.pod.address].end = nTimestamp;
- });
- });
- break;
- case EventType.GENERATE_SIGNATURE_START:
- tx.transaction.generateSignature.start = nTimestamp;
- break;
- case EventType.GENERATE_SIGNATURE_END:
- tx.transaction.generateSignature.end = nTimestamp;
- break;
- case EventType.VALIDATE_SIGNATURE_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.validateSignature.start = nTimestamp;
- break;
- case EventType.VALIDATE_SIGNATURE_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.validateSignature.end = nTimestamp;
- break;
- case EventType.VALIDATE_LEDGER_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.validateLedger.start = nTimestamp;
- break;
- case EventType.VALIDATE_LEDGER_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.validateLedger.end = nTimestamp;
- break;
- case EventType.GET_ENTRY_FROM_LEDGER_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.PREVIOUS, tx);
- tx.previousValidators[_owner.pod.address].validation.getEntryFromLedger.start = nTimestamp;
- break;
- case EventType.GET_ENTRY_FROM_LEDGER_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.PREVIOUS, tx);
- tx.previousValidators[_owner.pod.address].validation.getEntryFromLedger.end = nTimestamp;
- break;
- case EventType.GENERATE_TRANSACTION_HASH_START:
- try {
- if (_owner.pod.address === _sender.pod.address) {
- tx.transaction.generateHash.start = nTimestamp;
- }
- else {
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.generateHash.start = nTimestamp;
- }
- } catch (error) {
- console.dir(result);
- console.log(error);
- }
- break;
- case EventType.GENERATE_TRANSACTION_HASH_END:
- if (_owner.pod.address === _sender.pod.address) {
- tx.transaction.generateHash.end = nTimestamp;
- }
- else {
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.generateHash.end = nTimestamp;
- }
- break;
- case EventType.WRITE_TO_MY_LEDGER_START:
- tx.transaction.writeToMyLedger.start = nTimestamp;
- break;
- case EventType.WRITE_TO_MY_LEDGER_END:
- tx.transaction.writeToMyLedger.end = nTimestamp;
- break;
- case EventType.WRITE_TO_WITNESS_LEDGER_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.writeToWitnessLedger.start = nTimestamp;
- break;
- case EventType.WRITE_TO_WITNESS_LEDGER_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx);
- tx.currentValidators[_owner.pod.address].validation.writeToWitnessLedger.end = nTimestamp;
- break;
- case EventType.CONNECT_TO_SNAPSHOT_NODE_START:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx, () => {
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.SNAPSHOT, tx, () => {
- tempDoesValidatorExist(_owner.pod.address, ValidatorType.SNAPSHOT, _connectionTo.pod.address, tx);
- _connectionTo.counts.snapshotValidator += 1;
- tx.snapshotValidators[_connectionTo.pod.address].connection[_owner.pod.address].start = nTimestamp;
- });
- });
- break;
- case EventType.CONNECT_TO_SNAPSHOT_NODE_END:
- doesValidatorExist(_owner.pod.address, ValidatorType.CURRENT, tx, () => {
- doesValidatorExist(_connectionTo.pod.address, ValidatorType.SNAPSHOT, tx, () => {
- tempDoesValidatorExist(_owner.pod.address, ValidatorType.SNAPSHOT, _connectionTo.pod.address, tx);
- tx.snapshotValidators[_connectionTo.pod.address].connection[_owner.pod.address].end = nTimestamp;
- });
- });
- break;
- default:
- break;
- }
- }
- if (i === results.length - 1) {
- const keys = Object.keys(txMap);
- const randomNumber = Math.floor(Math.random() * (keys.length));
- const key = keys[randomNumber];
- const randomTx = txMap[key];
- const entries = (<any>Object).entries(txMap);
- setAllDiffs(entries);
- populateNodePerformance(txMap, nodeMap, (newTxMap, newPodMap) => {
- calculateNodePerformance(newTxMap, newPodMap);
- });
- checkTestIntegrity(results, txMap, nodeMap, localTestConfig, startTime, endTime, (result) => {
- if (result) {
- getResults(txMap, nodeMap, localTestConfig, printResults);
- }
- else {
- console.log('Log failed integrity check. Add messages.');
- }
- });
- console.log(key);
- console.log(util.inspect(randomTx, false, null));
- // console.dir(nodeMap);
- }
- }
- };
- const populateNodePerformance = (txMap: ITxMap, nodeMap: INodeMap, callback: (txMap: ITxMap, nodeMap: INodeMap) => void) => {
- const keys = Object.keys(txMap);
- for (let i = 0; i < keys.length; i += 1) {
- const key = keys[i];
- const tx = txMap[key];
- const cvKeys = Object.keys(tx.currentValidators);
- for (let k = 0; k < cvKeys.length; k += 1) {
- const cvKey = cvKeys[k];
- const node = getNodeByAddress(cvKey, nodeMap);
- if (!node.transactions.hasOwnProperty(key)) {
- node.transactions[key] = {
- currentValidator: false,
- previousValidator: false,
- };
- }
- node.transactions[key].currentValidator = true;
- }
- const pvKeys = Object.keys(tx.previousValidators);
- for (let j = 0; j < pvKeys.length; j += 1) {
- const pvKey = pvKeys[j];
- const node = getNodeByAddress(pvKey, nodeMap);
- if (!node.transactions.hasOwnProperty(key)) {
- node.transactions[key] = {
- currentValidator: false,
- previousValidator: false,
- };
- }
- node.transactions[key].previousValidator = true;
- }
- }
- callback(txMap, nodeMap);
- };
- const calculateNodePerformance = (txMap: ITxMap, nodeMap: INodeMap): void => {
- const keys = Object.keys(nodeMap);
- for (let i = 0; i < keys.length; i += 1) {
- const key = keys[i];
- const node = nodeMap[key];
- const txKeys = Object.keys(node.transactions);
- for (let k = 0; k < txKeys.length; k += 1) {
- const txKey = txKeys[k];
- const tx = txMap[txKey];
- const { txTimes, validationTimes, connectionTimes } = node.performance;
- const txDiff = tx.transaction.lifetime.diff;
- txTimes.count += 1;
- if (txDiff !== undefined) {
- txTimes.total += txDiff;
- txDiff > txTimes.longest && (txTimes.longest = txDiff);
- txDiff < txTimes.shortest && (txTimes.shortest = txDiff);
- }
- else {
- txTimes.undef += 1;
- }
- if (node.transactions[txKey].currentValidator) {
- const cv = tx.currentValidators[key];
- const cvValidDiff = cv.validation.lifetime.diff;
- validationTimes.count += 1;
- if (cvValidDiff !== undefined) {
- validationTimes.currentValidators.count += 1;
- validationTimes.total += cvValidDiff;
- validationTimes.currentValidators.total += cvValidDiff;
- cvValidDiff > validationTimes.currentValidators.longest && (validationTimes.currentValidators.longest = cvValidDiff);
- cvValidDiff < validationTimes.currentValidators.shortest && (validationTimes.currentValidators.shortest = cvValidDiff);
- }
- else {
- validationTimes.undef += 1;
- }
- const cvConnDiff = cv.connection.diff;
- connectionTimes.count += 1;
- if (cvConnDiff !== undefined) {
- connectionTimes.currentValidators.count += 1;
- connectionTimes.total += cvConnDiff;
- connectionTimes.currentValidators.total += cvConnDiff;
- cvConnDiff > connectionTimes.currentValidators.longest && (connectionTimes.currentValidators.longest = cvConnDiff);
- cvConnDiff < connectionTimes.currentValidators.shortest && (connectionTimes.currentValidators.shortest = cvConnDiff);
- }
- else {
- connectionTimes.undef += 1;
- }
- }
- if (node.transactions[txKey].previousValidator) {
- const pv = tx.previousValidators[key];
- const pvValidDiff = pv.validation.lifetime.diff;
- validationTimes.count += 1;
- if (pvValidDiff !== undefined) {
- validationTimes.previousValidators.count += 1;
- validationTimes.total += pvValidDiff;
- validationTimes.previousValidators.total += pvValidDiff;
- pvValidDiff > validationTimes.previousValidators.longest && (validationTimes.previousValidators.longest = pvValidDiff);
- pvValidDiff < validationTimes.previousValidators.shortest && (validationTimes.previousValidators.shortest = pvValidDiff);
- }
- else {
- validationTimes.undef += 1;
- }
- const pvConnKeys = Object.keys(pv.connection);
- for (let j = 0; j < pvConnKeys.length; j += 1) {
- const pvcKey = pvConnKeys[j];
- const pvc = pv.connection[pvcKey];
- const pvcDiff = pvc.diff;
- connectionTimes.count += 1;
- if (pvcDiff !== undefined) {
- connectionTimes.previousValidators.count += 1;
- connectionTimes.total += pvcDiff;
- connectionTimes.previousValidators.total += pvcDiff;
- pvcDiff > connectionTimes.previousValidators.longest && (connectionTimes.previousValidators.longest = pvcDiff);
- pvcDiff < connectionTimes.previousValidators.shortest && (connectionTimes.previousValidators.shortest = pvcDiff);
- }
- else {
- connectionTimes.undef += 1;
- }
- }
- }
- }
- }
- };
- const setAllDiffs = (entries: [string, ITx][]) => {
- const readProp = (prop: any) => {
- const keys = Object.keys(prop);
- for (let i = 0; i < keys.length; i += 1) {
- const key = keys[i];
- const value = prop[key];
- if (value) {
- if (value.constructor.name === 'EventTimestamp') {
- if (value.end >= value.start) {
- value.diff = value.end - value.start;
- }
- }
- else {
- if (typeof value === 'object') {
- readProp(value);
- }
- }
- }
- }
- };
- for (const [, tx] of entries) {
- readProp(tx);
- }
- };
- const getNode = (pod: Pod | undefined, nodeMap: INodeMap): INode => {
- let node = {} as INode;
- if (pod !== undefined) {
- if (!nodeMap.hasOwnProperty(pod.address)) {
- info('[getNode] Pod does not exist in nodeMap. Adding...');
- node = nodeMap[pod.address] = {
- pod,
- location: '',
- counts: {
- sender: 0,
- receiver: 0,
- currentValidator: 0,
- previousValidator: 0,
- },
- transactions: {},
- performance: {
- txTimes: new SubCounts(),
- validationTimes: new Counts(),
- connectionTimes: new Counts(),
- },
- };
- }
- else {
- info('[getNode] Pod already exists in nodeMap.');
- node = nodeMap[pod.address];
- }
- }
- else {
- console.log('Pod was undefined.');
- }
- return node;
- };
- const getNodeByAddress = (address: string, nodeMap: INodeMap): INode => {
- let node = {} as INode;
- if (!nodeMap.hasOwnProperty(address)) {
- info(`[getNodeByAddress] Pod with address: ${address} does not exist in nodeMap.`);
- }
- else {
- info('[getNodeByAddress] Pod already exists in nodeMap.');
- node = nodeMap[address];
- }
- return node;
- };
- const getTx = (transactionId: string, txMap: ITxMap): ITx => {
- let _tx = {} as ITx;
- if (!txMap.hasOwnProperty(transactionId)) {
- _tx = txMap[transactionId] = {
- txData: {
- from: '',
- to: '',
- },
- transaction: {
- lifetime: new EventTimestamp(0, 0),
- generateSignature: new EventTimestamp(0, 0),
- writeToMyLedger: new EventTimestamp(0, 0),
- generateHash: new EventTimestamp(0, 0),
- },
- currentValidators: {},
- previousValidators: {},
- snapshotValidators: {},
- };
- }
- else {
- _tx = txMap[transactionId];
- }
- return _tx;
- };
- const tempDoesValidatorExist = (currentValidatorAddress: string, type: ValidatorType, validatorAddress: string, tx: ITx): void => {
- if (type === ValidatorType.PREVIOUS) {
- if (!tx.previousValidators[validatorAddress].connection.hasOwnProperty(currentValidatorAddress)) {
- tx.previousValidators[validatorAddress].connection[currentValidatorAddress] = new EventTimestamp(0, 0);
- }
- }
- if (type === ValidatorType.SNAPSHOT) {
- if (!tx.snapshotValidators[validatorAddress].connection.hasOwnProperty(currentValidatorAddress)) {
- tx.snapshotValidators[validatorAddress].connection[currentValidatorAddress] = new EventTimestamp(0, 0);
- }
- }
- };
- const doesValidatorExist = (address: string, type: ValidatorType, tx: ITx, callback: () => void = () => {}): void => {
- if (type === ValidatorType.CURRENT) {
- if (!tx.currentValidators.hasOwnProperty(address)) {
- addValidatorToTx(address, type, tx, callback);
- }
- else {
- callback();
- }
- }
- if (type === ValidatorType.PREVIOUS) {
- if (!tx.previousValidators.hasOwnProperty(address)) {
- addValidatorToTx(address, type, tx, callback);
- }
- else {
- callback();
- }
- }
- if (type === ValidatorType.SNAPSHOT) {
- if (!tx.snapshotValidators.hasOwnProperty(address)) {
- addValidatorToTx(address, type, tx, callback);
- }
- else {
- callback();
- }
- }
- };
- const addValidatorToTx = (address: string, type: ValidatorType, tx: ITx, callback: () => void = () => { }): ITx => {
- const currentValidatorObj = {
- validation: {
- lifetime: new EventTimestamp(0, 0),
- requestValidation: new EventTimestamp(0, 0),
- validateSignature: new EventTimestamp(0, 0),
- validateLedger: new EventTimestamp(0, 0),
- generateHash: new EventTimestamp(0, 0),
- writeToWitnessLedger: new EventTimestamp(0, 0),
- },
- connection: new EventTimestamp(0, 0),
- };
- const previousValidatorObj = {
- validation: {
- lifetime: new EventTimestamp(0, 0),
- getEntryFromLedger: new EventTimestamp(0, 0),
- },
- connection: {},
- };
- const snapshotValidatorObj = {
- validation: {
- generateSnapshot: new EventTimestamp(0, 0),
- },
- connection: {},
- };
- if (type === ValidatorType.CURRENT) {
- tx.currentValidators[address] = currentValidatorObj;
- }
- if (type === ValidatorType.PREVIOUS) {
- tx.previousValidators[address] = previousValidatorObj;
- }
- if (type === ValidatorType.SNAPSHOT) {
- tx.snapshotValidators[address] = snapshotValidatorObj;
- }
- callback();
- return tx;
- };
- const getResults = (txMap: ITxMap, nodeMap: INodeMap, testConfig: TestConfig, printResults: boolean): LogResults | void => {
- const results = new LogResults(txMap, nodeMap, testConfig);
- results.numTxs = Object.keys(txMap).length;
- const regularPodCount = Object.keys(nodeMap).filter(key => nodeMap[key].pod.podType === 0).length;
- const partnerPodCount = Object.keys(nodeMap).filter(key => nodeMap[key].pod.podType === 1).length;
- results.numPods = {
- total: Object.keys(nodeMap).length,
- regular: regularPodCount,
- partner: partnerPodCount,
- sender: testConfig.numSenders,
- };
- results.duration = endTime - startTime;
- results.setTimes();
- globalResults = results;
- fs.writeFileSync('results.json', JSON.stringify(results, null, 2));
- if (printResults) {
- results.printResults();
- }
- else {
- return results;
- }
- };
- export { getGlobalResults, LogType, parseResults };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement