github.com/ethersphere/bee/v2@v2.2.0/pkg/pss/mining_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pss_test
     6  
     7  import (
     8  	"context"
     9  	"encoding/binary"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/crypto"
    14  	"github.com/ethersphere/bee/v2/pkg/pss"
    15  )
    16  
    17  func newTargets(length, depth int) pss.Targets {
    18  	targets := make([]pss.Target, length)
    19  	for i := 0; i < length; i++ {
    20  		buf := make([]byte, 8)
    21  		binary.LittleEndian.PutUint64(buf, uint64(i))
    22  		targets[i] = pss.Target(buf[:depth])
    23  	}
    24  	return pss.Targets(targets)
    25  }
    26  
    27  func BenchmarkWrap(b *testing.B) {
    28  	cases := []struct {
    29  		length int
    30  		depth  int
    31  	}{
    32  		{1, 1},
    33  		{256, 2},
    34  		{8, 1},
    35  		{256, 1},
    36  		{16, 2},
    37  		{64, 2},
    38  		{256, 3},
    39  		{4096, 3},
    40  		{16384, 3},
    41  	}
    42  	topic := pss.NewTopic("topic")
    43  	msg := []byte("this is my scariest")
    44  	key, err := crypto.GenerateSecp256k1Key()
    45  	if err != nil {
    46  		b.Fatal(err)
    47  	}
    48  	pubkey := &key.PublicKey
    49  	ctx := context.Background()
    50  	for _, c := range cases {
    51  		name := fmt.Sprintf("length:%d,depth:%d", c.length, c.depth)
    52  		b.Run(name, func(b *testing.B) {
    53  			targets := newTargets(c.length, c.depth)
    54  			for i := 0; i < b.N; i++ {
    55  				if _, err := pss.Wrap(ctx, topic, msg, pubkey, targets); err != nil {
    56  					b.Fatal(err)
    57  				}
    58  			}
    59  		})
    60  	}
    61  
    62  }