github.com/containers/podman/v4@v4.9.4/pkg/specgen/generate/ports_bench_test.go (about)

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