LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package rpc | 5 package rpc |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 } | 438 } |
439 | 439 |
440 func dialDirect() (*Client, error) { | 440 func dialDirect() (*Client, error) { |
441 return Dial("tcp", serverAddr) | 441 return Dial("tcp", serverAddr) |
442 } | 442 } |
443 | 443 |
444 func dialHTTP() (*Client, error) { | 444 func dialHTTP() (*Client, error) { |
445 return DialHTTP("tcp", httpServerAddr) | 445 return DialHTTP("tcp", httpServerAddr) |
446 } | 446 } |
447 | 447 |
448 func countMallocs(dial func() (*Client, error), t *testing.T) uint64 { | 448 func countMallocs(dial func() (*Client, error), t *testing.T) float64 { |
449 » defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1)) | |
450 once.Do(startServer) | 449 once.Do(startServer) |
451 client, err := dial() | 450 client, err := dial() |
452 if err != nil { | 451 if err != nil { |
453 t.Fatal("error dialing", err) | 452 t.Fatal("error dialing", err) |
454 } | 453 } |
455 args := &Args{7, 8} | 454 args := &Args{7, 8} |
456 reply := new(Reply) | 455 reply := new(Reply) |
457 » memstats := new(runtime.MemStats) | 456 » return testing.AllocsPerRun(100, func() { |
458 » runtime.ReadMemStats(memstats) | |
459 » mallocs := 0 - memstats.Mallocs | |
460 » const count = 100 | |
461 » for i := 0; i < count; i++ { | |
462 err := client.Call("Arith.Add", args, reply) | 457 err := client.Call("Arith.Add", args, reply) |
463 if err != nil { | 458 if err != nil { |
464 t.Errorf("Add: expected no error but got string %q", err
.Error()) | 459 t.Errorf("Add: expected no error but got string %q", err
.Error()) |
465 } | 460 } |
466 if reply.C != args.A+args.B { | 461 if reply.C != args.A+args.B { |
467 t.Errorf("Add: expected %d got %d", reply.C, args.A+args
.B) | 462 t.Errorf("Add: expected %d got %d", reply.C, args.A+args
.B) |
468 } | 463 } |
469 » } | 464 » }) |
470 » runtime.ReadMemStats(memstats) | |
471 » mallocs += memstats.Mallocs | |
472 » return mallocs / count | |
473 } | 465 } |
474 | 466 |
475 func TestCountMallocs(t *testing.T) { | 467 func TestCountMallocs(t *testing.T) { |
476 » fmt.Printf("mallocs per rpc round trip: %d\n", countMallocs(dialDirect,
t)) | 468 » fmt.Printf("mallocs per rpc round trip: %v\n", countMallocs(dialDirect,
t)) |
477 } | 469 } |
478 | 470 |
479 func TestCountMallocsOverHTTP(t *testing.T) { | 471 func TestCountMallocsOverHTTP(t *testing.T) { |
480 » fmt.Printf("mallocs per HTTP rpc round trip: %d\n", countMallocs(dialHTT
P, t)) | 472 » fmt.Printf("mallocs per HTTP rpc round trip: %v\n", countMallocs(dialHTT
P, t)) |
481 } | 473 } |
482 | 474 |
483 type writeCrasher struct { | 475 type writeCrasher struct { |
484 done chan bool | 476 done chan bool |
485 } | 477 } |
486 | 478 |
487 func (writeCrasher) Close() error { | 479 func (writeCrasher) Close() error { |
488 return nil | 480 return nil |
489 } | 481 } |
490 | 482 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 benchmarkEndToEnd(dialHTTP, b) | 613 benchmarkEndToEnd(dialHTTP, b) |
622 } | 614 } |
623 | 615 |
624 func BenchmarkEndToEndAsync(b *testing.B) { | 616 func BenchmarkEndToEndAsync(b *testing.B) { |
625 benchmarkEndToEndAsync(dialDirect, b) | 617 benchmarkEndToEndAsync(dialDirect, b) |
626 } | 618 } |
627 | 619 |
628 func BenchmarkEndToEndAsyncHTTP(b *testing.B) { | 620 func BenchmarkEndToEndAsyncHTTP(b *testing.B) { |
629 benchmarkEndToEndAsync(dialHTTP, b) | 621 benchmarkEndToEndAsync(dialHTTP, b) |
630 } | 622 } |
LEFT | RIGHT |