|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/amzn/ion-go/ion" |
| 10 | + "github.com/danielgtaylor/restish/cli" |
| 11 | + "github.com/danielgtaylor/restish/openapi" |
| 12 | + "github.com/fxamacker/cbor/v2" |
| 13 | + "github.com/shamaton/msgpack/v2" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func BenchmarkFormats(b *testing.B) { |
| 18 | + inputs := []struct { |
| 19 | + Name string |
| 20 | + URL string |
| 21 | + }{ |
| 22 | + { |
| 23 | + Name: "small", |
| 24 | + URL: "https://github.com/OAI/OpenAPI-Specification/raw/d1cc440056f1c7bb913bcd643b15c14ee1c409f4/examples/v3.0/uspto.json", |
| 25 | + }, |
| 26 | + { |
| 27 | + Name: "large", |
| 28 | + URL: "https://github.com/github/rest-api-description/blob/83cdec7384b62ef6f54bad60270544d6fc6f22cd/descriptions/api.github.com/api.github.com.json?raw=true", |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + cli.Init("benchmark", "1.0.0") |
| 33 | + cli.Defaults() |
| 34 | + cli.AddLoader(openapi.New()) |
| 35 | + |
| 36 | + for _, t := range inputs { |
| 37 | + resp, err := http.Get(t.URL) |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + if resp.StatusCode >= 300 { |
| 42 | + panic("non-success status from server, check URLs are still working") |
| 43 | + } |
| 44 | + |
| 45 | + dummy := &cobra.Command{} |
| 46 | + doc, err := cli.Load(t.URL, dummy) |
| 47 | + if err != nil { |
| 48 | + panic(err) |
| 49 | + } |
| 50 | + |
| 51 | + dataJSON, err := json.Marshal(doc) |
| 52 | + if err != nil { |
| 53 | + panic(err) |
| 54 | + } |
| 55 | + |
| 56 | + dataCBOR, err := cbor.Marshal(doc) |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + |
| 61 | + dataMsgPack, err := msgpack.Marshal(doc) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + dataIon, err := ion.MarshalBinary(doc) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Printf("json: %d\ncbor: %d\nmsgp: %d\n ion: %d\n", len(dataJSON), len(dataCBOR), len(dataMsgPack), len(dataIon)) |
| 72 | + |
| 73 | + b.Run(t.Name+"-json-marshal", func(b *testing.B) { |
| 74 | + b.ReportAllocs() |
| 75 | + for n := 0; n < b.N; n++ { |
| 76 | + json.Marshal(doc) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + b.Run(t.Name+"-json-unmarshal", func(b *testing.B) { |
| 81 | + b.ReportAllocs() |
| 82 | + for n := 0; n < b.N; n++ { |
| 83 | + var tmp cli.API |
| 84 | + json.Unmarshal(dataJSON, &tmp) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + b.Run(t.Name+"-cbor-marshal", func(b *testing.B) { |
| 89 | + b.ReportAllocs() |
| 90 | + for n := 0; n < b.N; n++ { |
| 91 | + cbor.Marshal(doc) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + b.Run(t.Name+"-cbor-unmarshal", func(b *testing.B) { |
| 96 | + b.ReportAllocs() |
| 97 | + for n := 0; n < b.N; n++ { |
| 98 | + var tmp cli.API |
| 99 | + cbor.Unmarshal(dataCBOR, &tmp) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + b.Run(t.Name+"-msgpack-marshal", func(b *testing.B) { |
| 104 | + b.ReportAllocs() |
| 105 | + for n := 0; n < b.N; n++ { |
| 106 | + msgpack.Marshal(doc) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + b.Run(t.Name+"-msgpack-unmarshal", func(b *testing.B) { |
| 111 | + b.ReportAllocs() |
| 112 | + for n := 0; n < b.N; n++ { |
| 113 | + var tmp cli.API |
| 114 | + msgpack.Unmarshal(dataMsgPack, &tmp) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + b.Run(t.Name+"-ion-marshal", func(b *testing.B) { |
| 119 | + b.ReportAllocs() |
| 120 | + for n := 0; n < b.N; n++ { |
| 121 | + ion.MarshalBinary(doc) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + b.Run(t.Name+"-ion-unmarshal", func(b *testing.B) { |
| 126 | + b.ReportAllocs() |
| 127 | + for n := 0; n < b.N; n++ { |
| 128 | + var tmp cli.API |
| 129 | + ion.Unmarshal(dataIon, &tmp) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments