Skip to content

Commit

Permalink
add benchmark for gnostic conversion with swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zielenski committed Mar 9, 2022
1 parent 0e8d80a commit 1aeb6d8
Showing 1 changed file with 96 additions and 8 deletions.
104 changes: 96 additions & 8 deletions pkg/validation/spec/gnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spec

import (
"encoding/json"
"io"
"os"
"testing"
"time"

Expand All @@ -10,9 +12,10 @@ import (
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func commonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
func gnosticCommonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
fuzzer.Funcs(
func (v **Paths, c fuzz.Continue) {
// Force paths non-nil since it does not have omitempty in json tasg.
Expand Down Expand Up @@ -332,7 +335,7 @@ func commonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
}

func TestGnosticConversionSmallDeterministic(t *testing.T) {
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(15).
Expand All @@ -345,7 +348,7 @@ func TestGnosticConversionSmallDeterministic(t *testing.T) {
func TestGnosticConversionSmallDeterministic2(t *testing.T) {
// A failed case of TestGnosticConversionSmallRandom
// which failed during development/testing loop
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(1646770841).
Expand All @@ -358,7 +361,7 @@ func TestGnosticConversionSmallDeterministic2(t *testing.T) {
func TestGnosticConversionSmallDeterministic3(t *testing.T) {
// A failed case of TestGnosticConversionSmallRandom
// which failed during development/testing loop
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(1646772024).
Expand All @@ -371,7 +374,7 @@ func TestGnosticConversionSmallDeterministic3(t *testing.T) {
func TestGnosticConversionSmallDeterministic4(t *testing.T) {
// A failed case of TestGnosticConversionSmallRandom
// which failed during development/testing loop
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(1646791953).
Expand All @@ -386,7 +389,7 @@ func TestGnosticConversionSmallRandom(t *testing.T) {
seed := time.Now().Unix()
t.Log("Using seed: ", seed)

commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(seed).
Expand All @@ -397,7 +400,7 @@ func TestGnosticConversionSmallRandom(t *testing.T) {
}

func TestGnosticConversionMediumDeterministic(t *testing.T) {
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(15).
Expand All @@ -408,7 +411,7 @@ func TestGnosticConversionMediumDeterministic(t *testing.T) {
}

func TestGnosticConversionLargeDeterministic(t *testing.T) {
commonTest(
gnosticCommonTest(
t,
fuzz.
NewWithSeed(15).
Expand All @@ -417,3 +420,88 @@ func TestGnosticConversionLargeDeterministic(t *testing.T) {
NumElements(3, 5),
)
}


func loadSwagger(b *testing.B) (*Swagger, *openapi_v2.Document) {
// Download kube-openapi swagger json
swagFile, err := os.Open("../../schemaconv/testdata/swagger.json")
if err != nil {
b.Fatal(err)
}
defer swagFile.Close()

originalJSON, err := io.ReadAll(swagFile)
if err != nil {
b.Fatal(err)
}

// Parse into kube-openapi types
var result *Swagger
b.Run("json->swagger", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
if err := json.Unmarshal(originalJSON, &result); err != nil {
b2.Fatal(err)
}
}
})

// Convert to JSON
var encodedJSON []byte
b.Run("swagger->json", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
encodedJSON, err = json.Marshal(result)
if err != nil {
b2.Fatal(err)
}
}
})

// Convert to gnostic
var originalGnostic *openapi_v2.Document
b.Run("json->gnostic", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
originalGnostic, err = openapi_v2.ParseDocument(encodedJSON)
if err != nil {
b2.Fatal(err)
}
}
})

// Convert to PB
var encodedProto []byte
b.Run("gnostic->pb", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
encodedProto, err = proto.Marshal(originalGnostic)
if err != nil {
b2.Fatal(err)
}
}
})

// Convert to gnostic
var backToGnostic openapi_v2.Document
b.Run("pb->gnostic", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
if err := proto.Unmarshal(encodedProto, &backToGnostic); err != nil {
b2.Fatal(err)
}
}
})

return result, &backToGnostic
}

func BenchmarkGnosticConversion(b *testing.B) {
_, gnsot := loadSwagger(b)

for i := 0; i < b.N; i++ {
b.Run("gnostic->kube", func(b2 *testing.B) {
for i := 0; i < b2.N; i++ {
decodedSwagger := &Swagger{}
if err := decodedSwagger.FromGnostic(gnsot); err != nil {
b2.Fatal(err)
}
}
})
}
}

0 comments on commit 1aeb6d8

Please sign in to comment.