github.com/aavshr/aws-sdk-go@v1.41.3/awstesting/integration/performance/s3DownloadManager/config.go (about)

     1  //go:build go1.13 && integration && perftest
     2  // +build go1.13,integration,perftest
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"net/http"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/aavshr/aws-sdk-go/service/s3/s3manager"
    14  )
    15  
    16  type Config struct {
    17  	Bucket         string
    18  	Size           int64
    19  	Key            string
    20  	LogVerbose     bool
    21  	UploadPartSize int64
    22  
    23  	SDK      SDKConfig
    24  	Client   ClientConfig
    25  	Profiler Profiler
    26  }
    27  
    28  func (c *Config) SetupFlags(prefix string, flagset *flag.FlagSet) {
    29  	flagset.StringVar(&c.Bucket, "bucket", "",
    30  		"The S3 bucket `name` to download the object from.")
    31  	flagset.Int64Var(&c.Size, "size", 0,
    32  		"The S3 object size in bytes to be first uploaded then downloaded")
    33  	flagset.StringVar(&c.Key, "key", "", "The S3 object key to download")
    34  	flagset.BoolVar(&c.LogVerbose, "verbose", false,
    35  		"The output log will include verbose request information")
    36  	flagset.Int64Var(&c.UploadPartSize, "upload-part-size", 0, "the upload part size when uploading a file to s3")
    37  
    38  	c.SDK.SetupFlags(prefix, flagset)
    39  	c.Client.SetupFlags(prefix, flagset)
    40  	c.Profiler.SetupFlags(prefix, flagset)
    41  }
    42  
    43  func (c *Config) Validate() error {
    44  	var errs Errors
    45  
    46  	if len(c.Bucket) == 0 || (c.Size <= 0 && len(c.Key) == 0) {
    47  		errs = append(errs, fmt.Errorf("bucket and filename/size are required"))
    48  	}
    49  
    50  	if err := c.SDK.Validate(); err != nil {
    51  		errs = append(errs, err)
    52  	}
    53  	if err := c.Client.Validate(); err != nil {
    54  		errs = append(errs, err)
    55  	}
    56  
    57  	if len(errs) != 0 {
    58  		return errs
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  type SDKConfig struct {
    65  	PartSize       int64
    66  	Concurrency    int
    67  	BufferProvider s3manager.WriterReadFromProvider
    68  }
    69  
    70  func (c *SDKConfig) SetupFlags(prefix string, flagset *flag.FlagSet) {
    71  	prefix += "sdk."
    72  
    73  	flagset.Int64Var(&c.PartSize, prefix+"part-size", s3manager.DefaultDownloadPartSize,
    74  		"Specifies the `size` of parts of the object to download.")
    75  	flagset.IntVar(&c.Concurrency, prefix+"concurrency", s3manager.DefaultDownloadConcurrency,
    76  		"Specifies the number of parts to download `at once`.")
    77  }
    78  
    79  func (c *SDKConfig) Validate() error {
    80  	return nil
    81  }
    82  
    83  type ClientConfig struct {
    84  	KeepAlive bool
    85  	Timeouts  Timeouts
    86  
    87  	MaxIdleConns        int
    88  	MaxIdleConnsPerHost int
    89  
    90  	// Go 1.13
    91  	ReadBufferSize  int
    92  	WriteBufferSize int
    93  }
    94  
    95  func (c *ClientConfig) SetupFlags(prefix string, flagset *flag.FlagSet) {
    96  	prefix += "client."
    97  
    98  	flagset.BoolVar(&c.KeepAlive, prefix+"http-keep-alive", true,
    99  		"Specifies if HTTP keep alive is enabled.")
   100  
   101  	defTR := http.DefaultTransport.(*http.Transport)
   102  
   103  	flagset.IntVar(&c.MaxIdleConns, prefix+"idle-conns", defTR.MaxIdleConns,
   104  		"Specifies max idle connection pool size.")
   105  
   106  	flagset.IntVar(&c.MaxIdleConnsPerHost, prefix+"idle-conns-host", http.DefaultMaxIdleConnsPerHost,
   107  		"Specifies max idle connection pool per host, will be truncated by idle-conns.")
   108  
   109  	flagset.IntVar(&c.ReadBufferSize, prefix+"read-buffer", defTR.ReadBufferSize, "size of the transport read buffer used")
   110  	flagset.IntVar(&c.WriteBufferSize, prefix+"writer-buffer", defTR.WriteBufferSize, "size of the transport write buffer used")
   111  
   112  	c.Timeouts.SetupFlags(prefix, flagset)
   113  }
   114  
   115  func (c *ClientConfig) Validate() error {
   116  	var errs Errors
   117  
   118  	if err := c.Timeouts.Validate(); err != nil {
   119  		errs = append(errs, err)
   120  	}
   121  
   122  	if len(errs) != 0 {
   123  		return errs
   124  	}
   125  	return nil
   126  }
   127  
   128  type Timeouts struct {
   129  	Connect        time.Duration
   130  	TLSHandshake   time.Duration
   131  	ExpectContinue time.Duration
   132  	ResponseHeader time.Duration
   133  }
   134  
   135  func (c *Timeouts) SetupFlags(prefix string, flagset *flag.FlagSet) {
   136  	prefix += "timeout."
   137  
   138  	flagset.DurationVar(&c.Connect, prefix+"connect", 30*time.Second,
   139  		"The `timeout` connecting to the remote host.")
   140  
   141  	defTR := http.DefaultTransport.(*http.Transport)
   142  
   143  	flagset.DurationVar(&c.TLSHandshake, prefix+"tls", defTR.TLSHandshakeTimeout,
   144  		"The `timeout` waiting for the TLS handshake to complete.")
   145  
   146  	flagset.DurationVar(&c.ExpectContinue, prefix+"expect-continue", defTR.ExpectContinueTimeout,
   147  		"The `timeout` waiting for the TLS handshake to complete.")
   148  
   149  	flagset.DurationVar(&c.ResponseHeader, prefix+"response-header", defTR.ResponseHeaderTimeout,
   150  		"The `timeout` waiting for the TLS handshake to complete.")
   151  }
   152  
   153  func (c *Timeouts) Validate() error {
   154  	return nil
   155  }
   156  
   157  type Errors []error
   158  
   159  func (es Errors) Error() string {
   160  	var buf strings.Builder
   161  	for _, e := range es {
   162  		buf.WriteString(e.Error())
   163  	}
   164  
   165  	return buf.String()
   166  }