Newer
Older
// Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const { connectToNetwork } = require('../dltApp/dist/fabricConnect');
const fsp = require('fs').promises;
const fs = require('fs');
const util = require('util');
const utf8Decoder = new TextDecoder();
const topoDirectory = '../samples/';
const topologies = ['topo1.json', 'topo2.json', 'topo3.json', 'topo4.json'];
//const topologies = ['topo4.json'];
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const iterations = 1000;
async function main() {
try {
const { contract, close } = await connectToNetwork();
for (const topoFile of topologies) {
const logFilePath = `./operation_times_${topoFile.split('.')[0]}.txt`; // Creates a separate logfile for each topology
const appendFile = util.promisify(fs.appendFile.bind(fs, logFilePath));
console.log(`Starting tests for ${topoFile}`);
for (let i = 0; i < iterations; i++) {
console.log(`Iteration ${i + 1} for ${topoFile}`);
await runBlockchainOperations(contract, topoFile, appendFile);
}
}
await close(); // Clean up the connection
} catch (error) {
console.error('An error occurred:', error);
}
}
async function runBlockchainOperations(contract, topoFile, appendFile) {
const assetId = `asset${Date.now()}`;
const jsonData = await readJsonData(`${topoDirectory}${topoFile}`);
// Define operations
const operations = [
{ type: 'STORE', jsonData },
{ type: 'UPDATE', jsonData },
{ type: 'FETCH', jsonData: null },
{ type: 'DELETE', jsonData: null },
{ type: 'FETCH_ALL', jsonData: null }
];
for (let op of operations) {
await executeOperation(contract, op.type, assetId, op.jsonData, appendFile);
}
}
async function readJsonData(filePath) {
try {
return await fsp.readFile(filePath, 'utf8');
} catch (error) {
console.error(`Failed to read file: ${filePath}`, error);
return '{}';
}
}
async function executeOperation(contract, operationType, assetId, jsonData, appendFile) {
const startTime = process.hrtime.bigint();
try {
let result;
switch (operationType) {
case 'STORE':
result = await contract.submitTransaction('StoreTopoData', assetId, jsonData);
break;
case 'UPDATE':
result = await contract.submitTransaction('UpdateTopoData', assetId, jsonData);
break;
case 'FETCH':
result = await contract.evaluateTransaction('RetrieveTopoData', assetId);
break;
case 'DELETE':
result = await contract.submitTransaction('DeleteTopo', assetId);
break;
case 'FETCH_ALL':
result = await contract.evaluateTransaction('GetAllInfo');
break;
}
result = utf8Decoder.decode(result);
const operationTime = recordOperationTime(startTime);
await logOperationTime(operationTime, operationType, appendFile);
console.log(`${operationType} Result:`, result);
} catch (error) {
console.error(`Error during ${operationType}:`, error);
}
}
function recordOperationTime(startTime) {
const endTime = process.hrtime.bigint();
const operationTime = Number(endTime - startTime) / 1e6;
return operationTime;
}
async function logOperationTime(operationTime, operationType, appendFile) {
const timestamp = Date.now();
const logEntry = `${timestamp} - ${operationType} - Execution time: ${operationTime.toFixed(3)} ms\n`;
try {
await appendFile(logEntry);
} catch (error) {
console, error('Error writing to log file:', error);
}
}
main();