@@ -2,6 +2,8 @@ package spec
22
33import (
44 "encoding/json"
5+ "io"
6+ "os"
57 "testing"
68 "time"
79
@@ -10,9 +12,10 @@ import (
1012 openapi_v2 "github.com/googleapis/gnostic/openapiv2"
1113 "github.com/stretchr/testify/assert"
1214 "github.com/stretchr/testify/require"
15+ "google.golang.org/protobuf/proto"
1316)
1417
15- func commonTest (t testing.TB , fuzzer * fuzz.Fuzzer ) {
18+ func gnosticCommonTest (t testing.TB , fuzzer * fuzz.Fuzzer ) {
1619 fuzzer .Funcs (
1720 func (v * * Paths , c fuzz.Continue ) {
1821 // Force paths non-nil since it does not have omitempty in json tasg.
@@ -332,7 +335,7 @@ func commonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
332335}
333336
334337func TestGnosticConversionSmallDeterministic (t * testing.T ) {
335- commonTest (
338+ gnosticCommonTest (
336339 t ,
337340 fuzz .
338341 NewWithSeed (15 ).
@@ -345,7 +348,7 @@ func TestGnosticConversionSmallDeterministic(t *testing.T) {
345348func TestGnosticConversionSmallDeterministic2 (t * testing.T ) {
346349 // A failed case of TestGnosticConversionSmallRandom
347350 // which failed during development/testing loop
348- commonTest (
351+ gnosticCommonTest (
349352 t ,
350353 fuzz .
351354 NewWithSeed (1646770841 ).
@@ -358,7 +361,7 @@ func TestGnosticConversionSmallDeterministic2(t *testing.T) {
358361func TestGnosticConversionSmallDeterministic3 (t * testing.T ) {
359362 // A failed case of TestGnosticConversionSmallRandom
360363 // which failed during development/testing loop
361- commonTest (
364+ gnosticCommonTest (
362365 t ,
363366 fuzz .
364367 NewWithSeed (1646772024 ).
@@ -371,7 +374,7 @@ func TestGnosticConversionSmallDeterministic3(t *testing.T) {
371374func TestGnosticConversionSmallDeterministic4 (t * testing.T ) {
372375 // A failed case of TestGnosticConversionSmallRandom
373376 // which failed during development/testing loop
374- commonTest (
377+ gnosticCommonTest (
375378 t ,
376379 fuzz .
377380 NewWithSeed (1646791953 ).
@@ -386,7 +389,7 @@ func TestGnosticConversionSmallRandom(t *testing.T) {
386389 seed := time .Now ().Unix ()
387390 t .Log ("Using seed: " , seed )
388391
389- commonTest (
392+ gnosticCommonTest (
390393 t ,
391394 fuzz .
392395 NewWithSeed (seed ).
@@ -397,7 +400,7 @@ func TestGnosticConversionSmallRandom(t *testing.T) {
397400}
398401
399402func TestGnosticConversionMediumDeterministic (t * testing.T ) {
400- commonTest (
403+ gnosticCommonTest (
401404 t ,
402405 fuzz .
403406 NewWithSeed (15 ).
@@ -408,7 +411,7 @@ func TestGnosticConversionMediumDeterministic(t *testing.T) {
408411}
409412
410413func TestGnosticConversionLargeDeterministic (t * testing.T ) {
411- commonTest (
414+ gnosticCommonTest (
412415 t ,
413416 fuzz .
414417 NewWithSeed (15 ).
@@ -417,3 +420,82 @@ func TestGnosticConversionLargeDeterministic(t *testing.T) {
417420 NumElements (3 , 5 ),
418421 )
419422}
423+
424+
425+ func BenchmarkGnosticConversion (b * testing.B ) {
426+ // Download kube-openapi swagger json
427+ swagFile , err := os .Open ("../../schemaconv/testdata/swagger.json" )
428+ if err != nil {
429+ b .Fatal (err )
430+ }
431+ defer swagFile .Close ()
432+
433+ originalJSON , err := io .ReadAll (swagFile )
434+ if err != nil {
435+ b .Fatal (err )
436+ }
437+
438+ // Parse into kube-openapi types
439+ var result * Swagger
440+ b .Run ("json->swagger" , func (b2 * testing.B ) {
441+ for i := 0 ; i < b2 .N ; i ++ {
442+ if err := json .Unmarshal (originalJSON , & result ); err != nil {
443+ b2 .Fatal (err )
444+ }
445+ }
446+ })
447+
448+ // Convert to JSON
449+ var encodedJSON []byte
450+ b .Run ("swagger->json" , func (b2 * testing.B ) {
451+ for i := 0 ; i < b2 .N ; i ++ {
452+ encodedJSON , err = json .Marshal (result )
453+ if err != nil {
454+ b2 .Fatal (err )
455+ }
456+ }
457+ })
458+
459+ // Convert to gnostic
460+ var originalGnostic * openapi_v2.Document
461+ b .Run ("json->gnostic" , func (b2 * testing.B ) {
462+ for i := 0 ; i < b2 .N ; i ++ {
463+ originalGnostic , err = openapi_v2 .ParseDocument (encodedJSON )
464+ if err != nil {
465+ b2 .Fatal (err )
466+ }
467+ }
468+ })
469+
470+ // Convert to PB
471+ var encodedProto []byte
472+ b .Run ("gnostic->pb" , func (b2 * testing.B ) {
473+ for i := 0 ; i < b2 .N ; i ++ {
474+ encodedProto , err = proto .Marshal (originalGnostic )
475+ if err != nil {
476+ b2 .Fatal (err )
477+ }
478+ }
479+ })
480+
481+ // Convert to gnostic
482+ var backToGnostic openapi_v2.Document
483+ b .Run ("pb->gnostic" , func (b2 * testing.B ) {
484+ for i := 0 ; i < b2 .N ; i ++ {
485+ if err := proto .Unmarshal (encodedProto , & backToGnostic ); err != nil {
486+ b2 .Fatal (err )
487+ }
488+ }
489+ })
490+
491+ for i := 0 ; i < b .N ; i ++ {
492+ b .Run ("gnostic->kube" , func (b2 * testing.B ) {
493+ for i := 0 ; i < b2 .N ; i ++ {
494+ decodedSwagger := & Swagger {}
495+ if err := decodedSwagger .FromGnostic (& backToGnostic ); err != nil {
496+ b2 .Fatal (err )
497+ }
498+ }
499+ })
500+ }
501+ }
0 commit comments