github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/tests/integrations/event_benchmark_test.go (about)

     1  package integrations
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	_ "embed"
     7  	"net"
     8  	"net/http"
     9  	"os"
    10  	"path"
    11  	"strings"
    12  	"sync"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/google/uuid"
    17  
    18  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr"
    19  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/requires"
    20  	client2 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/client"
    21  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/transformer"
    22  	"github.com/machinefi/w3bstream/pkg/types"
    23  )
    24  
    25  //go:embed testdata/log.wasm
    26  var code []byte
    27  
    28  var (
    29  	clientEvent           *applet_mgr.Client
    30  	projectNameEventBench = "test_event_benchmark"
    31  	publisherToken        string
    32  )
    33  
    34  func required() func() {
    35  	var (
    36  		client    = requires.AuthClient()
    37  		projectID types.SFID
    38  	)
    39  
    40  	{
    41  		req := &applet_mgr.CreateProject{}
    42  		req.CreateReq.Name = projectNameEventBench
    43  
    44  		rsp, _, err := client.CreateProject(req)
    45  		if err != nil {
    46  			panic(err)
    47  		}
    48  		projectID = rsp.ProjectID
    49  	}
    50  
    51  	{
    52  		cwd, _ := os.Getwd()
    53  		filename := path.Join(cwd, "../testdata/log.wasm")
    54  		req := &applet_mgr.CreateApplet{
    55  			ProjectName: projectNameEventBench,
    56  		}
    57  		req.CreateReq.File = transformer.MustNewFileHeader("file", filename, bytes.NewBuffer(code))
    58  		req.CreateReq.Info = applet_mgr.GithubComMachinefiW3BstreamPkgModulesAppletInfo{
    59  			AppletName: "log",
    60  			WasmName:   "log.wasm",
    61  		}
    62  
    63  		_, _, err := client.CreateApplet(req)
    64  		if err != nil {
    65  			panic(err)
    66  		}
    67  	}
    68  
    69  	{
    70  		req := &applet_mgr.CreatePublisher{
    71  			ProjectName: projectNameEventBench,
    72  		}
    73  		req.CreateReq.Name = "test_publisher"
    74  		req.CreateReq.Key = "mn_test_publisher"
    75  
    76  		rsp, _, err := client.CreatePublisher(req)
    77  		if err != nil {
    78  			panic(err)
    79  		}
    80  		publisherToken = rsp.Token
    81  	}
    82  
    83  	clientEvent = requires.ClientEvent()
    84  	clientEvent.WithContext(client2.ContextWithDftTransport(context.Background(),
    85  		&http.Transport{
    86  			DialContext: (&net.Dialer{
    87  				Timeout:   5 * time.Second,
    88  				KeepAlive: 100 * time.Second,
    89  			}).DialContext,
    90  			DisableKeepAlives:     false,
    91  			TLSHandshakeTimeout:   5 * time.Second,
    92  			ResponseHeaderTimeout: 5 * time.Second,
    93  			ExpectContinueTimeout: 1 * time.Second,
    94  		},
    95  	))
    96  
    97  	return func() {
    98  		if projectID != 0 {
    99  			_, _ = client.RemoveProject(&applet_mgr.RemoveProject{
   100  				ProjectName: projectNameEventBench,
   101  			})
   102  		}
   103  	}
   104  }
   105  
   106  var onceRequire = &sync.Once{}
   107  
   108  func BenchmarkEventHandling(b *testing.B) {
   109  	var stop func()
   110  	onceRequire.Do(func() {
   111  		stop = required()
   112  	})
   113  	defer stop()
   114  
   115  	channel := strings.Join([]string{"aid", requires.AccountID.String(), projectNameEventBench}, "_")
   116  	failed := 0
   117  	b.N = 10
   118  	for i := 0; i < b.N; i++ {
   119  		req := &applet_mgr.HandleEvent{
   120  			Channel:      channel,
   121  			AuthInHeader: "Bearer " + publisherToken,
   122  			EventID:      uuid.NewString(),
   123  			Timestamp:    time.Now().UTC().UnixMicro(),
   124  			Payload:      *bytes.NewBufferString("log content: " + uuid.NewString()),
   125  		}
   126  		_, _, err := clientEvent.HandleEvent(req)
   127  		if err != nil {
   128  			b.Log(i, err)
   129  			failed++
   130  		}
   131  	}
   132  	b.Logf("summary %d/%d\n", failed, b.N)
   133  }