File: integers_test.go

package info (click to toggle)
golang-github-tinylib-msgp 1.2.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 764 kB
  • sloc: makefile: 47
file content (140 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package msgp

import (
	"encoding/binary"
	"testing"
)

func BenchmarkIntegers(b *testing.B) {
	bytes64 := make([]byte, 9)
	bytes32 := make([]byte, 5)
	bytes16 := make([]byte, 3)

	b.Run("Int64", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMint64(bytes64, -1234567890123456789)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMint64(bytes64, -1234567890123456789)
			for i := 0; i < b.N; i++ {
				getMint64(bytes64)
			}
		})
	})
	b.Run("Int32", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMint32(bytes32, -123456789)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMint32(bytes32, -123456789)
			for i := 0; i < b.N; i++ {
				getMint32(bytes32)
			}
		})
	})
	b.Run("Int16", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMint16(bytes16, -12345)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMint16(bytes16, -12345)
			for i := 0; i < b.N; i++ {
				getMint16(bytes16)
			}
		})
	})

	b.Run("Uint64", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMuint64(bytes64, 1234567890123456789)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMuint64(bytes64, 1234567890123456789)
			for i := 0; i < b.N; i++ {
				getMuint64(bytes64)
			}
		})
	})
	b.Run("Uint32", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMuint32(bytes32, 123456789)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMuint32(bytes32, 123456789)
			for i := 0; i < b.N; i++ {
				getMuint32(bytes32)
			}
		})
	})
	b.Run("Uint16", func(b *testing.B) {
		b.Run("Put", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				putMuint16(bytes16, 12345)
			}
		})
		b.Run("Get", func(b *testing.B) {
			putMuint16(bytes16, 12345)
			for i := 0; i < b.N; i++ {
				getMuint16(bytes16)
			}
		})
	})
}

func BenchmarkIntegersUnix(b *testing.B) {
	bytes := make([]byte, 12)
	var sec int64 = 1609459200
	var nsec int32 = 123456789

	b.Run("Get", func(b *testing.B) {
		binary.BigEndian.PutUint64(bytes, uint64(sec))
		binary.BigEndian.PutUint32(bytes[8:], uint32(nsec))
		for i := 0; i < b.N; i++ {
			getUnix(bytes)
		}
	})

	b.Run("Put", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			putUnix(bytes, sec, nsec)
		}
	})
}

func BenchmarkIntegersPrefix(b *testing.B) {
	bytesU16 := make([]byte, 3)
	bytesU32 := make([]byte, 5)
	bytesU64 := make([]byte, 9)

	b.Run("u16", func(b *testing.B) {
		var pre byte = 0x01
		var sz uint16 = 12345
		for i := 0; i < b.N; i++ {
			prefixu16(bytesU16, pre, sz)
		}
	})
	b.Run("u32", func(b *testing.B) {
		var pre byte = 0x02
		var sz uint32 = 123456789
		for i := 0; i < b.N; i++ {
			prefixu32(bytesU32, pre, sz)
		}
	})
	b.Run("u64", func(b *testing.B) {
		var pre byte = 0x03
		var sz uint64 = 1234567890123456789
		for i := 0; i < b.N; i++ {
			prefixu64(bytesU64, pre, sz)
		}
	})
}