github.com/go-graphite/carbonapi@v0.17.0/cmd/carbonapi/http/render_handler_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/go-graphite/carbonapi/cmd/carbonapi/config"
     9  	"github.com/lomik/zapwriter"
    10  )
    11  
    12  func BenchmarkResponseCacheComputeKey(b *testing.B) {
    13  	var from int64 = 1628876560
    14  	var until int64 = 1628876620
    15  	var maxDataPoints int64 = 1024
    16  	noNullPoints := false
    17  	template := "test"
    18  	targets := []string{
    19  		"test.metric.*.cpu.load_avg",
    20  		"test.metric.*.memory.free",
    21  	}
    22  	format := "json"
    23  
    24  	for i := 0; i < b.N; i++ {
    25  		_ = responseCacheComputeKey(from, until, targets, format, maxDataPoints, noNullPoints, template)
    26  	}
    27  }
    28  
    29  func BenchmarkBackendCacheComputeKey(b *testing.B) {
    30  	from := "1628876560"
    31  	until := "1628876620"
    32  	targets := []string{
    33  		"test.metric.*.cpu.load_avg",
    34  		"test.metric.*.memory.free",
    35  	}
    36  
    37  	for i := 0; i < b.N; i++ {
    38  		_ = backendCacheComputeKey(from, until, targets, 0, true)
    39  	}
    40  }
    41  
    42  func BenchmarkBackendCacheComputeKeyAbs(b *testing.B) {
    43  	var from int64 = 1628876560
    44  	var until int64 = 1628876620
    45  	targets := []string{
    46  		"test.metric.*.cpu.load_avg",
    47  		"test.metric.*.memory.free",
    48  	}
    49  
    50  	for i := 0; i < b.N; i++ {
    51  		_ = backendCacheComputeKeyAbs(from, until, targets, 0, true)
    52  	}
    53  }
    54  
    55  func Test_getCacheTimeout(t *testing.T) {
    56  	cacheConfig := config.CacheConfig{
    57  		ShortTimeoutSec:     60,
    58  		DefaultTimeoutSec:   300,
    59  		ShortDuration:       3 * time.Hour,
    60  		ShortUntilOffsetSec: 120,
    61  	}
    62  
    63  	now := int64(1636985018)
    64  
    65  	tests := []struct {
    66  		name  string
    67  		now   time.Time
    68  		from  int64
    69  		until int64
    70  		want  int32
    71  	}{
    72  		{
    73  			name:  "short: from = now - 600, until = now - 120",
    74  			now:   time.Unix(now, 0),
    75  			from:  now - 600,
    76  			until: now - 120,
    77  			want:  60,
    78  		},
    79  		{
    80  			name:  "short: from = now - 10800",
    81  			now:   time.Unix(now, 0),
    82  			from:  now - 10800,
    83  			until: now,
    84  			want:  60,
    85  		},
    86  		{
    87  			name:  "short: from = now - 10810, until = now - 120",
    88  			now:   time.Unix(now, 0),
    89  			from:  now - 10800,
    90  			until: now - 120,
    91  			want:  60,
    92  		},
    93  		{
    94  			name:  "short: from = now - 10800, until now - 121",
    95  			now:   time.Unix(now, 0),
    96  			from:  now - 10800,
    97  			until: now - 121,
    98  			want:  300,
    99  		},
   100  		{
   101  			name:  "default: from = now - 10801",
   102  			now:   time.Unix(now, 0),
   103  			from:  now - 10801,
   104  			until: now,
   105  			want:  300,
   106  		},
   107  		{
   108  			name:  "short: from = now - 122, until = now - 121",
   109  			now:   time.Unix(now, 0),
   110  			from:  now - 122,
   111  			until: now - 121,
   112  			want:  300,
   113  		},
   114  	}
   115  	logger := zapwriter.Logger("test")
   116  	r, _ := http.NewRequest("GET", "http://127.0.0.1/render", nil)
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			duration := time.Second * time.Duration(tt.until-tt.from)
   120  			if got := getCacheTimeout(logger, r, tt.now.Unix(), tt.until, duration, &cacheConfig); got != tt.want {
   121  				t.Errorf("getCacheTimeout() = %v, want %v", got, tt.want)
   122  			}
   123  		})
   124  	}
   125  }