Skip to content

Commit

Permalink
loadtest: refactor stress test for better re-usability
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Oct 10, 2023
1 parent d7ae239 commit 49ffe7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions itest/loadtest/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type testCase struct {

var loadTestCases = []testCase{
{
name: "mint_batch_stress",
fn: execMintBatchStressTest,
name: "mint",
fn: mintTest,
},
}

Expand Down
54 changes: 10 additions & 44 deletions itest/loadtest/mint_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/rpcclient"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/itest"
"github.com/lightninglabs/taproot-assets/taprpc"
Expand All @@ -24,60 +23,27 @@ import (
//go:embed testdata/8k-metadata.hex
var imageMetadataHex []byte

// execMintBatchStressTest checks that we are able to mint a batch of assets
// and that other memebers in the federation see the universe updated
// accordingly.
func execMintBatchStressTest(t *testing.T, ctx context.Context, cfg *Config) {
// Create tapd clients.
alice, aliceCleanUp := getTapClient(t, ctx, cfg.Alice.Tapd)
defer aliceCleanUp()

_, err := alice.GetInfo(ctx, &taprpc.GetInfoRequest{})
require.NoError(t, err)

bob, bobCleanUp := getTapClient(t, ctx, cfg.Bob.Tapd)
defer bobCleanUp()

_, err = bob.GetInfo(ctx, &taprpc.GetInfoRequest{})
require.NoError(t, err)

// Create bitcoin client.
bitcoinClient := getBitcoinConn(t, cfg.Bitcoin)

itest.MineBlocks(t, bitcoinClient, 1, 0)

// If we fail from this point onward, we might have created a
// transaction that isn't mined yet. To make sure we can run the test
// again, we'll make sure to clean up the mempool by mining a block.
t.Cleanup(func() {
itest.MineBlocks(t, bitcoinClient, 1, 0)
})
// mintTest checks that we are able to mint a batch of assets and that other
// members in the federation see the universe updated accordingly.
func mintTest(t *testing.T, ctx context.Context, cfg *Config) {
// Start by initializing all our client connections.
alice, bob, bitcoinClient := initClients(t, ctx, cfg)

imageMetadataBytes, err := hex.DecodeString(
strings.Trim(string(imageMetadataHex), "\n"),
)
require.NoError(t, err)

aliceHost := fmt.Sprintf("%s:%d", cfg.Alice.Tapd.Host,
cfg.Alice.Tapd.Port)

minterTimeout := 10 * time.Minute
mintBatchStressTest(
t, ctx, bitcoinClient, alice, bob, aliceHost, cfg.BatchSize,
imageMetadataBytes, minterTimeout,
)
}

func mintBatchStressTest(t *testing.T, ctx context.Context,
bitcoinClient *rpcclient.Client, alice, bob itest.TapdClient,
aliceHost string, batchSize int, imageMetadataBytes []byte,
minterTimeout time.Duration) {

var (
minterTimeout = cfg.TestTimeout
batchSize = cfg.BatchSize
batchReqs = make([]*mintrpc.MintAssetRequest, batchSize)
baseName = fmt.Sprintf("jpeg-%d", rand.Int31())
metaPrefixSize = binary.MaxVarintLen16
metadataPrefix = make([]byte, metaPrefixSize)
aliceHost = fmt.Sprintf(
"%s:%d", alice.cfg.Host, alice.cfg.Port,
)
)

// Before we mint a new group, let's first find out how many there
Expand Down
46 changes: 40 additions & 6 deletions itest/loadtest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/btcsuite/btcd/rpcclient"
"github.com/lightninglabs/taproot-assets/itest"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
Expand All @@ -28,14 +29,45 @@ var (
)

type rpcClient struct {
cfg *TapConfig
taprpc.TaprootAssetsClient
universerpc.UniverseClient
mintrpc.MintClient
assetwalletrpc.AssetWalletClient
}

func initClients(t *testing.T, ctx context.Context,
cfg *Config) (*rpcClient, *rpcClient, *rpcclient.Client) {

// Create tapd clients.
alice := getTapClient(t, ctx, cfg.Alice.Tapd)

_, err := alice.GetInfo(ctx, &taprpc.GetInfoRequest{})
require.NoError(t, err)

bob := getTapClient(t, ctx, cfg.Bob.Tapd)

_, err = bob.GetInfo(ctx, &taprpc.GetInfoRequest{})
require.NoError(t, err)

// Create bitcoin client.
bitcoinClient := getBitcoinConn(t, cfg.Bitcoin)

// Test bitcoin client connection by mining a block.
itest.MineBlocks(t, bitcoinClient, 1, 0)

// If we fail from this point onward, we might have created a
// transaction that isn't mined yet. To make sure we can run the test
// again, we'll make sure to clean up the mempool by mining a block.
t.Cleanup(func() {
itest.MineBlocks(t, bitcoinClient, 1, 0)
})

return alice, bob, bitcoinClient
}

func getTapClient(t *testing.T, ctx context.Context,
cfg *TapConfig) (*rpcClient, func()) {
cfg *TapConfig) *rpcClient {

creds := credentials.NewTLS(&tls.Config{})
if cfg.TLSPath != "" {
Expand Down Expand Up @@ -72,7 +104,7 @@ func getTapClient(t *testing.T, ctx context.Context,
}

svrAddr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
conn, err := grpc.Dial(svrAddr, opts...)
conn, err := grpc.DialContext(ctx, svrAddr, opts...)
require.NoError(t, err)

assetsClient := taprpc.NewTaprootAssetsClient(conn)
Expand All @@ -81,17 +113,19 @@ func getTapClient(t *testing.T, ctx context.Context,
assetWalletClient := assetwalletrpc.NewAssetWalletClient(conn)

client := &rpcClient{
cfg: cfg,
TaprootAssetsClient: assetsClient,
UniverseClient: universeClient,
MintClient: mintMintClient,
AssetWalletClient: assetWalletClient,
}

cleanUp := func() {
conn.Close()
}
t.Cleanup(func() {
err := conn.Close()
require.NoError(t, err)
})

return client, cleanUp
return client
}

func getBitcoinConn(t *testing.T, cfg *BitcoinConfig) *rpcclient.Client {
Expand Down

0 comments on commit 49ffe7b

Please sign in to comment.