github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/in10nmem/inv/main.go (about)

     1  /*
     2  * Copyright (c) 2023-present unTill Pro, Ltd.
     3  * @author Maxim Geraskin
     4   */
     5  
     6  package main
     7  
     8  import (
     9  	"context"
    10  	"log"
    11  	"math/rand"
    12  	"os"
    13  	"sync"
    14  	"time"
    15  
    16  	"github.com/voedger/voedger/pkg/appdef"
    17  	"github.com/voedger/voedger/pkg/in10n"
    18  	"github.com/voedger/voedger/pkg/in10nmem"
    19  	in10nmemv1 "github.com/voedger/voedger/pkg/in10nmem/v1"
    20  	"github.com/voedger/voedger/pkg/istructs"
    21  )
    22  
    23  func main() {
    24  
    25  	const BigNumber = 1000000000000000000
    26  
    27  	quotas := in10n.Quotas{
    28  		Channels:                BigNumber,
    29  		ChannelsPerSubject:      BigNumber,
    30  		Subscriptions:           BigNumber,
    31  		SubscriptionsPerSubject: BigNumber,
    32  	}
    33  
    34  	if len(os.Args) < 2 {
    35  		println("Use v1 or v2 as argument")
    36  		return
    37  	}
    38  
    39  	switch os.Args[1] {
    40  	case "v1":
    41  		println("Running v1...")
    42  		nb := in10nmemv1.Provide(quotas)
    43  		runChannels(nb)
    44  	case "v2":
    45  		println("Running v2...")
    46  		nb, cleanup := in10nmem.ProvideEx2(quotas, time.Now)
    47  		defer cleanup()
    48  
    49  		runChannels(nb)
    50  	default:
    51  		log.Fatal("Unknown argument", os.Args[1])
    52  	}
    53  }
    54  
    55  func checkErr(err error) {
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  }
    60  
    61  const numPartitions = 200
    62  const numProjectorsPerPartition = 1000
    63  const eventsPerSeconds = 500
    64  const subject istructs.SubjectLogin = "main"
    65  
    66  var projectionPLog = appdef.NewQName("sys", "plog")
    67  
    68  func runChannels(broker in10n.IN10nBroker) {
    69  
    70  	wg := sync.WaitGroup{}
    71  
    72  	for partition := 0; partition < numPartitions; partition++ {
    73  
    74  		for projector := 0; projector < numProjectorsPerPartition; projector++ {
    75  
    76  			channelID, err := broker.NewChannel(subject, hours24)
    77  			checkErr(err)
    78  
    79  			projectionKeyExample := in10n.ProjectionKey{
    80  				App:        istructs.AppQName_test1_app1,
    81  				Projection: projectionPLog,
    82  				WS:         istructs.WSID(partition),
    83  			}
    84  
    85  			err = broker.Subscribe(channelID, projectionKeyExample)
    86  			checkErr(err)
    87  
    88  			wg.Add(1)
    89  			go runChannel(channelID, broker)
    90  		}
    91  	}
    92  
    93  	t := time.NewTicker(1 * time.Second / eventsPerSeconds)
    94  
    95  	println("numPartitions: ", numPartitions)
    96  	println("numProjectorsPerPartition: ", numProjectorsPerPartition)
    97  	println("eventsPerSeconds: ", eventsPerSeconds)
    98  
    99  	offset := 0
   100  	for range t.C {
   101  
   102  		// nolint
   103  		partition := rand.Intn(numPartitions)
   104  
   105  		projectionKeyExample := in10n.ProjectionKey{
   106  			App:        istructs.AppQName_test1_app1,
   107  			Projection: projectionPLog,
   108  			WS:         istructs.WSID(partition),
   109  		}
   110  		broker.Update(projectionKeyExample, 0)
   111  		offset++
   112  
   113  	}
   114  
   115  	wg.Wait()
   116  
   117  }
   118  
   119  func runChannel(channelID in10n.ChannelID, broker in10n.IN10nBroker) {
   120  
   121  	broker.WatchChannel(context.Background(), channelID, updatesMock)
   122  
   123  }
   124  
   125  func updatesMock(projection in10n.ProjectionKey, offset istructs.Offset) {
   126  
   127  }