Skip to content

Commit

Permalink
perf: add benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geddy11 committed Dec 22, 2024
1 parent 1836349 commit 0ba69e6
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 25 deletions.
12 changes: 1 addition & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ pillow = ">=9.3.0"
[tool.poetry.dev-dependencies]
pytest = "^8.0"
pytest-cov = "^4.0"
matplotlib = "^3.0"
jupyter-book = "^1.0.0"
toml = ">=0.10.2"
pandas = "^2.0"
tqdm = ">=4.63"
packaging = ">=21.1"
pydot = ">=3.0.0"
pillow = ">=9.3.0"
pytest-benchmark = "^4.0"

[tool.poetry.group.docs]
optional = true
Expand Down Expand Up @@ -76,7 +69,4 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
#addopts = [
# "--import-mode=importlib",
#]
pythonpath = "src"
1 change: 1 addition & 0 deletions src/sysloss/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def from_file(cls, fname: str):
)
# add sources/pmux
for e in range(1, len(entires)):
vo = 0.0
if sys[entires[e]]["type"] == "SOURCE":
vo = _get_mand(sys[entires[e]]["params"], "vo")
rs = _get_opt(sys[entires[e]]["params"], "rs", RS_DEFAULT)
Expand Down
85 changes: 85 additions & 0 deletions tests/benchmark/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# MIT License
#
# Copyright (c) 2024, Geir Drange
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest

from sysloss.system import System
from sysloss.components import *


@pytest.fixture(scope="class")
def sys1():
"""System 1 test fixture"""
sys = System("sys1_fixture", Source("20V", vo=20))
sys.add_source(Source("-24V", vo=-24, rs=0.05))
sys.add_comp("-24V", comp=Rectifier("D-bridge", vdrop=0.3))
sys.add_comp("20V", comp=Rectifier("M-bridge", rs=0.017, iq=1e-3, ig=2e-3))
sys.add_comp(["D-bridge", "M-bridge"], comp=PMux("Pmux", rs=0.01), rail="Vsys")
d1 = {"vi": [3.3], "io": [0.1, 0.5, 0.9], "vdrop": [[0.55, 0.78, 0.92]]}
sys.add_comp("Vsys", comp=VLoss("Diode 1", vdrop=d1))
sys.add_comp("Diode 1", comp=RLoss("R1", rs=1.247))
sys.add_comp("R1", comp=ILoad("I-load 1", ii=0.125))
e2 = {
"vi": [3.3, 15.0, 48.0],
"io": [0.1, 0.5, 0.9],
"eff": [[0.55, 0.78, 0.92], [0.5, 0.74, 0.83], [0.4, 0.6, 0.766]],
}
sys.add_comp("Vsys", comp=Converter("Buck 5.0", vo=5.0, eff=e2))
igdata = {
"vi": [15, 50],
"io": [0.0, 0.01, 0.02, 0.1],
"ig": [[0.12e-6, 0.51e-3, 1.52e-3, 2.3e-3], [1e-6, 1e-3, 2e-3, 3e-3]],
}
sys.add_comp("Buck 5.0", comp=LinReg("LDO", vo=2.5, ig=igdata))
sys.add_comp("LDO", comp=PLoad("Load 2", pwr=0.33))
sys.add_comp("Vsys", comp=RLoad("Load 3", rs=1234))
sys.add_comp("Vsys", comp=PSwitch("Sw1", rs=0.077))
sys.add_comp("Sw1", comp=RLoss("R2", rs=10.247))
sys.add_comp("R2", comp=PLoad("Load 4", pwr=0.099))
sys.add_comp("R2", comp=RLoss("R3", rs=20.33))
sys.add_comp("R3", comp=PLoad("Load 5", pwr=0.33))
sys.add_comp("R3", comp=RLoss("R4", rs=15))
sys.add_comp("R4", comp=PLoad("Load 6", pwr=1))
sys_phases = {"one": 1, "two": 1}
sys.set_sys_phases(sys_phases)
sys.set_comp_phases("20V", phase_conf=["one"])
sys.set_comp_phases("-24V", phase_conf=["two"])
return sys


