github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/monitor/map_test.go (about)

     1  // Copyright (c) 2017 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  // This code was forked from the Go project, here's the original copyright header:
     6  
     7  // Copyright 2009 The Go Authors. All rights reserved.
     8  // Use of this source code is governed by a BSD-style
     9  // license that can be found in the LICENSE file.
    10  
    11  package monitor
    12  
    13  import (
    14  	"encoding/json"
    15  	"expvar"
    16  	"fmt"
    17  	"runtime"
    18  	"sync/atomic"
    19  	"testing"
    20  )
    21  
    22  func TestMapCounter(t *testing.T) {
    23  	colors := NewMap("colors-in-french")
    24  
    25  	red := expvar.NewString("red-in-french")
    26  	red.Set("rouge")
    27  	colors.Set("red", red)
    28  	blue := expvar.NewString("blue-in-french")
    29  	blue.Set("bleu")
    30  	colors.Set("blue", blue)
    31  	green := expvar.NewString("green-in-french")
    32  	green.Set("vert")
    33  	colors.Set("green", green)
    34  	colors.Delete("green")
    35  	if x := colors.Get("red").(*expvar.String).Value(); x != "rouge" {
    36  		t.Errorf(`colors.m["red"] = %v, want "rouge"`, x)
    37  	}
    38  	if x := colors.Get("blue").(*expvar.String).Value(); x != "bleu" {
    39  		t.Errorf(`colors.m["blue"] = %v, want "bleu"`, x)
    40  	}
    41  
    42  	// colors.String() should be `{"red":"rouge", "blue":"bleu"}`,
    43  	// though the order of red and blue could vary.
    44  	s := colors.String()
    45  	var j interface{}
    46  	err := json.Unmarshal([]byte(s), &j)
    47  	if err != nil {
    48  		t.Fatalf("colors.String() isn't valid JSON: %v", err)
    49  	}
    50  	m, ok := j.(map[string]interface{})
    51  	if !ok {
    52  		t.Error("colors.String() didn't produce a map.")
    53  	}
    54  	if len(m) != 2 {
    55  		t.Error("Should've been only 2 entries in", m)
    56  	}
    57  
    58  	if rouge, ok := m["red"].(string); !ok || rouge != "rouge" {
    59  		t.Error("bad value for red:", m)
    60  	}
    61  }
    62  
    63  func BenchmarkMapSet(b *testing.B) {
    64  	m := new(Map)
    65  
    66  	v := new(expvar.Int)
    67  
    68  	b.RunParallel(func(pb *testing.PB) {
    69  		for pb.Next() {
    70  			m.Set("red", v)
    71  		}
    72  	})
    73  }
    74  
    75  func BenchmarkMapSetDifferent(b *testing.B) {
    76  	procKeys := make([][]string, runtime.GOMAXPROCS(0))
    77  	for i := range procKeys {
    78  		keys := make([]string, 4)
    79  		for j := range keys {
    80  			keys[j] = fmt.Sprint(i, j)
    81  		}
    82  		procKeys[i] = keys
    83  	}
    84  
    85  	m := new(Map)
    86  	v := new(expvar.Int)
    87  	b.ResetTimer()
    88  
    89  	var n int32
    90  	b.RunParallel(func(pb *testing.PB) {
    91  		i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys)
    92  		keys := procKeys[i]
    93  
    94  		for pb.Next() {
    95  			for _, k := range keys {
    96  				m.Set(k, v)
    97  			}
    98  		}
    99  	})
   100  }
   101  
   102  func BenchmarkMapSetString(b *testing.B) {
   103  	m := new(Map)
   104  
   105  	v := new(expvar.String)
   106  	v.Set("Hello, !")
   107  
   108  	b.RunParallel(func(pb *testing.PB) {
   109  		for pb.Next() {
   110  			m.Set("red", v)
   111  		}
   112  	})
   113  }