|
| 1 | +package performance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + "github.com/onsi/ginkgo" |
| 8 | + "github.com/rs/zerolog/log" |
| 9 | + uuid "github.com/satori/go.uuid" |
| 10 | + "github.com/smartcontractkit/integrations-framework/actions" |
| 11 | + "github.com/smartcontractkit/integrations-framework/client" |
| 12 | + "github.com/smartcontractkit/integrations-framework/contracts" |
| 13 | + "github.com/smartcontractkit/integrations-framework/environment" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | + "math/big" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// RunlogJobMap is a custom map type that holds the record of jobs by the contract instance and the chainlink node |
| 21 | +type RunlogJobMap map[ConsumerOraclePair]map[client.Chainlink]string |
| 22 | + |
| 23 | +// ConsumerOraclePair consumer and oracle pair |
| 24 | +type ConsumerOraclePair struct { |
| 25 | + consumer contracts.APIConsumer |
| 26 | + oracle contracts.Oracle |
| 27 | + jobUUID string |
| 28 | +} |
| 29 | + |
| 30 | +// RunlogTestOptions contains the parameters for the Runlog soak test to be executed |
| 31 | +type RunlogTestOptions struct { |
| 32 | + TestOptions |
| 33 | + RoundTimeout time.Duration |
| 34 | + AdapterValue int |
| 35 | + TestDuration time.Duration |
| 36 | +} |
| 37 | + |
| 38 | +// RunlogTest is the implementation of Test that will configure and execute soak test |
| 39 | +// of Runlog contracts & jobs |
| 40 | +type RunlogTest struct { |
| 41 | + TestOptions RunlogTestOptions |
| 42 | + Environment environment.Environment |
| 43 | + Blockchain client.BlockchainClient |
| 44 | + Wallets client.BlockchainWallets |
| 45 | + Deployer contracts.ContractDeployer |
| 46 | + Link contracts.LinkToken |
| 47 | + |
| 48 | + chainlinkClients []client.Chainlink |
| 49 | + nodeAddresses []common.Address |
| 50 | + contractInstances []*ConsumerOraclePair |
| 51 | + adapter environment.ExternalAdapter |
| 52 | + |
| 53 | + jobMap RunlogJobMap |
| 54 | +} |
| 55 | + |
| 56 | +// NewRunlogTest creates new Runlog performance/soak test |
| 57 | +func NewRunlogTest( |
| 58 | + testOptions RunlogTestOptions, |
| 59 | + env environment.Environment, |
| 60 | + link contracts.LinkToken, |
| 61 | + blockchain client.BlockchainClient, |
| 62 | + wallets client.BlockchainWallets, |
| 63 | + deployer contracts.ContractDeployer, |
| 64 | + adapter environment.ExternalAdapter, |
| 65 | +) Test { |
| 66 | + return &RunlogTest{ |
| 67 | + TestOptions: testOptions, |
| 68 | + Environment: env, |
| 69 | + Link: link, |
| 70 | + Blockchain: blockchain, |
| 71 | + Wallets: wallets, |
| 72 | + Deployer: deployer, |
| 73 | + adapter: adapter, |
| 74 | + jobMap: RunlogJobMap{}, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// RecordValues records Runlog metrics |
| 79 | +func (f *RunlogTest) RecordValues(b ginkgo.Benchmarker) error { |
| 80 | + // TODO: collect metrics |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// Setup setups Runlog performance/soak test |
| 85 | +func (f *RunlogTest) Setup() error { |
| 86 | + chainlinkClients, err := environment.GetChainlinkClients(f.Environment) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + nodeAddresses, err := actions.ChainlinkNodeAddresses(chainlinkClients) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + adapter, err := environment.GetExternalAdapter(f.Environment) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + f.chainlinkClients = chainlinkClients |
| 99 | + f.nodeAddresses = nodeAddresses |
| 100 | + f.adapter = adapter |
| 101 | + return f.deployContracts() |
| 102 | +} |
| 103 | + |
| 104 | +func (f *RunlogTest) deployContract(c chan<- *ConsumerOraclePair) error { |
| 105 | + oracle, err := f.Deployer.DeployOracle(f.Wallets.Default(), f.Link.Address()) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if err = oracle.SetFulfillmentPermission(f.Wallets.Default(), f.nodeAddresses[0].Hex(), true); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + consumer, err := f.Deployer.DeployAPIConsumer(f.Wallets.Default(), f.Link.Address()) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + err = consumer.Fund(f.Wallets.Default(), nil, big.NewFloat(20000)) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + c <- &ConsumerOraclePair{consumer: consumer, oracle: oracle} |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (f *RunlogTest) deployContracts() error { |
| 125 | + contractChan := make(chan *ConsumerOraclePair, f.TestOptions.NumberOfContracts) |
| 126 | + g := errgroup.Group{} |
| 127 | + |
| 128 | + for i := 0; i < f.TestOptions.NumberOfContracts; i++ { |
| 129 | + g.Go(func() error { |
| 130 | + return f.deployContract(contractChan) |
| 131 | + }) |
| 132 | + } |
| 133 | + if err := g.Wait(); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + close(contractChan) |
| 137 | + for contract := range contractChan { |
| 138 | + f.contractInstances = append(f.contractInstances, contract) |
| 139 | + } |
| 140 | + log.Warn().Int("Pairs", len(f.contractInstances)).Msg("Pairs") |
| 141 | + return f.Blockchain.WaitForEvents() |
| 142 | +} |
| 143 | + |
| 144 | +func (f *RunlogTest) requestData() error { |
| 145 | + g := errgroup.Group{} |
| 146 | + for _, p := range f.contractInstances { |
| 147 | + p := p |
| 148 | + g.Go(func() error { |
| 149 | + jobUUIDReplaces := strings.Replace(p.jobUUID, "-", "", 4) |
| 150 | + var jobID [32]byte |
| 151 | + copy(jobID[:], jobUUIDReplaces) |
| 152 | + if err := p.consumer.CreateRequestTo( |
| 153 | + f.Wallets.Default(), |
| 154 | + p.oracle.Address(), |
| 155 | + jobID, |
| 156 | + big.NewInt(1e18), |
| 157 | + fmt.Sprintf("%s/five", f.adapter.ClusterURL()), |
| 158 | + "data,result", |
| 159 | + big.NewInt(100), |
| 160 | + ); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + return nil |
| 164 | + }) |
| 165 | + } |
| 166 | + return g.Wait() |
| 167 | +} |
| 168 | + |
| 169 | +// Run runs Runlog performance/soak test |
| 170 | +func (f *RunlogTest) Run() error { |
| 171 | + if err := f.createChainlinkJobs(); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + ctx, cancel := context.WithTimeout(context.Background(), f.TestOptions.TestDuration) |
| 175 | + defer cancel() |
| 176 | + i := 1 |
| 177 | + for { |
| 178 | + select { |
| 179 | + case <-ctx.Done(): |
| 180 | + log.Warn().Msg("Test finished") |
| 181 | + return nil |
| 182 | + default: |
| 183 | + log.Warn().Int("RoundID", i).Msg("New round") |
| 184 | + if err := f.requestData(); err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + if err := f.waitRoundEnd(i); err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + i++ |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func (f *RunlogTest) waitRoundEnd(roundID int) error { |
| 196 | + for _, p := range f.contractInstances { |
| 197 | + rc := contracts.NewRunlogRoundConfirmer(p.consumer, big.NewInt(int64(roundID)), f.TestOptions.RoundTimeout) |
| 198 | + f.Blockchain.AddHeaderEventSubscription(p.consumer.Address(), rc) |
| 199 | + } |
| 200 | + return f.Blockchain.WaitForEvents() |
| 201 | +} |
| 202 | + |
| 203 | +func (f *RunlogTest) createChainlinkJobs() error { |
| 204 | + jobsChan := make(chan RunlogJobMap, len(f.contractInstances)) |
| 205 | + g := errgroup.Group{} |
| 206 | + |
| 207 | + bta := client.BridgeTypeAttributes{ |
| 208 | + Name: "five", |
| 209 | + URL: fmt.Sprintf("%s/five", f.adapter.ClusterURL()), |
| 210 | + } |
| 211 | + if err := f.chainlinkClients[0].CreateBridge(&bta); err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + os := &client.DirectRequestTxPipelineSpec{ |
| 215 | + BridgeTypeAttributes: bta, |
| 216 | + DataPath: "data,result", |
| 217 | + } |
| 218 | + ost, err := os.String() |
| 219 | + if err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + |
| 223 | + for _, p := range f.contractInstances { |
| 224 | + p := p |
| 225 | + g.Go(func() error { |
| 226 | + jobUUID := uuid.NewV4() |
| 227 | + p.jobUUID = jobUUID.String() |
| 228 | + _, err := f.chainlinkClients[0].CreateJob(&client.DirectRequestJobSpec{ |
| 229 | + Name: "direct_request", |
| 230 | + ContractAddress: p.oracle.Address(), |
| 231 | + ExternalJobID: jobUUID.String(), |
| 232 | + ObservationSource: ost, |
| 233 | + }) |
| 234 | + if err != nil { |
| 235 | + return err |
| 236 | + } |
| 237 | + return nil |
| 238 | + }) |
| 239 | + } |
| 240 | + if err := g.Wait(); err != nil { |
| 241 | + return err |
| 242 | + } |
| 243 | + close(jobsChan) |
| 244 | + |
| 245 | + for jobMap := range jobsChan { |
| 246 | + for contractAddr, m := range jobMap { |
| 247 | + if _, ok := f.jobMap[contractAddr]; !ok { |
| 248 | + f.jobMap[contractAddr] = map[client.Chainlink]string{} |
| 249 | + } |
| 250 | + for k, v := range m { |
| 251 | + f.jobMap[contractAddr][k] = v |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + return nil |
| 256 | +} |
0 commit comments