...

Source file src/github.com/go-openapi/runtime/middleware/denco/router_bench_test.go

Documentation: github.com/go-openapi/runtime/middleware/denco

     1  package denco_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"fmt"
     7  	"math/big"
     8  	"testing"
     9  
    10  	"github.com/go-openapi/runtime/middleware/denco"
    11  )
    12  
    13  func BenchmarkRouterLookupStatic100(b *testing.B) {
    14  	benchmarkRouterLookupStatic(b, 100)
    15  }
    16  
    17  func BenchmarkRouterLookupStatic300(b *testing.B) {
    18  	benchmarkRouterLookupStatic(b, 300)
    19  }
    20  
    21  func BenchmarkRouterLookupStatic700(b *testing.B) {
    22  	benchmarkRouterLookupStatic(b, 700)
    23  }
    24  
    25  func BenchmarkRouterLookupSingleParam100(b *testing.B) {
    26  	records := makeTestSingleParamRecords(100)
    27  	benchmarkRouterLookupSingleParam(b, records)
    28  }
    29  
    30  func BenchmarkRouterLookupSingleParam300(b *testing.B) {
    31  	records := makeTestSingleParamRecords(300)
    32  	benchmarkRouterLookupSingleParam(b, records)
    33  }
    34  
    35  func BenchmarkRouterLookupSingleParam700(b *testing.B) {
    36  	records := makeTestSingleParamRecords(700)
    37  	benchmarkRouterLookupSingleParam(b, records)
    38  }
    39  
    40  func BenchmarkRouterLookupSingle2Param100(b *testing.B) {
    41  	records := makeTestSingle2ParamRecords(100)
    42  	benchmarkRouterLookupSingleParam(b, records)
    43  }
    44  
    45  func BenchmarkRouterLookupSingle2Param300(b *testing.B) {
    46  	records := makeTestSingle2ParamRecords(300)
    47  	benchmarkRouterLookupSingleParam(b, records)
    48  }
    49  
    50  func BenchmarkRouterLookupSingle2Param700(b *testing.B) {
    51  	records := makeTestSingle2ParamRecords(700)
    52  	benchmarkRouterLookupSingleParam(b, records)
    53  }
    54  
    55  func BenchmarkRouterBuildStatic100(b *testing.B) {
    56  	records := makeTestStaticRecords(100)
    57  	benchmarkRouterBuild(b, records)
    58  }
    59  
    60  func BenchmarkRouterBuildStatic300(b *testing.B) {
    61  	records := makeTestStaticRecords(300)
    62  	benchmarkRouterBuild(b, records)
    63  }
    64  
    65  func BenchmarkRouterBuildStatic700(b *testing.B) {
    66  	records := makeTestStaticRecords(700)
    67  	benchmarkRouterBuild(b, records)
    68  }
    69  
    70  func BenchmarkRouterBuildSingleParam100(b *testing.B) {
    71  	records := makeTestSingleParamRecords(100)
    72  	benchmarkRouterBuild(b, records)
    73  }
    74  
    75  func BenchmarkRouterBuildSingleParam300(b *testing.B) {
    76  	records := makeTestSingleParamRecords(300)
    77  	benchmarkRouterBuild(b, records)
    78  }
    79  
    80  func BenchmarkRouterBuildSingleParam700(b *testing.B) {
    81  	records := makeTestSingleParamRecords(700)
    82  	benchmarkRouterBuild(b, records)
    83  }
    84  
    85  func BenchmarkRouterBuildSingle2Param100(b *testing.B) {
    86  	records := makeTestSingle2ParamRecords(100)
    87  	benchmarkRouterBuild(b, records)
    88  }
    89  
    90  func BenchmarkRouterBuildSingle2Param300(b *testing.B) {
    91  	records := makeTestSingle2ParamRecords(300)
    92  	benchmarkRouterBuild(b, records)
    93  }
    94  
    95  func BenchmarkRouterBuildSingle2Param700(b *testing.B) {
    96  	records := makeTestSingle2ParamRecords(700)
    97  	benchmarkRouterBuild(b, records)
    98  }
    99  
   100  func benchmarkRouterLookupStatic(b *testing.B, n int) {
   101  	b.StopTimer()
   102  	router := denco.New()
   103  	records := makeTestStaticRecords(n)
   104  	if err := router.Build(records); err != nil {
   105  		b.Fatal(err)
   106  	}
   107  	record := pickTestRecord(records)
   108  	b.StartTimer()
   109  	for i := 0; i < b.N; i++ {
   110  		if r, _, _ := router.Lookup(record.Key); r != record.Value {
   111  			b.Fail()
   112  		}
   113  	}
   114  }
   115  
   116  func benchmarkRouterLookupSingleParam(b *testing.B, records []denco.Record) {
   117  	router := denco.New()
   118  	if err := router.Build(records); err != nil {
   119  		b.Fatal(err)
   120  	}
   121  	record := pickTestRecord(records)
   122  	b.ResetTimer()
   123  	for i := 0; i < b.N; i++ {
   124  		if _, _, found := router.Lookup(record.Key); !found {
   125  			b.Fail()
   126  		}
   127  	}
   128  }
   129  
   130  func benchmarkRouterBuild(b *testing.B, records []denco.Record) {
   131  	for i := 0; i < b.N; i++ {
   132  		router := denco.New()
   133  		if err := router.Build(records); err != nil {
   134  			b.Fatal(err)
   135  		}
   136  	}
   137  }
   138  
   139  func makeTestStaticRecords(n int) []denco.Record {
   140  	records := make([]denco.Record, n)
   141  	for i := 0; i < n; i++ {
   142  		records[i] = denco.NewRecord("/"+randomString(50), fmt.Sprintf("testroute%d", i))
   143  	}
   144  	return records
   145  }
   146  
   147  func makeTestSingleParamRecords(n int) []denco.Record {
   148  	records := make([]denco.Record, n)
   149  	for i := 0; i < len(records); i++ {
   150  		records[i] = denco.NewRecord(fmt.Sprintf("/user%d/:name", i), fmt.Sprintf("testroute%d", i))
   151  	}
   152  	return records
   153  }
   154  
   155  func makeTestSingle2ParamRecords(n int) []denco.Record {
   156  	records := make([]denco.Record, n)
   157  	for i := 0; i < len(records); i++ {
   158  		records[i] = denco.NewRecord(fmt.Sprintf("/user%d/:name/comment/:id", i), fmt.Sprintf("testroute%d", i))
   159  	}
   160  	return records
   161  }
   162  
   163  func pickTestRecord(records []denco.Record) denco.Record {
   164  	return records[len(records)/2]
   165  }
   166  
   167  func randomString(n int) string {
   168  	const srcStrings = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/"
   169  	var buf bytes.Buffer
   170  	for i := 0; i < n; i++ {
   171  		num, err := rand.Int(rand.Reader, big.NewInt(int64(len(srcStrings)-1)))
   172  		if err != nil {
   173  			panic(err)
   174  		}
   175  		buf.WriteByte(srcStrings[num.Int64()])
   176  	}
   177  	return buf.String()
   178  }
   179  

View as plain text