move benchmarks/util.py to tests/util/benchmarks.py (#17251)

This commit is contained in:
Kyle Altendorf 2024-01-10 10:30:00 -05:00 committed by GitHub
parent c4f2595e54
commit 384c14615a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 176 additions and 171 deletions

View File

@ -8,18 +8,7 @@ from pathlib import Path
from time import monotonic
from typing import List
from benchmarks.utils import (
clvm_generator,
rand_bytes,
rand_class_group_element,
rand_g1,
rand_g2,
rand_hash,
rand_vdf,
rand_vdf_proof,
rewards,
setup_db,
)
from benchmarks.utils import setup_db
from chia.consensus.block_record import BlockRecord
from chia.full_node.block_store import BlockStore
from chia.types.blockchain_format.foliage import Foliage, FoliageBlockData, FoliageTransactionBlock, TransactionsInfo
@ -31,6 +20,17 @@ from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.sub_epoch_summary import SubEpochSummary
from chia.types.full_block import FullBlock
from chia.util.ints import uint8, uint32, uint64, uint128
from tests.util.benchmarks import (
clvm_generator,
rand_bytes,
rand_class_group_element,
rand_g1,
rand_g2,
rand_hash,
rand_vdf,
rand_vdf_proof,
rewards,
)
# to run this benchmark:
# python -m benchmarks.coin_store

View File

@ -8,11 +8,12 @@ from pathlib import Path
from time import monotonic
from typing import List, Tuple
from benchmarks.utils import rand_hash, rewards, setup_db
from benchmarks.utils import setup_db
from chia.full_node.coin_store import CoinStore
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import uint32, uint64
from tests.util.benchmarks import rand_hash, rewards
# to run this benchmark:
# python -m benchmarks.coin_store

View File

@ -10,11 +10,12 @@ from typing import Any, Callable, Dict, List, Optional, TextIO, Tuple, Type, Uni
import click
from benchmarks.utils import EnumType, get_commit_hash, rand_bytes, rand_full_block, rand_hash
from benchmarks.utils import EnumType, get_commit_hash
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.full_block import FullBlock
from chia.util.ints import uint8, uint64
from chia.util.streamable import Streamable, streamable
from tests.util.benchmarks import rand_bytes, rand_full_block, rand_hash
# to run this benchmark:
# python -m benchmarks.streamable

View File

@ -3,36 +3,14 @@ from __future__ import annotations
import contextlib
import enum
import os
import random
import subprocess
import sys
from pathlib import Path
from typing import Any, AsyncIterator, Generic, Optional, Tuple, Type, TypeVar, Union
from typing import Any, AsyncIterator, Generic, Optional, Type, TypeVar, Union
import click
from chia_rs import AugSchemeMPL, G1Element, G2Element
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.types.blockchain_format.classgroup import ClassgroupElement
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.foliage import Foliage, FoliageBlockData, FoliageTransactionBlock, TransactionsInfo
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.proof_of_space import ProofOfSpace
from chia.types.blockchain_format.reward_chain_block import RewardChainBlock
from chia.types.blockchain_format.serialized_program import SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32, bytes100
from chia.types.blockchain_format.vdf import VDFInfo, VDFProof
from chia.types.full_block import FullBlock
from chia.util.db_wrapper import DBWrapper2
from chia.util.ints import uint8, uint32, uint64, uint128
# farmer puzzle hash
ph = bytes32(b"a" * 32)
with open(Path(os.path.realpath(__file__)).parent / "clvm_generator.bin", "rb") as f:
clvm_generator = f.read()
_T_Enum = TypeVar("_T_Enum", bound=enum.Enum)
@ -48,136 +26,6 @@ class EnumType(click.Choice, Generic[_T_Enum]):
return self.__enum(converted_str)
def rewards(height: uint32) -> Tuple[Coin, Coin]:
farmer_coin = create_farmer_coin(height, ph, uint64(250000000), DEFAULT_CONSTANTS.GENESIS_CHALLENGE)
pool_coin = create_pool_coin(height, ph, uint64(1750000000), DEFAULT_CONSTANTS.GENESIS_CHALLENGE)
return farmer_coin, pool_coin
def rand_bytes(num: int) -> bytes:
ret = bytearray(num)
for i in range(num):
ret[i] = random.getrandbits(8)
return bytes(ret)
def rand_hash() -> bytes32:
return bytes32(rand_bytes(32))
def rand_g1() -> G1Element:
sk = AugSchemeMPL.key_gen(rand_bytes(96))
return sk.get_g1()
def rand_g2() -> G2Element:
sk = AugSchemeMPL.key_gen(rand_bytes(96))
return AugSchemeMPL.sign(sk, b"foobar")
def rand_class_group_element() -> ClassgroupElement:
return ClassgroupElement(bytes100(rand_bytes(100)))
def rand_vdf() -> VDFInfo:
return VDFInfo(rand_hash(), uint64(random.randint(100000, 1000000000)), rand_class_group_element())
def rand_vdf_proof() -> VDFProof:
return VDFProof(
uint8(1), # witness_type
rand_hash(), # witness
bool(random.randint(0, 1)), # normalized_to_identity
)
def rand_full_block() -> FullBlock:
proof_of_space = ProofOfSpace(
rand_hash(),
rand_g1(),
None,
rand_g1(),
uint8(0),
rand_bytes(8 * 32),
)
reward_chain_block = RewardChainBlock(
uint128(1),
uint32(2),
uint128(3),
uint8(4),
rand_hash(),
proof_of_space,
None,
rand_g2(),
rand_vdf(),
None,
rand_g2(),
rand_vdf(),
rand_vdf(),
True,
)
pool_target = PoolTarget(
rand_hash(),
uint32(0),
)
foliage_block_data = FoliageBlockData(
rand_hash(),
pool_target,
rand_g2(),
rand_hash(),
rand_hash(),
)
foliage = Foliage(
rand_hash(),
rand_hash(),
foliage_block_data,
rand_g2(),
rand_hash(),
rand_g2(),
)
foliage_transaction_block = FoliageTransactionBlock(
rand_hash(),
uint64(0),
rand_hash(),
rand_hash(),
rand_hash(),
rand_hash(),
)
farmer_coin, pool_coin = rewards(uint32(0))
transactions_info = TransactionsInfo(
rand_hash(),
rand_hash(),
rand_g2(),
uint64(0),
uint64(1),
[farmer_coin, pool_coin],
)
full_block = FullBlock(
[],
reward_chain_block,
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
foliage,
foliage_transaction_block,
transactions_info,
SerializedProgram.from_bytes(clvm_generator),
[],
)
return full_block
@contextlib.asynccontextmanager
async def setup_db(name: Union[str, os.PathLike[str]], db_version: int) -> AsyncIterator[DBWrapper2]:
db_filename = Path(name)

View File

@ -5,12 +5,12 @@ from typing import Dict, List
import pytest
from benchmarks.utils import rand_hash
from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.find_fork_point import find_fork_point_in_chain, lookup_fork_chain
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.ints import uint32
from tests.util.benchmarks import rand_hash
class DummyChain:

View File

@ -2,10 +2,10 @@ from __future__ import annotations
from itertools import permutations
from benchmarks.utils import rand_hash
from chia.types.blockchain_format.coin import hash_coin_ids
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.hash import std_hash
from tests.util.benchmarks import rand_hash
def test_hash_coin_ids_empty() -> None:

View File

@ -8,9 +8,9 @@ from unittest.mock import MagicMock
import pytest
from chia_rs import G1Element
from benchmarks.utils import rand_g1, rand_hash
from chia.pools.pool_wallet import PoolWallet
from chia.types.blockchain_format.sized_bytes import bytes32
from tests.util.benchmarks import rand_g1, rand_hash
@dataclass

155
tests/util/benchmarks.py Normal file
View File

@ -0,0 +1,155 @@
from __future__ import annotations
import random
from typing import Tuple
import pkg_resources
from chia_rs import AugSchemeMPL, ClassgroupElement, Coin, G1Element, G2Element, VDFInfo, VDFProof
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.types.blockchain_format.foliage import Foliage, FoliageBlockData, FoliageTransactionBlock, TransactionsInfo
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.proof_of_space import ProofOfSpace
from chia.types.blockchain_format.reward_chain_block import RewardChainBlock
from chia.types.blockchain_format.serialized_program import SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32, bytes100
from chia.types.full_block import FullBlock
from chia.util.ints import uint8, uint32, uint64, uint128
# farmer puzzle hash
ph = bytes32(b"a" * 32)
clvm_generator_bin_path = pkg_resources.resource_filename(__name__, "clvm_generator.bin")
with open(clvm_generator_bin_path, "rb") as f:
clvm_generator = f.read()
def rewards(height: uint32) -> Tuple[Coin, Coin]:
farmer_coin = create_farmer_coin(height, ph, uint64(250000000), DEFAULT_CONSTANTS.GENESIS_CHALLENGE)
pool_coin = create_pool_coin(height, ph, uint64(1750000000), DEFAULT_CONSTANTS.GENESIS_CHALLENGE)
return farmer_coin, pool_coin
def rand_bytes(num: int) -> bytes:
ret = bytearray(num)
for i in range(num):
ret[i] = random.getrandbits(8)
return bytes(ret)
def rand_hash() -> bytes32:
return bytes32(rand_bytes(32))
def rand_g1() -> G1Element:
sk = AugSchemeMPL.key_gen(rand_bytes(96))
return sk.get_g1()
def rand_g2() -> G2Element:
sk = AugSchemeMPL.key_gen(rand_bytes(96))
return AugSchemeMPL.sign(sk, b"foobar")
def rand_class_group_element() -> ClassgroupElement:
return ClassgroupElement(bytes100(rand_bytes(100)))
def rand_vdf() -> VDFInfo:
return VDFInfo(rand_hash(), uint64(random.randint(100000, 1000000000)), rand_class_group_element())
def rand_vdf_proof() -> VDFProof:
return VDFProof(
uint8(1), # witness_type
rand_hash(), # witness
bool(random.randint(0, 1)), # normalized_to_identity
)
def rand_full_block() -> FullBlock:
proof_of_space = ProofOfSpace(
rand_hash(),
rand_g1(),
None,
rand_g1(),
uint8(0),
rand_bytes(8 * 32),
)
reward_chain_block = RewardChainBlock(
uint128(1),
uint32(2),
uint128(3),
uint8(4),
rand_hash(),
proof_of_space,
None,
rand_g2(),
rand_vdf(),
None,
rand_g2(),
rand_vdf(),
rand_vdf(),
True,
)
pool_target = PoolTarget(
rand_hash(),
uint32(0),
)
foliage_block_data = FoliageBlockData(
rand_hash(),
pool_target,
rand_g2(),
rand_hash(),
rand_hash(),
)
foliage = Foliage(
rand_hash(),
rand_hash(),
foliage_block_data,
rand_g2(),
rand_hash(),
rand_g2(),
)
foliage_transaction_block = FoliageTransactionBlock(
rand_hash(),
uint64(0),
rand_hash(),
rand_hash(),
rand_hash(),
rand_hash(),
)
farmer_coin, pool_coin = rewards(uint32(0))
transactions_info = TransactionsInfo(
rand_hash(),
rand_hash(),
rand_g2(),
uint64(0),
uint64(1),
[farmer_coin, pool_coin],
)
full_block = FullBlock(
[],
reward_chain_block,
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
rand_vdf_proof(),
foliage,
foliage_transaction_block,
transactions_info,
SerializedProgram.from_bytes(clvm_generator),
[],
)
return full_block

View File

@ -6,7 +6,6 @@ from typing import Generator, Iterator, List, Optional
import pytest
from chia_rs import G1Element, G2Element
from benchmarks.utils import rand_bytes, rand_g1, rand_g2, rand_hash, rand_vdf, rand_vdf_proof, rewards
from chia.types.blockchain_format.foliage import Foliage, FoliageBlockData, FoliageTransactionBlock, TransactionsInfo
from chia.types.blockchain_format.pool_target import PoolTarget
from chia.types.blockchain_format.proof_of_space import ProofOfSpace
@ -26,6 +25,7 @@ from chia.types.header_block import HeaderBlock
from chia.util.full_block_utils import block_info_from_block, generator_from_block, header_block_from_block
from chia.util.generator_tools import get_block_header
from chia.util.ints import uint8, uint32, uint64, uint128
from tests.util.benchmarks import rand_bytes, rand_g1, rand_g2, rand_hash, rand_vdf, rand_vdf_proof, rewards
test_g2s: List[G2Element] = [rand_g2() for _ in range(10)]
test_g1s: List[G1Element] = [rand_g1() for _ in range(10)]