-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utxo perf test #1756
Merged
Merged
Utxo perf test #1756
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a3dab36
create and check utxo
ayrat555 a14b04c
spend utxo
ayrat555 e739591
assert balances after spending
ayrat555 dbac9c2
validate exact utxos
ayrat555 62d343c
fix token encoding
ayrat555 7f73e55
add test to run on ci
ayrat555 e3eedd8
Update priv/perf/apps/load_test/lib/watcher_info/utxo.ex
ayrat555 171d094
Update priv/perf/apps/load_test/lib/watcher_info/utxo.ex
ayrat555 3e4bac1
mix format
ayrat555 7920b3b
Merge branch 'master' into ayrat555/utxo-test
ayrat555 2f200a6
fix build
ayrat555 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule LoadTest.Runner.Utxos do | ||
@moduledoc """ | ||
Utxos tests runner. | ||
""" | ||
use Chaperon.LoadTest | ||
|
||
def scenarios do | ||
[ | ||
{{1, LoadTest.Scenario.Utxos}, %{}} | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule LoadTest.Scenario.Utxos do | ||
@moduledoc """ | ||
The scenario for utxos tests: | ||
|
||
1. It creates two accounts: the sender and the receiver. | ||
2. It funds sender with the specified amount on the childchain, checks utxos and balance. | ||
3. The sender account sends the specifed amount on the childchain to the receiver, | ||
checks its balance on the childchain and utxos for both accounts. | ||
|
||
""" | ||
|
||
use Chaperon.Scenario | ||
|
||
alias Chaperon.Session | ||
alias ExPlasma.Encoding | ||
alias LoadTest.ChildChain.Transaction | ||
alias LoadTest.Ethereum.Account | ||
alias LoadTest.Service.Faucet | ||
alias LoadTest.WatcherInfo.Balance | ||
alias LoadTest.WatcherInfo.Utxo | ||
|
||
@spec run(Session.t()) :: Session.t() | ||
def run(session) do | ||
tps = config(session, [:run_config, :tps]) | ||
period_in_seconds = config(session, [:run_config, :period_in_seconds]) | ||
|
||
total_number_of_transactions = tps * period_in_seconds | ||
period_in_mseconds = period_in_seconds * 1_000 | ||
|
||
session | ||
|> cc_spread( | ||
:create_utxos_and_make_assertions, | ||
total_number_of_transactions, | ||
period_in_mseconds | ||
) | ||
|> await_all(:create_utxos_and_make_assertions) | ||
end | ||
|
||
def create_utxos_and_make_assertions(session) do | ||
with {:ok, sender, receiver} <- create_accounts(), | ||
{:ok, utxo} <- fund_account(session, sender), | ||
:ok <- spend_utxo(session, utxo, sender, receiver) do | ||
Session.add_metric(session, "error_rate", 0) | ||
else | ||
error -> | ||
log_error(session, "#{__MODULE__} failed with #{inspect(error)}") | ||
|
||
session | ||
|> Session.add_metric("error_rate", 1) | ||
|> Session.add_error(:error, error) | ||
end | ||
end | ||
|
||
defp create_accounts() do | ||
{:ok, sender_address} = Account.new() | ||
{:ok, receiver_address} = Account.new() | ||
|
||
{:ok, sender_address, receiver_address} | ||
end | ||
|
||
defp fund_account(session, account) do | ||
initial_amount = config(session, [:chain_config, :initial_amount]) | ||
token = config(session, [:chain_config, :token]) | ||
|
||
with {:ok, utxo} <- fund_childchain_account(account, initial_amount, token), | ||
:ok <- | ||
fetch_childchain_balance(account, | ||
amount: initial_amount, | ||
token: Encoding.to_binary(token), | ||
error: :wrong_childchain_after_funding | ||
), | ||
:ok <- validate_utxos(account, %{utxo | owner: account.addr}) do | ||
{:ok, utxo} | ||
end | ||
end | ||
|
||
defp validate_utxos(account, utxo) do | ||
utxo_with_owner = | ||
case utxo do | ||
:empty -> :empty | ||
_ -> %{utxo | owner: account.addr} | ||
end | ||
|
||
case Utxo.get_utxos(account, utxo_with_owner) do | ||
{:ok, _} -> :ok | ||
_other -> :invalid_utxos | ||
end | ||
end | ||
|
||
defp fund_childchain_account(address, amount, token) do | ||
case Faucet.fund_child_chain_account(address, amount, token) do | ||
{:ok, utxo} -> {:ok, utxo} | ||
_ -> :failed_to_fund_childchain_account | ||
end | ||
end | ||
|
||
defp spend_utxo(session, utxo, sender, receiver) do | ||
amount = config(session, [:chain_config, :initial_amount]) | ||
token = config(session, [:chain_config, :token]) | ||
fee = config(session, [:chain_config, :fee]) | ||
amount_to_transfer = amount - fee | ||
|
||
with [new_utxo] <- Transaction.spend_utxo(utxo, amount_to_transfer, fee, sender, receiver, token), | ||
:ok <- validate_utxos(sender, :empty), | ||
:ok <- validate_utxos(receiver, %{new_utxo | owner: receiver.addr}), | ||
:ok <- | ||
fetch_childchain_balance(sender, amount: 0, token: Encoding.to_binary(token), error: :wrong_sender_balance), | ||
:ok <- | ||
fetch_childchain_balance(receiver, | ||
amount: amount_to_transfer, | ||
token: Encoding.to_binary(token), | ||
error: :wrong_sender_balance | ||
) do | ||
:ok | ||
end | ||
end | ||
|
||
defp fetch_childchain_balance(account, amount: amount, token: token, error: error) do | ||
childchain_balance = Balance.fetch_balance(account.addr, amount, token) | ||
|
||
case childchain_balance do | ||
nil when amount == 0 -> :ok | ||
%{"amount" => ^amount} -> :ok | ||
_ -> error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License.w | ||
|
||
defmodule LoadTest.WatcherInfo.Utxo do | ||
@moduledoc """ | ||
Functions for retrieving utxos through WatcherInfo API. | ||
""" | ||
require Logger | ||
|
||
alias LoadTest.Connection.WatcherInfo | ||
alias LoadTest.Ethereum.Account | ||
alias LoadTest.Service.Sync | ||
alias LoadTest.Utils.Encoding | ||
|
||
@poll_timeout 60_000 | ||
|
||
@spec get_utxos(Account.addr_t(), ExPlasma.Utxo.t() | nil | :empty) :: {:ok, [] | ExPlasma.Utxo.t()} | no_return | ||
def get_utxos(sender, utxo \\ nil) do | ||
Sync.repeat_until_success( | ||
fn -> | ||
fetch_utxos(sender, utxo) | ||
end, | ||
@poll_timeout, | ||
"Failed to fetch utxos" | ||
) | ||
end | ||
|
||
defp fetch_utxos(sender, utxo) do | ||
client = WatcherInfo.client() | ||
body = %WatcherInfoAPI.Model.AddressBodySchema1{address: Encoding.to_hex(sender.addr)} | ||
account_utxos = WatcherInfoAPI.Api.Account.account_get_utxos(client, body) | ||
|
||
case account_utxos do | ||
{:ok, result} -> | ||
result.body | ||
|> Jason.decode!() | ||
|> find_utxo(utxo) | ||
|
||
other -> | ||
other | ||
end | ||
end | ||
|
||
defp find_utxo(decoded_response, nil) do | ||
{:ok, decoded_response} | ||
end | ||
|
||
defp find_utxo(%{"data" => []}, :empty) do | ||
{:ok, []} | ||
end | ||
|
||
defp find_utxo(decoded_response, :empty) do | ||
{:error, decoded_response} | ||
end | ||
|
||
defp find_utxo(decoded_response, utxo) do | ||
do_find_utxo(decoded_response, utxo) | ||
end | ||
|
||
defp do_find_utxo(response, utxo) do | ||
found_utxo = | ||
Enum.find(response["data"], fn %{ | ||
"amount" => amount, | ||
"blknum" => blknum, | ||
"currency" => currency, | ||
"oindex" => oindex, | ||
"otype" => otype, | ||
"owner" => owner, | ||
"txindex" => txindex | ||
} -> | ||
current_utxo = %ExPlasma.Utxo{ | ||
amount: amount, | ||
blknum: blknum, | ||
currency: Encoding.to_binary(currency), | ||
oindex: oindex, | ||
output_type: otype, | ||
owner: Encoding.to_binary(owner), | ||
txindex: txindex | ||
} | ||
|
||
current_utxo == utxo | ||
end) | ||
|
||
case found_utxo do | ||
nil -> {:error, response} | ||
_ -> {:ok, found_utxo} | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
priv/perf/apps/load_test/test/load_tests/runner/utxos_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule LoadTest.Runner.UtxosTest do | ||
use ExUnit.Case | ||
|
||
@moduletag :utxos | ||
|
||
test "deposits test" do | ||
token = "0x0000000000000000000000000000000000000000" | ||
initial_amount = 760 | ||
fee = 75 | ||
|
||
config = %{ | ||
chain_config: %{ | ||
token: token, | ||
initial_amount: initial_amount, | ||
fee: fee | ||
}, | ||
run_config: %{ | ||
tps: 1, | ||
period_in_seconds: 20 | ||
}, | ||
timeout: :infinity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why infinity?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the default timeout is 10_000 ms for task execution inside of chaperon
|
||
} | ||
|
||
result = Chaperon.run_load_test(LoadTest.Runner.Utxos, config: config) | ||
|
||
assert result.metrics["error_rate"][:mean] == 0.0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure this two are the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nil when amount == 0
means we're expecting amount to be empty in the childchain for the specified currency. if it's not found in the json returned from watcherinfo, it means amount is zero.