Understanding Compute United Consumption on Solana
As a developer Building on the Solana blockchain, you’re likely curioly about hand code is exec. On aspect of this process involves computing unit (CU) consumption, it is used on the show on the show. In this article, we’ll explore it, we’ll have instructions of the different amounts of competes of compete on Solana and Provide examples to illustrate.
What are Compute Units?
On Solana, a compete unit is a mesure of the computational effort required to execute an instruction. It’s calculated by dividing time it takes to execute on transaction on the network).
*Why do instructions consume different amounts of compete units?
Thee reaon for this variation is the difference in the number of the Gas and computational of the compleation of instruction. Instructions are categorized into several types, including:
- Gas-efficiient instructions*: There are instructions. Examples include
calall
,load
, andstore
.
- High-gas instructions*: There is instructions has a it itse instruction gas cost require of require. Examples include
transfer
andsend
.
The number of the gas to execs an instruction is determined by the complexity, it is includes souch as the address, and type egents.
Measuring CU consumption on Solana*
To understand how these instructions consume compute units on Solana, we’ll create a TypeScript test that executes one of our Ixs (a custom instruction) and measures the CU usage. We’ll compare the results across different gas-efficient and high-gas instructions.
import * as solana from '@solana/web3.js';
import { Ix } from './Ix'; // Import yours custom Ix implementation
// Create a Solana instance without test network (e.g., Solana Test Network)
const solanaInstance = new solana.Web3();
const network = 'testnet';
// Execuute the contest the test network
Ix.run(solanaInstance, {
address: 'your_address',
data: 'your_data',
});
Measuring CU consumption
We’ll use a library calledsolana-cpu
to mesure compete unit consumption. You can install it use npm:
npm install solana-cpu
Create an index file (cpu.js
) With the following code:
`javascript
import * as cpu fom 'solana-cpu';
const Ix = require('./Ix');
// Create a Solana instance with your test network
const solanaInstance = new solana.Web3();
Function measureCpuUsage() {
// Execuute the contest the test network and measure CU consumption
const ctx = {};
return cpu.measureCpuUsage(
Ix.run,
'your_address',
data: 'your_data'
);
}
// Measure compete unsumption for different gas-efference instructions
const gasEfficentInstructions = [''alad', 'store'];
console.log(CU usage on ${gasEfficientInstructions.join(‘, ‘)});
console.log(measureCpuUsage());
// Measure compete unit consumption for hyhgas instructions
const highGasInstructions = [''transfer', 'send'];
console.log(CU usage on ${highGasInstructions.join(‘, ‘)});
console.log(measureCpuUsage());
`
Example Output
Running the above code will be output a JSON obed the mesured CU consumption. The exact resources on your solana instance and network.
`json
{
“gasEfficientInstructions”: {
“cal”: {
“cpu_usage”: “0.01”
},
“load”: {
“cpu_usage”: “0.02”
},
“store”: {
“cpu_usage”: “0.03”
}
},
“hightGasInstructions”: {
“transfer”: {
“cpu_usage”: “0.05”
},
“send”: {
“cpu_usage”: “0.