Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium 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. |
| 4 | |
Jungho Ahn | fa7a31f | 2015-01-07 17:34:12 -0800 | [diff] [blame] | 5 | package benchmark |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | "regexp" |
Cosmos Nicolaou | e35c92f | 2015-08-24 16:22:00 -0700 | [diff] [blame] | 13 | "runtime" |
Jungho Ahn | fa7a31f | 2015-01-07 17:34:12 -0800 | [diff] [blame] | 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | ) |
| 18 | |
| 19 | func BenchmarkTest(b *testing.B) { |
| 20 | stats := AddStats(b, 0) |
| 21 | for i := 1; i <= b.N; i++ { |
| 22 | time.Sleep(1 * time.Microsecond) |
| 23 | stats.Add(time.Duration(i) * time.Microsecond) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func BenchmarkTestMulti(b *testing.B) { |
| 28 | stats1 := AddStatsWithName(b, "S1", 0) |
| 29 | stats2 := AddStatsWithName(b, "S2", 0) |
| 30 | for i := 1; i <= b.N; i++ { |
| 31 | time.Sleep(1 * time.Microsecond) |
| 32 | stats1.Add(time.Duration(i) * time.Microsecond) |
| 33 | stats2.Add(time.Duration(i) * time.Millisecond) |
| 34 | } |
| 35 | } |
| 36 | |
Cosmos Nicolaou | e35c92f | 2015-08-24 16:22:00 -0700 | [diff] [blame] | 37 | func benchmarkName(name string) string { |
| 38 | procs := runtime.GOMAXPROCS(-1) |
| 39 | if procs != 1 { |
| 40 | return fmt.Sprintf("%s-%d", name, procs) |
| 41 | } |
| 42 | return name |
| 43 | } |
| 44 | |
Jungho Ahn | fa7a31f | 2015-01-07 17:34:12 -0800 | [diff] [blame] | 45 | func TestStatsInjection(t *testing.T) { |
| 46 | stdout := os.Stdout |
| 47 | r, w, _ := os.Pipe() |
| 48 | os.Stdout = w |
| 49 | |
| 50 | outC := make(chan string) |
| 51 | go func() { |
| 52 | b := new(bytes.Buffer) |
| 53 | io.Copy(b, r) |
| 54 | r.Close() |
| 55 | outC <- b.String() |
| 56 | }() |
| 57 | |
| 58 | startStatsInjector() |
| 59 | |
Cosmos Nicolaou | e35c92f | 2015-08-24 16:22:00 -0700 | [diff] [blame] | 60 | fmt.Printf("%s\t", benchmarkName("BenchmarkTest")) |
Jungho Ahn | fa7a31f | 2015-01-07 17:34:12 -0800 | [diff] [blame] | 61 | result := testing.Benchmark(BenchmarkTest) |
| 62 | fmt.Println(result.String()) |
| 63 | |
Cosmos Nicolaou | e35c92f | 2015-08-24 16:22:00 -0700 | [diff] [blame] | 64 | fmt.Printf("%s\t", benchmarkName("BenchmarkTestMulti")) |
Jungho Ahn | fa7a31f | 2015-01-07 17:34:12 -0800 | [diff] [blame] | 65 | result = testing.Benchmark(BenchmarkTestMulti) |
| 66 | fmt.Println(result.String()) |
| 67 | |
| 68 | stopStatsInjector() |
| 69 | |
| 70 | w.Close() |
| 71 | os.Stdout = stdout |
| 72 | out := <-outC |
| 73 | |
| 74 | if strings.Count(out, "Histogram") != 3 { |
| 75 | t.Errorf("unexpected stats output:\n%s", out) |
| 76 | } |
| 77 | if matched, _ := regexp.MatchString("Histogram.*\\)\n", out); !matched { |
| 78 | t.Errorf("unnamed stats not found:\n%s", out) |
| 79 | } |
| 80 | if matched, _ := regexp.MatchString("Histogram.*\\): S1\n", out); !matched { |
| 81 | t.Errorf("stats S1 not found:\n%s", out) |
| 82 | } |
| 83 | if matched, _ := regexp.MatchString("Histogram.*\\): S2\n", out); !matched { |
| 84 | t.Errorf("stats S2 not found:\n%s", out) |
| 85 | } |
| 86 | } |