Intermediate

 

How to finding ReadSpeed of transactions for Stellar

STEP 1.

  • Create a folder and open it on vscode and creating a  package.json using npm init  file and also a file like read.js

Step 2.

  • Install three dependencies like  express, axios, moment

Express :
  • Express is a node js web application framework that provides broad features for building web and mobile applications.

  •  It is used to build a single page, multipage, and hybrid web application.

  • It’s a layer built on the top of the Node js that helps manage servers and routes.

 Axios :
  • Axios is a browser and Node. js promise-based HTTP client.

  •  Axios makes it straightforward to send asynchronous HTTP requests to REST APIs and conduct CRUD activities

Moment :
  • A JavaScript date library for parsing, validating, manipulating, and formatting dates.

 Step 3.

  • Now, import dependencies and create a express server in read.js like this.

 

Step 4.

  •  Make a async readTransction  function for calculating transction read speed :Read.js

//read.js
const readTransaction = async () => {

try {

}

catch (error) {

console.log(error);

}  };

Step 5.

  • Now , use logic to get read speed value like

//read.js
const readTransaction = async () => {

try {

//here initialize two variable like startTime and totalTime

let startTime,

totalTime = 0;

//use for loop 5 times time for the transction api

for (let index = 0; index < 5; index++) {

//assign current date to the startTime variable

startTime = Date.now();

//getting api response using axios

await axios

.get("https://horizon-testnet.stellar.org/transactions/")

.then(async (res) => {

console.log(res.data);

response = res.data;

})

.catch((error) => {

console.log(error);

});

//here finding difference of time

totalTime += moment().diff(moment(startTime), "milliseconds");

}

// finding average of totaltime divided by 5 (because loop is going on 5times)
Var readspeed =totalTime/5;

console.log('totaltime',readspeed);

 

} catch (error) {

console.log(error);

}

};

Time is calculating in milliseconds

 

readTransction();