File: benchmark_test.go

package info (click to toggle)
golang-github-manyminds-api2go 1.0-RC2%2Bgit20161229.31.dc368bb-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 484 kB
  • ctags: 488
  • sloc: sh: 23; makefile: 3
file content (122 lines) | stat: -rw-r--r-- 1,769 bytes parent folder | download | duplicates (3)
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
package jsonapi

import (
	"database/sql"
	"testing"
)

func BenchmarkMarshal(b *testing.B) {
	post := &Post{
		ID:    1,
		Title: "Title",
	}

	for i := 0; i < b.N; i++ {
		_, err := Marshal(post)
		if err != nil {
			panic(err)
		}
	}
}

func BenchmarkUnmarshal(b *testing.B) {
	post := &Post{
		ID:    1,
		Title: "Title",
	}

	data, err := Marshal(post)
	if err != nil {
		panic(err)
	}

	for i := 0; i < b.N; i++ {
		err = Unmarshal(data, &Post{})
		if err != nil {
			panic(err)
		}
	}
}

func BenchmarkMarshalSlice(b *testing.B) {
	post := []Post{
		{
			ID:    1,
			Title: "Title",
		},
		{
			ID:    2,
			Title: "Title",
		},
	}

	for i := 0; i < b.N; i++ {
		_, err := Marshal(post)
		if err != nil {
			panic(err)
		}
	}
}

func BenchmarkUnmarshalSlice(b *testing.B) {
	post := []*Post{
		{
			ID:    1,
			Title: "Title",
		},
		{
			ID:    2,
			Title: "Title",
		},
	}

	data, err := Marshal(post)
	if err != nil {
		panic(err)
	}

	for i := 0; i < b.N; i++ {
		var posts []Post
		err = Unmarshal(data, &posts)
		if err != nil {
			panic(err)
		}
	}
}

func BenchmarkMarshalWithRelationships(b *testing.B) {
	post := &Post{
		ID:          1,
		Title:       "Title",
		AuthorID:    sql.NullInt64{Valid: true, Int64: 1},
		CommentsIDs: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
	}

	for i := 0; i < b.N; i++ {
		_, err := Marshal(post)
		if err != nil {
			panic(err)
		}
	}
}

func BenchmarkUnmarshalWithRelationships(b *testing.B) {
	post := &Post{
		ID:          1,
		Title:       "Title",
		AuthorID:    sql.NullInt64{Valid: true, Int64: 1},
		CommentsIDs: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
	}

	data, err := Marshal(post)
	if err != nil {
		panic(err)
	}

	for i := 0; i < b.N; i++ {
		err = Unmarshal(data, &Post{})
		if err != nil {
			panic(err)
		}
	}
}