1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
21 // RemoveAll removes all exported variables.
22 // This is for tests only.
25 defer varKeysMu
.Unlock()
26 for _
, k
:= range varKeys
{
32 func TestNil(t
*testing
.T
) {
36 t
.Errorf("got %v, want nil", val
)
40 func TestInt(t
*testing
.T
) {
42 reqs
:= NewInt("requests")
43 if i
:= reqs
.Value(); i
!= 0 {
44 t
.Errorf("reqs.Value() = %v, want 0", i
)
46 if reqs
!= Get("requests").(*Int
) {
47 t
.Errorf("Get() failed.")
52 if i
:= reqs
.Value(); i
!= 4 {
53 t
.Errorf("reqs.Value() = %v, want 4", i
)
56 if s
:= reqs
.String(); s
!= "4" {
57 t
.Errorf("reqs.String() = %q, want \"4\"", s
)
61 if i
:= reqs
.Value(); i
!= -2 {
62 t
.Errorf("reqs.Value() = %v, want -2", i
)
66 func BenchmarkIntAdd(b
*testing
.B
) {
69 b
.RunParallel(func(pb
*testing
.PB
) {
76 func BenchmarkIntSet(b
*testing
.B
) {
79 b
.RunParallel(func(pb
*testing
.PB
) {
86 func TestFloat(t
*testing
.T
) {
88 reqs
:= NewFloat("requests-float")
90 t
.Errorf("reqs.f = %v, want 0", reqs
.f
)
92 if reqs
!= Get("requests-float").(*Float
) {
93 t
.Errorf("Get() failed.")
98 if v
:= reqs
.Value(); v
!= 2.75 {
99 t
.Errorf("reqs.Value() = %v, want 2.75", v
)
102 if s
:= reqs
.String(); s
!= "2.75" {
103 t
.Errorf("reqs.String() = %q, want \"4.64\"", s
)
107 if v
:= reqs
.Value(); v
!= 0.75 {
108 t
.Errorf("reqs.Value() = %v, want 0.75", v
)
112 func BenchmarkFloatAdd(b
*testing
.B
) {
115 b
.RunParallel(func(pb
*testing
.PB
) {
122 func BenchmarkFloatSet(b
*testing
.B
) {
125 b
.RunParallel(func(pb
*testing
.PB
) {
132 func TestString(t
*testing
.T
) {
134 name
:= NewString("my-name")
135 if s
:= name
.Value(); s
!= "" {
136 t
.Errorf(`NewString("my-name").Value() = %q, want ""`, s
)
140 if s
, want
:= name
.String(), `"Mike"`; s
!= want
{
141 t
.Errorf(`after name.Set("Mike"), name.String() = %q, want %q`, s
, want
)
143 if s
, want
:= name
.Value(), "Mike"; s
!= want
{
144 t
.Errorf(`after name.Set("Mike"), name.Value() = %q, want %q`, s
, want
)
147 // Make sure we produce safe JSON output.
149 if s
, want
:= name
.String(), "\"\\u003c\""; s
!= want
{
150 t
.Errorf(`after name.Set("<"), name.String() = %q, want %q`, s
, want
)
154 func BenchmarkStringSet(b
*testing
.B
) {
157 b
.RunParallel(func(pb
*testing
.PB
) {
164 func TestMapCounter(t
*testing
.T
) {
166 colors
:= NewMap("bike-shed-colors")
170 colors
.Add("blue", 4)
171 colors
.AddFloat(`green "midori"`, 4.125)
172 if x
:= colors
.Get("red").(*Int
).Value(); x
!= 3 {
173 t
.Errorf("colors.m[\"red\"] = %v, want 3", x
)
175 if x
:= colors
.Get("blue").(*Int
).Value(); x
!= 4 {
176 t
.Errorf("colors.m[\"blue\"] = %v, want 4", x
)
178 if x
:= colors
.Get(`green "midori"`).(*Float
).Value(); x
!= 4.125 {
179 t
.Errorf("colors.m[`green \"midori\"] = %v, want 4.125", x
)
182 // colors.String() should be '{"red":3, "blue":4}',
183 // though the order of red and blue could vary.
186 err
:= json
.Unmarshal([]byte(s
), &j
)
188 t
.Errorf("colors.String() isn't valid JSON: %v", err
)
190 m
, ok
:= j
.(map[string]interface{})
192 t
.Error("colors.String() didn't produce a map.")
195 x
, ok
:= red
.(float64)
197 t
.Error("red.Kind() is not a number.")
200 t
.Errorf("red = %v, want 3", x
)
204 func BenchmarkMapSet(b
*testing
.B
) {
209 b
.RunParallel(func(pb
*testing
.PB
) {
216 func BenchmarkMapSetDifferent(b
*testing
.B
) {
217 procKeys
:= make([][]string, runtime
.GOMAXPROCS(0))
218 for i
:= range procKeys
{
219 keys
:= make([]string, 4)
220 for j
:= range keys
{
221 keys
[j
] = fmt
.Sprint(i
, j
)
231 b
.RunParallel(func(pb
*testing
.PB
) {
232 i
:= int(atomic
.AddInt32(&n
, 1)-1) % len(procKeys
)
236 for _
, k
:= range keys
{
243 func BenchmarkMapSetString(b
*testing
.B
) {
249 b
.RunParallel(func(pb
*testing
.PB
) {
256 func BenchmarkMapAddSame(b
*testing
.B
) {
257 b
.RunParallel(func(pb
*testing
.PB
) {
268 func BenchmarkMapAddDifferent(b
*testing
.B
) {
269 procKeys
:= make([][]string, runtime
.GOMAXPROCS(0))
270 for i
:= range procKeys
{
271 keys
:= make([]string, 4)
272 for j
:= range keys
{
273 keys
[j
] = fmt
.Sprint(i
, j
)
281 b
.RunParallel(func(pb
*testing
.PB
) {
282 i
:= int(atomic
.AddInt32(&n
, 1)-1) % len(procKeys
)
287 for _
, k
:= range keys
{
294 func BenchmarkMapAddSameSteadyState(b
*testing
.B
) {
296 b
.RunParallel(func(pb
*testing
.PB
) {
303 func BenchmarkMapAddDifferentSteadyState(b
*testing
.B
) {
304 procKeys
:= make([][]string, runtime
.GOMAXPROCS(0))
305 for i
:= range procKeys
{
306 keys
:= make([]string, 4)
307 for j
:= range keys
{
308 keys
[j
] = fmt
.Sprint(i
, j
)
317 b
.RunParallel(func(pb
*testing
.PB
) {
318 i
:= int(atomic
.AddInt32(&n
, 1)-1) % len(procKeys
)
322 for _
, k
:= range keys
{
329 func TestFunc(t
*testing
.T
) {
331 var x
interface{} = []string{"a", "b"}
332 f
:= Func(func() interface{} { return x
})
333 if s
, exp
:= f
.String(), `["a","b"]`; s
!= exp
{
334 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)
336 if v
:= f
.Value(); !reflect
.DeepEqual(v
, x
) {
337 t
.Errorf(`f.Value() = %q, want %q`, v
, x
)
341 if s
, exp
:= f
.String(), `17`; s
!= exp
{
342 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)
346 func TestHandler(t
*testing
.T
) {
352 for i
:= 0; i
< 9; i
++ {
353 m2
.Add(strconv
.Itoa(i
), int64(i
))
355 rr
:= httptest
.NewRecorder()
356 rr
.Body
= new(bytes
.Buffer
)
357 expvarHandler(rr
, nil)
359 "map1": {"a": 1, "z": 2},
360 "map2": {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8}
363 if got
:= rr
.Body
.String(); got
!= want
{
364 t
.Errorf("HTTP handler wrote:\n%s\nWant:\n%s", got
, want
)
368 func BenchmarkRealworldExpvarUsage(b
*testing
.B
) {
374 // The benchmark creates GOMAXPROCS client/server pairs.
375 // Each pair creates 4 goroutines: client reader/writer and server reader/writer.
376 // The benchmark stresses concurrent reading and writing to the same connection.
377 // Such pattern is used in net/http and net/rpc.
381 P
:= runtime
.GOMAXPROCS(0)
385 // Setup P client/server connections.
386 clients
:= make([]net
.Conn
, P
)
387 servers
:= make([]net
.Conn
, P
)
388 ln
, err
:= net
.Listen("tcp", "127.0.0.1:0")
390 b
.Fatalf("Listen failed: %v", err
)
393 done
:= make(chan bool)
395 for p
:= 0; p
< P
; p
++ {
396 s
, err
:= ln
.Accept()
398 b
.Errorf("Accept failed: %v", err
)
405 for p
:= 0; p
< P
; p
++ {
406 c
, err
:= net
.Dial("tcp", ln
.Addr().String())
408 b
.Fatalf("Dial failed: %v", err
)
416 var wg sync
.WaitGroup
418 for p
:= 0; p
< P
; p
++ {
420 go func(c net
.Conn
) {
423 for i
:= 0; i
< N
; i
++ {
425 for w
:= 0; w
< W
; w
++ {
429 n
, err
:= c
.Write(buf
[:])
431 b
.Errorf("Write failed: %v", err
)
435 bytesSent
.Add(int64(n
))
439 // Pipe between server reader and server writer.
440 pipe
:= make(chan byte, 128)
443 go func(s net
.Conn
) {
446 for i
:= 0; i
< N
; i
++ {
447 n
, err
:= s
.Read(buf
[:])
450 b
.Errorf("Read failed: %v", err
)
454 bytesRead
.Add(int64(n
))
460 go func(s net
.Conn
) {
463 for i
:= 0; i
< N
; i
++ {
465 for w
:= 0; w
< W
; w
++ {
469 n
, err
:= s
.Write(buf
[:])
471 b
.Errorf("Write failed: %v", err
)
475 bytesSent
.Add(int64(n
))
481 go func(c net
.Conn
) {
484 for i
:= 0; i
< N
; i
++ {
485 n
, err
:= c
.Read(buf
[:])
488 b
.Errorf("Read failed: %v", err
)
492 bytesRead
.Add(int64(n
))