@pytest.fixture(scope="class")
def sys2():
"""System 2 test fixture"""
sdc = System("Sensor Daisy chain", Source("Li-ion", vo=14.4, rs=0.2))
sdc.add_comp("Li-ion", comp=Converter("Boost 48V", vo=48.0, eff=0.88))
parent = "Boost 48V"
for i in range(1, 17, 1):
idx = " [{}]".format(i)
sdc.add_comp(parent, comp=RLoss("Twisted pair" + idx, rs=4.3))
sdc.add_comp("Twisted pair" + idx, comp=VLoss("Diode" + idx, vdrop=0.54))
sdc.add_comp(
"Diode" + idx, comp=PSwitch("Power switch" + idx, rs=0.03, ig=10e-6)
)
sdc.add_comp("Diode" + idx, comp=Converter("Buck 3.3V" + idx, vo=3.3, eff=0.72))
sdc.add_comp("Buck 3.3V" + idx, comp=PLoad("Sensor unit" + idx, pwr=0.35))
parent = "Power switch" + idx
return sdc
45 changes: 45 additions & 0 deletions tests/benchmark/test_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# MIT License
#
# Copyright (c) 2024, Geir Drange
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest
from sysloss.system import System
from sysloss.components import *


def test_sys1(sys1: System, benchmark):
"""Benchmark 1"""

def run1():
sys1.solve()

benchmark(run1)
assert benchmark.stats["max"] < 0.1


def test_sys2(sys2: System, benchmark):
"""Benchmark 2"""

def run2():
sys2.solve()

benchmark(run2)
assert benchmark.stats["max"] < 0.5
6 changes: 3 additions & 3 deletions tests/unit/test_comp_arith.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,9 @@ def rectifier_d(name, vdrop, rt):
return Rectifier(
name,
vdrop=vdrop,
rs=random.uniform(0.0, 4.3),
ig=random.uniform(0.0, 1.2e-3),
iq=random.uniform(0.0, 1.5e-4),
rs=random.SystemRandom().uniform(0.0, 4.3),
ig=random.SystemRandom().uniform(0.0, 1.2e-3),
iq=random.SystemRandom().uniform(0.0, 1.5e-4),
rt=rt,
)

Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,17 @@ def test_pmux():
assert pa._limits == pc._limits, "PMux limits from file"
pd = PMux(
"P-mux",
rs=[0.33, 0.42],
rs=[0.31, 0.47],
ig={
"vi": [1.9, 5.8, 12.5],
"io": [0.002, 0.02, 0.2],
"vi": [2.3, 4.7, 11.8],
"io": [0.001, 0.015, 0.22],
"ig": [
[3.0e-6, 3.0e-6, 4.0e-6],
[9.0e-6, 9.0e-6, 99.0e-6],
[56.0e-6, 67.0e-6, 77.0e-6],
[3.1e-5, 3.2e-5, 4.2e-5],
[7.8e-5, 8.8e-5, 97.0e-5],
[52.0e-5, 62.0e-5, 72.0e-5],
],
},
iis=0.57e-6,
iis=0.67e-5,
)
with pytest.raises(ValueError):
pd._solv_outp_volt(
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def test_case5b():
with pytest.raises(ValueError):
case5b.add_comp("5V system", comp=LinReg("LDO 3.3", vo=3.3, ig=igdata))
igdata = {
"vi": [5.0],
"io": [0.0, 0.4, 0.6, 0.8],
"ig": [[-0.3, -0.4, -0.67, -0.89]],
"vi": [4.5],
"io": [0.0, 0.3, 0.55, 0.75],
"ig": [[-0.2, -0.3, -0.5, -0.78]],
}
with pytest.raises(ValueError):
case5b.add_comp("5V system", comp=LinReg("LDO 3.3", vo=3.3, ig=igdata))
Expand Down Expand Up @@ -192,7 +192,11 @@ def test_case5b():
def test_case5c():
"""PSwitch with invalid interpolation parameter"""
case5c = System("Case5c system", Source("5V system", vo=5.0))
igdata = {"vi": [5.0], "io": [0.1, 0.4, 0.6, 0.5], "ig": [[0.3, 0.4, 0.67, 0.89]]}
igdata = {
"vi": [3.3],
"io": [0.11, 0.41, 0.61, 0.51],
"ig": [[0.3, 0.4, 0.67, 0.89]],
}
with pytest.raises(ValueError):
case5c.add_comp("5V system", comp=PSwitch("PSwitch", rs=3.3, ig=igdata))
igdata = {
Expand Down

0 comments on commit 0ba69e6

Please sign in to comment.