github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/specgen/generate/ports_bench_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/containers/common/libnetwork/types"
     8  )
     9  
    10  func benchmarkParsePortMapping(b *testing.B, ports []types.PortMapping) {
    11  	for n := 0; n < b.N; n++ {
    12  		_, _ = ParsePortMapping(ports, nil)
    13  	}
    14  }
    15  
    16  func BenchmarkParsePortMappingNoPorts(b *testing.B) {
    17  	benchmarkParsePortMapping(b, nil)
    18  }
    19  
    20  func BenchmarkParsePortMapping1(b *testing.B) {
    21  	benchmarkParsePortMapping(b, []types.PortMapping{
    22  		{
    23  			HostPort:      8080,
    24  			ContainerPort: 80,
    25  			Protocol:      "tcp",
    26  		},
    27  	})
    28  }
    29  
    30  func BenchmarkParsePortMapping100(b *testing.B) {
    31  	ports := make([]types.PortMapping, 0, 100)
    32  	for i := uint16(8080); i < 8180; i++ {
    33  		ports = append(ports, types.PortMapping{
    34  			HostPort:      i,
    35  			ContainerPort: i,
    36  			Protocol:      "tcp",
    37  		})
    38  	}
    39  	b.ResetTimer()
    40  	benchmarkParsePortMapping(b, ports)
    41  }
    42  
    43  func BenchmarkParsePortMapping1k(b *testing.B) {
    44  	ports := make([]types.PortMapping, 0, 1000)
    45  	for i := uint16(8080); i < 9080; i++ {
    46  		ports = append(ports, types.PortMapping{
    47  			HostPort:      i,
    48  			ContainerPort: i,
    49  			Protocol:      "tcp",
    50  		})
    51  	}
    52  	b.ResetTimer()
    53  	benchmarkParsePortMapping(b, ports)
    54  }
    55  
    56  func BenchmarkParsePortMapping10k(b *testing.B) {
    57  	ports := make([]types.PortMapping, 0, 30000)
    58  	for i := uint16(8080); i < 18080; i++ {
    59  		ports = append(ports, types.PortMapping{
    60  			HostPort:      i,
    61  			ContainerPort: i,
    62  			Protocol:      "tcp",
    63  		})
    64  	}
    65  	b.ResetTimer()
    66  	benchmarkParsePortMapping(b, ports)
    67  }
    68  
    69  func BenchmarkParsePortMapping1m(b *testing.B) {
    70  	ports := make([]types.PortMapping, 0, 1000000)
    71  	for j := 0; j < 20; j++ {
    72  		for i := uint16(1); i <= 50000; i++ {
    73  			ports = append(ports, types.PortMapping{
    74  				HostPort:      i,
    75  				ContainerPort: i,
    76  				Protocol:      "tcp",
    77  				HostIP:        fmt.Sprintf("192.168.1.%d", j),
    78  			})
    79  		}
    80  	}
    81  	b.ResetTimer()
    82  	benchmarkParsePortMapping(b, ports)
    83  }
    84  
    85  func BenchmarkParsePortMappingReverse100(b *testing.B) {
    86  	ports := make([]types.PortMapping, 0, 100)
    87  	for i := uint16(8180); i > 8080; i-- {
    88  		ports = append(ports, types.PortMapping{
    89  			HostPort:      i,
    90  			ContainerPort: i,
    91  			Protocol:      "tcp",
    92  		})
    93  	}
    94  	b.ResetTimer()
    95  	benchmarkParsePortMapping(b, ports)
    96  }
    97  
    98  func BenchmarkParsePortMappingReverse1k(b *testing.B) {
    99  	ports := make([]types.PortMapping, 0, 1000)
   100  	for i := uint16(9080); i > 8080; i-- {
   101  		ports = append(ports, types.PortMapping{
   102  			HostPort:      i,
   103  			ContainerPort: i,
   104  			Protocol:      "tcp",
   105  		})
   106  	}
   107  	b.ResetTimer()
   108  	benchmarkParsePortMapping(b, ports)
   109  }
   110  
   111  func BenchmarkParsePortMappingReverse10k(b *testing.B) {
   112  	ports := make([]types.PortMapping, 0, 30000)
   113  	for i := uint16(18080); i > 8080; i-- {
   114  		ports = append(ports, types.PortMapping{
   115  			HostPort:      i,
   116  			ContainerPort: i,
   117  			Protocol:      "tcp",
   118  		})
   119  	}
   120  	b.ResetTimer()
   121  	benchmarkParsePortMapping(b, ports)
   122  }
   123  
   124  func BenchmarkParsePortMappingReverse1m(b *testing.B) {
   125  	ports := make([]types.PortMapping, 0, 1000000)
   126  	for j := 0; j < 20; j++ {
   127  		for i := uint16(50000); i > 0; i-- {
   128  			ports = append(ports, types.PortMapping{
   129  				HostPort:      i,
   130  				ContainerPort: i,
   131  				Protocol:      "tcp",
   132  				HostIP:        fmt.Sprintf("192.168.1.%d", j),
   133  			})
   134  		}
   135  	}
   136  	b.ResetTimer()
   137  	benchmarkParsePortMapping(b, ports)
   138  }
   139  
   140  func BenchmarkParsePortMappingRange1(b *testing.B) {
   141  	benchmarkParsePortMapping(b, []types.PortMapping{
   142  		{
   143  			HostPort:      8080,
   144  			ContainerPort: 80,
   145  			Protocol:      "tcp",
   146  			Range:         1,
   147  		},
   148  	})
   149  }
   150  
   151  func BenchmarkParsePortMappingRange100(b *testing.B) {
   152  	benchmarkParsePortMapping(b, []types.PortMapping{
   153  		{
   154  			HostPort:      8080,
   155  			ContainerPort: 80,
   156  			Protocol:      "tcp",
   157  			Range:         100,
   158  		},
   159  	})
   160  }
   161  
   162  func BenchmarkParsePortMappingRange1k(b *testing.B) {
   163  	benchmarkParsePortMapping(b, []types.PortMapping{
   164  		{
   165  			HostPort:      8080,
   166  			ContainerPort: 80,
   167  			Protocol:      "tcp",
   168  			Range:         1000,
   169  		},
   170  	})
   171  }
   172  
   173  func BenchmarkParsePortMappingRange10k(b *testing.B) {
   174  	benchmarkParsePortMapping(b, []types.PortMapping{
   175  		{
   176  			HostPort:      8080,
   177  			ContainerPort: 80,
   178  			Protocol:      "tcp",
   179  			Range:         10000,
   180  		},
   181  	})
   182  }
   183  
   184  func BenchmarkParsePortMappingRange1m(b *testing.B) {
   185  	ports := make([]types.PortMapping, 0, 1000000)
   186  	for j := 0; j < 20; j++ {
   187  		ports = append(ports, types.PortMapping{
   188  			HostPort:      1,
   189  			ContainerPort: 1,
   190  			Protocol:      "tcp",
   191  			Range:         50000,
   192  			HostIP:        fmt.Sprintf("192.168.1.%d", j),
   193  		})
   194  	}
   195  	b.ResetTimer()
   196  	benchmarkParsePortMapping(b, ports)
   197  }