File: bench_test.go

package info (click to toggle)
golang-gopkg-macaroon.v1 0.0~git20150121.0.ab3940c-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 128 kB
  • ctags: 146
  • sloc: makefile: 3
file content (109 lines) | stat: -rw-r--r-- 2,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package macaroon_test

import (
	"crypto/rand"
	"encoding/base64"
	"testing"

	"gopkg.in/macaroon.v1"
)

func randomBytes(n int) []byte {
	b := make([]byte, n)
	_, err := rand.Read(b)
	if err != nil {
		panic(err)
	}
	return b
}

func BenchmarkNew(b *testing.B) {
	rootKey := randomBytes(24)
	id := base64.StdEncoding.EncodeToString(randomBytes(100))
	loc := base64.StdEncoding.EncodeToString(randomBytes(40))
	b.ResetTimer()
	for i := b.N - 1; i >= 0; i-- {
		MustNew(rootKey, id, loc)
	}
}

func BenchmarkAddCaveat(b *testing.B) {
	rootKey := randomBytes(24)
	id := base64.StdEncoding.EncodeToString(randomBytes(100))
	loc := base64.StdEncoding.EncodeToString(randomBytes(40))
	b.ResetTimer()
	for i := b.N - 1; i >= 0; i-- {
		b.StopTimer()
		m := MustNew(rootKey, id, loc)
		b.StartTimer()
		m.AddFirstPartyCaveat("some caveat stuff")
	}
}

func benchmarkVerify(b *testing.B, mspecs []macaroonSpec) {
	rootKey, primary, discharges := makeMacaroons(mspecs)
	check := func(string) error {
		return nil
	}
	b.ResetTimer()
	for i := b.N - 1; i >= 0; i-- {
		err := primary.Verify(rootKey, check, discharges)
		if err != nil {
			b.Fatalf("verification failed: %v", err)
		}
	}
}

func BenchmarkVerifyLarge(b *testing.B) {
	benchmarkVerify(b, recursiveThirdPartyCaveatMacaroons)
}

func BenchmarkVerifySmall(b *testing.B) {
	benchmarkVerify(b, []macaroonSpec{{
		rootKey: "root-key",
		id:      "root-id",
		caveats: []caveat{{
			condition: "wonderful",
		}},
	}})
}

func BenchmarkMarshalJSON(b *testing.B) {
	rootKey := randomBytes(24)
	id := base64.StdEncoding.EncodeToString(randomBytes(100))
	loc := base64.StdEncoding.EncodeToString(randomBytes(40))
	m := MustNew(rootKey, id, loc)
	b.ResetTimer()
	for i := b.N - 1; i >= 0; i-- {
		_, err := m.MarshalJSON()
		if err != nil {
			b.Fatalf("cannot marshal JSON: %v", err)
		}
	}
}

func MustNew(rootKey []byte, id, loc string) *macaroon.Macaroon {
	m, err := macaroon.New(rootKey, id, loc)
	if err != nil {
		panic(err)
	}
	return m
}

func BenchmarkUnmarshalJSON(b *testing.B) {
	rootKey := randomBytes(24)
	id := base64.StdEncoding.EncodeToString(randomBytes(100))
	loc := base64.StdEncoding.EncodeToString(randomBytes(40))
	m := MustNew(rootKey, id, loc)
	data, err := m.MarshalJSON()
	if err != nil {
		b.Fatalf("cannot marshal JSON: %v", err)
	}
	for i := b.N - 1; i >= 0; i-- {
		var m macaroon.Macaroon
		err := m.UnmarshalJSON(data)
		if err != nil {
			b.Fatalf("cannot unmarshal JSON: %v", err)
		}
	}
}