import sys
import time
from StcPython import StcPython
stc = StcPython()
stc.log(“INFO”, “Starting Test”)
This line will show the TestCenter commands on stdout
stc.config(“automationoptions”, logto=“stdout”, loglevel=“INFO”)
Retrieve and display the current API version.
print(“SpirentTestCenter system version:\t”, stc.get(“system1”, “version”))
Physical topology
szChassisIp = “10.98.3.12”
ServerAdd = “10.98.30.36”
iTxSlot = 10
iTxPort = 5
iRxSlot = 10
iRxPort = 6
szMacAddrPortA = “00:01:02:03:04:05”
szMacAddrPortB = “22:22:22:22:22:22”
Create the root project object
print(“Creating project …”)
hProject = stc.create(“project”)
Create ports
print(“Creating ports …”)
hPortTx = stc.create(“port”, under=hProject, location="//{0}/{1}/{2}".format(szChassisIp, iTxSlot, iTxPort), useDefaultHost=“False”)
hPortRx = stc.create(“port”, under=hProject, location="//{0}/{1}/{2}".format(szChassisIp, iRxSlot, iRxPort), useDefaultHost=“False”)
Attach ports.
Connect to a chassis
print("Connecting ", szChassisIp)
stc.connect(szChassisIp)
Reserve
print(“Reserving {0}/{1}/{2} and {3}/{4}/{5}”.format(szChassisIp, iTxSlot, iTxPort, szChassisIp, iRxSlot, iRxPort))
stc.reserve("{0}/{1}/{2} {3}/{4}/{5}".format(szChassisIp, iTxSlot, iTxPort, szChassisIp, iRxSlot, iRxPort))
Create the mapping between the physical ports and their logical
representation in the test configuration.
print(“Set up port mappings”)
stc.perform(“SetupPortMappings”)
Apply the configuration.
print(“Apply configuration”)
stc.apply()
Create traffic.
hStreamBlock = stc.create(“streamBlock”, under=hPortA, insertSig=“true”, frameConfig="", frameLengthMode=FIXED, maxFrameLength=1200, FixedFrameLength=128, loadUnit=“BITS_PER_SECOND”, load=100, name=“StreamBlockPortA”)
Add the EthernetII header.
hEthernet =stc.create(“ethernet:EthernetII”, under=hStreamBlock, name=“sb1_eth”, srcMac=szMacAddrPortA, dstMac =szMacAddrPortB)
Add a Vlan container object.
hVlanContainer = stc.create(“vlans”, under=hEthernet)
Add a Vlan header. Note that an undocumented attribute, -name, is required. This provides
a reference for the Modifier object’s -OffsetReference attribute.
hVlan = stc.create(“Vlan”,under =hVlanContainer, pri=000, cfi=0, id=101,name=“vlan1”)
Use modifier to generate multiple streams.
print (“Creating Modifier on Stream Block …\n”)
hRangeModifier = stc.create(“RangeModifier”, under=hStreamBlock, ModifierMode=INCR, Mask=4095, StepValue=1,Data=0,RecycleCount=10,RepeatCount=0,DataType=“NATIVE”,EnableStream=“TRUE”, Offset=0,OffsetReference=“sb1_eth.vlans.vlan1.id”)
Configure generator.
print(“Configuring Generator”)
hGeneratorConfig = stc.get(hGenerator, “children-GeneratorConfig”)
stc.config(hGeneratorConfig,
DurationMode=“BURSTS”,
BurstSize=1,
Duration=100,
LoadMode=“FIXED”,
FixedLoad=100,
LoadUnit=“PERCENT_LINE_RATE”,
SchedulingMode=“PORT_BASED”)
Subscribe to realtime results.
print(“Subscribe to results”)
hAnaResults = stc.subscribe(Parent=hProject,
ConfigType=“Analyzer”,
resulttype=“AnalyzerPortResults”,
filenameprefix=“Analyzer_Port_Results”)
hGenResults = stc.subscribe(Parent=hProject,
ConfigType=“Generator”,
resulttype=“GeneratorPortResults”,
filenameprefix=“Generator_Port_Counter”,
Interval=2)
stc.subscribe(Parent=hProject,
ConfigType=“StreamBlock”,
resulttype=“RxStreamSummaryResults”,
filenameprefix=“RxStreamResults”,
)
print("\nApply configuration")
stc.apply()
Start the analyzer and generator.
print(“Start Analyzer”)
stc.perform(“AnalyzerStart”, AnalyzerList=hProject)
print("Current analyzer state ", stc.get(hAnalyzer, “state”))
print(“Start Generator”)
stc.perform(“GeneratorStart”, GeneratorList=hProject )
print(“Current generator state”, stc.get(hGenerator, “state”))
print(“Wait 2 seconds …”)
stc.sleep(2)
print(“Wait until generator stops …”)
stc.waitUntilComplete(timeout=100)
print("Current analyzer state ", stc.get(hAnalyzer, “state”))
print("Current generator state ", stc.get(hGenerator, “state”))
print(“Stop Analyzer”)
Stop the generator.
stc.perform(“GeneratorStop”, GeneratorList=hGenerator)
Stop the analyzer.
stc.perform(“AnalyzerStop”, AnalyzerList=hAnalyzer)
Display some statistics.
hAnalyzerResults = stc.get(hAnalyzer, “children-AnalyzerPortResults”)
print(“Frames Counts:”, hAnalyzer)
print("\tSignature frames: “, stc.get(hAnalyzerResults, “sigFrameCount”))
print(”\tTotal frames: ", stc.get(hAnalyzerResults, “totalFrameCount”))
Display some statistics.
hAnalyzerResults = stc.get(hAnalyzer2, “children-AnalyzerPortResults”)
print(“Frames Counts:”, hAnalyzer2)
print("\tSignature frames: “, stc.get(hAnalyzerResults, “sigFrameCount”))
print(”\tTotal frames: ", stc.get(hAnalyzerResults, “totalFrameCount”))
Release ports.
print(“Releasing ports …”)
stc.release("{0}/{1}/{2} {3}/{4}/{5}".format(szChassisIp, iTxSlot, iTxPort, szChassisIp, iRxSlot, iRxPort))
Disconnect from a chassis
print(“Disconnect from the chassis …”)
stc.disconnect(szChassisIp)
Delete configuration
print(“Deleting project”)
stc.delete(hProject)
详情见思博伦官网知识库,搜索python
https://support.spirent.com/