map_test.go 2.4 KB

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