@@ -10,8 +10,10 @@ import (
1010 "context"
1111 "testing"
1212
13+ "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
1314 "github.com/hyperledger-labs/fabric-token-sdk/token/driver"
1415 "github.com/hyperledger-labs/fabric-token-sdk/token/driver/mock"
16+ benchmark2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/benchmark"
1517 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
1618 "github.com/stretchr/testify/assert"
1719 "github.com/stretchr/testify/require"
@@ -187,3 +189,222 @@ func TestDeserializeIssueAction(t *testing.T) {
187189 require .Error (t , err )
188190 })
189191}
192+
193+ // BenchmarkIssueServiceIssue benchmarks the Issue method of the IssueService.
194+ func BenchmarkIssueServiceIssue (b * testing.B ) {
195+ _ , _ , cases , err := benchmark2 .GenerateCasesWithDefaults ()
196+ require .NoError (b , err )
197+
198+ for _ , tc := range cases {
199+ b .Run (tc .Name , func (b * testing.B ) {
200+ env , err := newBenchmarkIssueEnv (b .N , tc .BenchmarkCase )
201+ require .NoError (b , err )
202+
203+ b .ResetTimer ()
204+ i := 0
205+ for b .Loop () {
206+ e := env .Envs [i % len (env .Envs )]
207+ action , _ , err := e .is .Issue (
208+ b .Context (),
209+ e .issuer ,
210+ e .tokenType ,
211+ e .values ,
212+ e .owners ,
213+ nil ,
214+ )
215+ require .NoError (b , err )
216+ require .NotNil (b , action )
217+ i ++
218+ }
219+ })
220+ }
221+ }
222+
223+ // TestParallelBenchmarkIssueServiceIssue runs the issue benchmark in parallel.
224+ func TestParallelBenchmarkIssueServiceIssue (t * testing.T ) {
225+ _ , _ , cases , err := benchmark2 .GenerateCasesWithDefaults ()
226+ require .NoError (t , err )
227+
228+ test := benchmark2.NewTest [* benchmarkIssueEnv ](cases )
229+ test .RunBenchmark (t ,
230+ func (c * benchmark2.Case ) (* benchmarkIssueEnv , error ) {
231+ return newBenchmarkIssueEnv (1 , c )
232+ },
233+ func (ctx context.Context , env * benchmarkIssueEnv ) error {
234+ action , _ , err := env .Envs [0 ].is .Issue (
235+ ctx ,
236+ env .Envs [0 ].issuer ,
237+ env .Envs [0 ].tokenType ,
238+ env .Envs [0 ].values ,
239+ env .Envs [0 ].owners ,
240+ nil ,
241+ )
242+ if err != nil {
243+ return err
244+ }
245+ _ , err = action .Serialize ()
246+
247+ return err
248+ },
249+ )
250+ }
251+
252+ // BenchmarkIssuer benchmarks issue action generation and serialization.
253+ func BenchmarkIssuer (b * testing.B ) {
254+ _ , _ , cases , err := benchmark2 .GenerateCasesWithDefaults ()
255+ require .NoError (b , err )
256+
257+ test := benchmark2.NewTest [* benchmarkIssuerActionEnv ](cases )
258+ test .GoBenchmark (b ,
259+ func (c * benchmark2.Case ) (* benchmarkIssuerActionEnv , error ) {
260+ return newBenchmarkIssuerActionEnv (1 , c )
261+ },
262+ func (ctx context.Context , env * benchmarkIssuerActionEnv ) error {
263+ transfer := & actions.IssueAction {
264+ Issuer : env .Envs [0 ].issuer ,
265+ Outputs : env .Envs [0 ].outputs ,
266+ }
267+ _ , err := transfer .Serialize ()
268+
269+ return err
270+ },
271+ )
272+ }
273+
274+ // BenchmarkProofVerificationIssuer benchmarks issue action deserialization and verification.
275+ func BenchmarkProofVerificationIssuer (b * testing.B ) {
276+ _ , _ , cases , err := benchmark2 .GenerateCasesWithDefaults ()
277+ require .NoError (b , err )
278+
279+ test := benchmark2.NewTest [* benchmarkIssuerActionEnv ](cases )
280+ test .GoBenchmark (b ,
281+ func (c * benchmark2.Case ) (* benchmarkIssuerActionEnv , error ) {
282+ return newBenchmarkIssuerVerificationEnv (1 , c )
283+ },
284+ func (ctx context.Context , env * benchmarkIssuerActionEnv ) error {
285+ ia := & actions.IssueAction {}
286+ if err := ia .Deserialize (env .Envs [0 ].raw ); err != nil {
287+ return err
288+ }
289+
290+ return ia .Validate ()
291+ },
292+ )
293+ }
294+
295+ type issuerActionEnv struct {
296+ issuer driver.Identity
297+ outputs []* actions.Output
298+ raw []byte
299+ }
300+
301+ func newIssuerActionEnv (benchmarkCase * benchmark2.Case ) (* issuerActionEnv , error ) {
302+ issuer := driver .Identity ("issuer" )
303+ outputs := make ([]* actions.Output , benchmarkCase .NumOutputs )
304+ for i := range outputs {
305+ outputs [i ] = & actions.Output {
306+ Owner : []byte ("owner" ),
307+ Type : "ABC" ,
308+ Quantity : token .NewQuantityFromUInt64 (uint64 (i )* 10 + 10 ).Hex (),
309+ }
310+ }
311+
312+ return & issuerActionEnv {
313+ issuer : issuer ,
314+ outputs : outputs ,
315+ }, nil
316+ }
317+
318+ type benchmarkIssuerActionEnv struct {
319+ Envs []* issuerActionEnv
320+ }
321+
322+ func newBenchmarkIssuerActionEnv (n int , benchmarkCase * benchmark2.Case ) (* benchmarkIssuerActionEnv , error ) {
323+ envs := make ([]* issuerActionEnv , n )
324+ for i := range n {
325+ env , err := newIssuerActionEnv (benchmarkCase )
326+ if err != nil {
327+ return nil , err
328+ }
329+ envs [i ] = env
330+ }
331+
332+ return & benchmarkIssuerActionEnv {Envs : envs }, nil
333+ }
334+
335+ func newBenchmarkIssuerVerificationEnv (n int , benchmarkCase * benchmark2.Case ) (* benchmarkIssuerActionEnv , error ) {
336+ envs := make ([]* issuerActionEnv , n )
337+ for i := range n {
338+ env , err := newIssuerActionEnv (benchmarkCase )
339+ if err != nil {
340+ return nil , err
341+ }
342+ issue := & actions.IssueAction {
343+ Issuer : env .issuer ,
344+ Outputs : env .outputs ,
345+ }
346+ raw , err := issue .Serialize ()
347+ if err != nil {
348+ return nil , err
349+ }
350+ env .raw = raw
351+ envs [i ] = env
352+ }
353+
354+ return & benchmarkIssuerActionEnv {Envs : envs }, nil
355+ }
356+
357+ type issueEnv struct {
358+ is * IssueService
359+ issuer driver.Identity
360+ tokenType token.Type
361+ values []uint64
362+ owners [][]byte
363+ }
364+
365+ func newIssueEnv (benchmarkCase * benchmark2.Case ) (* issueEnv , error ) {
366+ ppm := & mock.PublicParamsManager {}
367+ pp := & mock.PublicParameters {}
368+ pp .PrecisionReturns (64 )
369+ ppm .PublicParametersReturns (pp )
370+
371+ ws := & mock.WalletService {}
372+ des := & mock.Deserializer {}
373+ is := NewIssueService (ppm , ws , des )
374+
375+ issuer := driver .Identity ("issuer" )
376+ tokenType := token .Type ("ABC" )
377+ values := make ([]uint64 , benchmarkCase .NumOutputs )
378+ owners := make ([][]byte , benchmarkCase .NumOutputs )
379+ for i := range values {
380+ values [i ] = uint64 (i )* 10 + 10
381+ owners [i ] = []byte ("owner" )
382+ }
383+
384+ des .GetAuditInfoReturns ([]byte ("audit" ), nil )
385+
386+ return & issueEnv {
387+ is : is ,
388+ issuer : issuer ,
389+ tokenType : tokenType ,
390+ values : values ,
391+ owners : owners ,
392+ }, nil
393+ }
394+
395+ type benchmarkIssueEnv struct {
396+ Envs []* issueEnv
397+ }
398+
399+ func newBenchmarkIssueEnv (n int , benchmarkCase * benchmark2.Case ) (* benchmarkIssueEnv , error ) {
400+ envs := make ([]* issueEnv , n )
401+ for i := range n {
402+ env , err := newIssueEnv (benchmarkCase )
403+ if err != nil {
404+ return nil , err
405+ }
406+ envs [i ] = env
407+ }
408+
409+ return & benchmarkIssueEnv {Envs : envs }, nil
410+ }
0 commit comments