Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion arazzo/arazzo_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"strings"
"testing"

libopenapi "github.com/pb33f/libopenapi"
"github.com/pb33f/go-yaml"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/arazzo/expression"
high "github.com/pb33f/libopenapi/datamodel/high/arazzo"
)

func largeArazzoDocument(stepCount, outputCount int) []byte {
Expand Down Expand Up @@ -82,3 +86,70 @@ func BenchmarkRenderLargeArazzoSelectorMap(b *testing.B) {
}
}
}

func BenchmarkSimpleCriterion(b *testing.B) {
ctx := &expression.Context{StatusCode: 200, Method: "POST"}
for _, condition := range []string{
"true",
"$statusCode == 200",
"$method == 'POST'",
"$statusCode == 200 && ($method == 'POST' || $method == 'PUT')",
} {
b.Run(condition, func(b *testing.B) {
caches := newCriterionCaches()
exprCache := map[string]expression.Expression{}
caches.parseExpr = func(s string) (expression.Expression, error) {
if e, ok := exprCache[s]; ok {
return e, nil
}
e, err := expression.Parse(s)
if err == nil {
exprCache[s] = e
}
return e, err
}
criterion := &high.Criterion{Condition: condition}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := evaluateCriterionImpl(criterion, ctx, caches); err != nil {
b.Fatal(err)
}
}
})
}
}

// BenchmarkEvaluateCriterionPublic measures the public, uncached
// EvaluateCriterion entry point. Every call pays for parsing the condition, so
// this is the benchmark that tracks per-call compile cost and allocations.
// Compare against main with:
//
// go test -run xxx -bench BenchmarkEvaluateCriterionPublic -benchmem -count 12 ./arazzo > new.txt
// (same on main > old.txt) && benchstat old.txt new.txt
func BenchmarkEvaluateCriterionPublic(b *testing.B) {
var body yaml.Node
if err := yaml.Unmarshal([]byte(`{"status": "ok"}`), &body); err != nil {
b.Fatal(err)
}
ctx := &expression.Context{StatusCode: 200, Method: "POST", ResponseBody: body.Content[0]}

for _, tc := range []struct {
name, condition string
}{
{"status_code", "$statusCode == 200"},
{"response_body", "$response.body#/status == 'ok'"},
{"compound", "$statusCode == 200 && ($method == 'POST' || $method == 'PUT')"},
} {
b.Run(tc.name, func(b *testing.B) {
criterion := &high.Criterion{Condition: tc.condition}
if ok, err := EvaluateCriterion(criterion, ctx); err != nil || !ok {
b.Fatalf("condition %q: got (%v, %v), want (true, nil)", tc.condition, ok, err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = EvaluateCriterion(criterion, ctx)
}
})
}
}
109 changes: 41 additions & 68 deletions arazzo/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,74 +346,47 @@ func TestSplitSimpleCondition_BareExpressionNoOperator(t *testing.T) {
}

// ---------------------------------------------------------------------------
// evaluateSimpleOperand
// ---------------------------------------------------------------------------

func TestEvaluateSimpleOperand_EmptyString(t *testing.T) {
val, err := evaluateSimpleOperand("", nil, nil)
require.NoError(t, err)
assert.Equal(t, "", val)
}

func TestEvaluateSimpleOperand_ExpressionPrefix(t *testing.T) {
ctx := &expression.Context{StatusCode: 200}
val, err := evaluateSimpleOperand("$statusCode", ctx, nil)
require.NoError(t, err)
assert.Equal(t, 200, val)
}

func TestEvaluateSimpleOperand_DoubleQuotedString(t *testing.T) {
val, err := evaluateSimpleOperand("\"hello\"", nil, nil)
require.NoError(t, err)
assert.Equal(t, "hello", val)
}

func TestEvaluateSimpleOperand_SingleQuotedString(t *testing.T) {
val, err := evaluateSimpleOperand("'world'", nil, nil)
require.NoError(t, err)
assert.Equal(t, "world", val)
}

func TestEvaluateSimpleOperand_BooleanTrue(t *testing.T) {
val, err := evaluateSimpleOperand("true", nil, nil)
require.NoError(t, err)
assert.Equal(t, true, val)
}

func TestEvaluateSimpleOperand_BooleanFalse(t *testing.T) {
val, err := evaluateSimpleOperand("false", nil, nil)
require.NoError(t, err)
assert.Equal(t, false, val)
}

func TestEvaluateSimpleOperand_Integer(t *testing.T) {
val, err := evaluateSimpleOperand("42", nil, nil)
require.NoError(t, err)
assert.Equal(t, int64(42), val)
}

func TestEvaluateSimpleOperand_NegativeInteger(t *testing.T) {
val, err := evaluateSimpleOperand("-5", nil, nil)
require.NoError(t, err)
assert.Equal(t, int64(-5), val)
}

func TestEvaluateSimpleOperand_Float(t *testing.T) {
val, err := evaluateSimpleOperand("3.14", nil, nil)
require.NoError(t, err)
assert.Equal(t, 3.14, val)
}

func TestEvaluateSimpleOperand_PlainString(t *testing.T) {
val, err := evaluateSimpleOperand("hello", nil, nil)
require.NoError(t, err)
assert.Equal(t, "hello", val)
}

func TestEvaluateSimpleOperand_WhitespaceTrimmmed(t *testing.T) {
val, err := evaluateSimpleOperand(" 42 ", nil, nil)
require.NoError(t, err)
assert.Equal(t, int64(42), val)
// simpleLiteral
// ---------------------------------------------------------------------------

func TestSimpleLiteral(t *testing.T) {
tests := []struct {
name string
input string
want any
}{
{"double quoted string", `"hello"`, "hello"},
{"single quoted string", `'world'`, "world"},
{"quoted empty string", `''`, ""},
{"quoted keeps inner whitespace", `' a '`, " a "},
{"lone quote is plain text", `"`, `"`},
{"mismatched quotes are plain text", `"a'`, `"a'`},
{"true", "true", true},
{"false", "false", false},
{"integer", "42", int64(42)},
{"negative integer", "-5", int64(-5)},
{"float", "3.14", 3.14},
{"plain string", "hello", "hello"},
{"empty", "", ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, simpleLiteral(tc.input))
})
}
}

// Operand text is trimmed once, when the node is built, before it is classified.
func TestInitOperandNode_TrimsAndClassifies(t *testing.T) {
var n conditionNode
initOperandNode(&n, " 42 ")
assert.False(t, n.isExpr)
assert.Equal(t, int64(42), n.literal)

initOperandNode(&n, " $statusCode ")
assert.True(t, n.isExpr)
assert.Equal(t, "$statusCode", n.raw)
assert.Nil(t, n.literal)
}

// ---------------------------------------------------------------------------
Expand Down
Loading
Loading