@@ -10,9 +10,13 @@ import {
1010 SandboxContract ,
1111 TreasuryContract ,
1212} from "@ton/sandbox" ;
13- import { Forward } from "./contracts/output/forward_Forward" ;
1413import { Functions } from "./contracts/output/functions_Functions" ;
14+ import { Sha256Small } from "./contracts/output/benchmark_sha256_small_Sha256Small" ;
15+ import { Sha256Big } from "./contracts/output/benchmark_sha256_big_Sha256Big" ;
16+ import { Sha256AsSlice } from "./contracts/output/benchmark_sha256_as_slice_Sha256AsSlice" ;
17+ import { Forward } from "./contracts/output/forward_Forward" ;
1518import "@ton/test-utils" ;
19+ import { getUsedGas } from "./util" ;
1620
1721function measureGas ( txs : BlockchainTransaction [ ] ) {
1822 return (
@@ -67,4 +71,97 @@ describe("benchmarks", () => {
6771 const codeSize = testContract . init ! . code . toBoc ( ) . length ;
6872 expect ( codeSize ) . toMatchSnapshot ( "code size" ) ;
6973 } ) ;
74+
75+ async function hashStringSmall (
76+ sha256 : SandboxContract < Sha256Small > ,
77+ s : string ,
78+ ) : Promise < bigint > {
79+ const result = await sha256 . send (
80+ treasure . getSender ( ) ,
81+ { value : toNano ( 1 ) } ,
82+ { $$type : "HashData" , value : s } ,
83+ ) ;
84+
85+ return getUsedGas ( result ) ;
86+ }
87+
88+ async function hashStringBig (
89+ sha256 : SandboxContract < Sha256Big > ,
90+ s : string ,
91+ ) : Promise < bigint > {
92+ const result = await sha256 . send (
93+ treasure . getSender ( ) ,
94+ { value : toNano ( 1 ) } ,
95+ { $$type : "HashData" , value : s } ,
96+ ) ;
97+
98+ return getUsedGas ( result ) ;
99+ }
100+
101+ async function hashStringAsSLice (
102+ sha256 : SandboxContract < Sha256AsSlice > ,
103+ s : string ,
104+ ) : Promise < bigint > {
105+ const result = await sha256 . send (
106+ treasure . getSender ( ) ,
107+ { value : toNano ( 1 ) } ,
108+ { $$type : "HashData" , value : s } ,
109+ ) ;
110+
111+ return getUsedGas ( result ) ;
112+ }
113+
114+ it ( "benchmark sha256" , async ( ) => {
115+ const sha256Small = blockchain . openContract (
116+ await Sha256Small . fromInit ( ) ,
117+ ) ;
118+ const sha256Big = blockchain . openContract ( await Sha256Big . fromInit ( ) ) ;
119+ const sha256AsSlice = blockchain . openContract (
120+ await Sha256AsSlice . fromInit ( ) ,
121+ ) ;
122+
123+ await sha256Small . send (
124+ treasure . getSender ( ) ,
125+ { value : toNano ( 1 ) } ,
126+ null ,
127+ ) ;
128+ await sha256Big . send ( treasure . getSender ( ) , { value : toNano ( 1 ) } , null ) ;
129+ await sha256AsSlice . send (
130+ treasure . getSender ( ) ,
131+ { value : toNano ( 1 ) } ,
132+ null ,
133+ ) ;
134+
135+ await hashStringBig ( sha256Big , "hello world" ) ;
136+ await hashStringSmall ( sha256Small , "hello world" ) ;
137+ await hashStringAsSLice ( sha256AsSlice , "hello world" ) ;
138+
139+ expect ( await hashStringBig ( sha256Big , "hello world" ) ) . toEqual ( 3039n ) ;
140+ expect ( await hashStringSmall ( sha256Small , "hello world" ) ) . toEqual (
141+ 2516n ,
142+ ) ;
143+ expect ( await hashStringAsSLice ( sha256AsSlice , "hello world" ) ) . toEqual (
144+ 2516n ,
145+ ) ;
146+
147+ expect ( await hashStringBig ( sha256Big , "hello world" . repeat ( 5 ) ) ) . toEqual (
148+ 3040n ,
149+ ) ;
150+ expect (
151+ await hashStringSmall ( sha256Small , "hello world" . repeat ( 5 ) ) ,
152+ ) . toEqual ( 2516n ) ;
153+ expect (
154+ await hashStringAsSLice ( sha256AsSlice , "hello world" . repeat ( 5 ) ) ,
155+ ) . toEqual ( 2516n ) ;
156+
157+ expect (
158+ await hashStringBig ( sha256Big , "hello world" . repeat ( 10 ) ) ,
159+ ) . toEqual ( 3042n ) ;
160+ expect (
161+ await hashStringSmall ( sha256Small , "hello world" . repeat ( 10 ) ) ,
162+ ) . toEqual ( 2516n ) ;
163+ expect (
164+ await hashStringAsSLice ( sha256AsSlice , "hello world" . repeat ( 10 ) ) ,
165+ ) . toEqual ( 2516n ) ;
166+ } ) ;
70167} ) ;
0 commit comments