FunTester Byteman 使用指南(十)

FunTester · 2025年02月14日 · 1407 次阅读

跟踪和调试操作

1. 调试(Debug)

规则引擎提供了一个简单的内置 debug 方法,用于在规则执行期间有条件地显示消息。帮助器类定义的 API 如下:

public boolean debug(String message)
  • debug:将提供的消息打印到 System.out,并以正在执行的规则名称作为前缀。此方法始终返回 true,允许调试消息在条件表达式中通过 AND 与其他布尔表达式结合使用。

通过在 JVM 命令行上设置以下系统属性,可控制调试消息的生成:

-Dorg.jboss.byteman.debug=true

2. 跟踪(Trace)

规则引擎提供了一组内置方法,用于在执行期间记录跟踪消息。消息可以输出到 System.outSystem.err 或指定文件。帮助器类定义的 API 如下:

public boolean traceOpen(Object identifier, String filename)
public boolean traceOpen(Object identifier)
public boolean traceClose(Object identifier)
public boolean trace(Object identifier, String message)
public boolean traceln(Object identifier, String message)
public boolean trace(String message)
public boolean traceln(String message)
  • traceOpen:打开由 filename 指定的文件,将其与 identifier 关联,并返回 truefilename 可以是相对路径或绝对路径。相对路径文件位于 JVM 的当前工作目录下。如果已有文件与 identifier 关联,traceOpen 将立即返回 false;若文件已存在,则以追加模式打开;若未提供 filename,将自动生成一个唯一的文件名。
  • traceClose:关闭与 identifier 关联的文件并解除关联,返回 true;若未找到关联文件,则返回 false
  • trace:将消息打印到与 identifier 关联的文件,并返回 true;若无关联文件,将自动创建一个文件并完成关联。
  • traceln:与 trace 类似,但会在消息后附加一个换行符。
  • identifier 被省略或为 null,消息将写入 System.out;若为字符串 "err",消息将写入 System.err

特殊情况

  • identifiernull"out" 时,tracetraceln 会输出到 System.out;若为 "err",则输出到 System.err。对于这些特殊值,traceOpentraceClose 始终返回 false

堆栈管理操作

1. 检查调用树

规则引擎提供了一组内置方法,用于检查规则触发时的调用栈。帮助器类定义的 API 如下:

public boolean callerEquals(String name)
public boolean callerEquals(String name, int frameCount)
public boolean callerEquals(String name, int startFrame, int frameCount)
public boolean callerEquals(String name, boolean includeClass)
public boolean callerEquals(String name, boolean includeClass, int frameCount)
public boolean callerEquals(String name, boolean includeClass, int startFrame, int frameCount)
public boolean callerEquals(String name, boolean includeClass, boolean includePackage)
public boolean callerEquals(String name, boolean includeClass, boolean includePackage, int frameCount)
public boolean callerEquals(String name, boolean includeClass, boolean includePackage, int startFrame, int frameCount)

public boolean callerMatches(String regExp)
public boolean callerMatches(String regExp, int frameCount)
public boolean callerMatches(String regExp, int startFrame, int frameCount)
public boolean callerMatches(String regExp, boolean includeClass)
public boolean callerMatches(String regExp, boolean includeClass, int frameCount)
public boolean callerMatches(String regExp, boolean includeClass, int startFrame, int frameCount)
public boolean callerMatches(String regExp, boolean includeClass, boolean includePackage)
public boolean callerMatches(String regExp, boolean includeClass, boolean includePackage, int frameCount)
public boolean callerMatches(String regExp, boolean includeClass, int startFrame, int frameCount)

public boolean callerCheck(String match, boolean isRegExp, boolean includeClass, boolean includePackage, int startFrame, int frameCount)
  • callerCheck:检查从 startFrame 开始的调用栈帧(共 frameCount 个),若任意帧匹配则返回 true
    • startFrame:默认为 1,表示触发方法的调用者栈帧(0 表示触发方法本身)。
    • frameCount:默认为 1,即仅检查触发方法调用者的栈帧。
    • includeClassincludePackage:若为 false,仅比较裸方法名;若为 true,则分别比较类限定名或完全限定名。
    • isRegExp:若为 true,使用正则表达式匹配;否则使用字符串精确匹配。

2. 跟踪调用栈

规则引擎提供了一组方法,用于记录或格式化调用栈的字符串表示。API 定义如下:

public void traceStack()
public void traceStack(String prefix)
public void traceStack(String prefix, Object key)
public void traceStack(int maxFrames)
public void traceStack(String prefix, int maxFrames)
public void traceStack(String prefix, Object key, int maxFrames)

public String formatStack()
public String formatStack(String prefix)
public String formatStack(int maxFrames)
public String formatStack(String prefix, int maxFrames)
  • formatStack:生成调用栈的字符串表示,包括完全限定的方法名、文件及行号,按行分隔。
  • traceStack:调用 formatStack 生成调用栈字符串,并将其写入跟踪文件。

使用正则表达式可筛选部分栈帧,相关方法如下:

public void traceStackMatching(String regExp)
public void traceStackMatching(String regExp, String prefix)
public void traceStackMatching(String regExp, boolean includeClass, ...)
public String formatStackMatching(String regExp, boolean includeClass, ...)

3. 跟踪线程调用栈

支持记录特定线程或所有线程的调用栈:

public void traceThreadStack(String threadName, ...)
public void traceAllStacks(String prefix, ...)

默认帮助器生命周期方法

默认帮助器提供了四个生命周期方法,在 System.out 中生成调试消息。例如:

Default helper activated
Installed rule using default helper : my test rule
Uninstalled rule using default helper : my test rule
Default helper deactivated

通过这些方法,开发者可以有效管理和调试规则的执行过程,确保规则的正确性和高效性。

FunTester 原创精华

【连载】从 Java 开始性能测试

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册