1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package benchmark // import "go.mongodb.org/mongo-driver/benchmark"
8
9import (
10	"context"
11	"testing"
12	"time"
13
14	"github.com/stretchr/testify/require"
15)
16
17const (
18	five            = 5
19	ten             = 2 * five
20	hundred         = ten * ten
21	thousand        = ten * hundred
22	tenThousand     = ten * thousand
23	hundredThousand = hundred * thousand
24	million         = hundred * hundredThousand
25	halfMillion     = five * hundredThousand
26
27	ExecutionTimeout = five * time.Minute
28	StandardRuntime  = time.Minute
29	MinimumRuntime   = five * time.Second
30	MinIterations    = hundred
31)
32
33type BenchCase func(context.Context, TimerManager, int) error
34type BenchFunction func(*testing.B)
35
36func WrapCase(bench BenchCase) BenchFunction {
37	name := getName(bench)
38	return func(b *testing.B) {
39		ctx := context.Background()
40		b.ResetTimer()
41		b.ReportAllocs()
42		err := bench(ctx, b, b.N)
43		require.NoError(b, err, "case='%s'", name)
44	}
45}
46
47func getAllCases() []*CaseDefinition {
48	return []*CaseDefinition{
49		{
50			Bench:              CanaryIncCase,
51			Count:              million,
52			Size:               -1,
53			Runtime:            MinimumRuntime,
54			RequiredIterations: ten,
55		},
56		{
57			Bench:              GlobalCanaryIncCase,
58			Count:              million,
59			Size:               -1,
60			Runtime:            MinimumRuntime,
61			RequiredIterations: ten,
62		},
63		{
64			Bench:   BSONFlatDocumentEncoding,
65			Count:   tenThousand,
66			Size:    75310000,
67			Runtime: StandardRuntime,
68		},
69		{
70			Bench:   BSONFlatDocumentDecodingLazy,
71			Count:   tenThousand,
72			Size:    75310000,
73			Runtime: StandardRuntime,
74		},
75		{
76			Bench:   BSONFlatDocumentDecoding,
77			Count:   tenThousand,
78			Size:    75310000,
79			Runtime: StandardRuntime,
80		},
81		{
82			Bench:   BSONDeepDocumentEncoding,
83			Count:   tenThousand,
84			Size:    19640000,
85			Runtime: StandardRuntime,
86		},
87		{
88			Bench:   BSONDeepDocumentDecodingLazy,
89			Count:   tenThousand,
90			Size:    19640000,
91			Runtime: StandardRuntime,
92		},
93		{
94			Bench:   BSONDeepDocumentDecoding,
95			Count:   tenThousand,
96			Size:    19640000,
97			Runtime: StandardRuntime,
98		},
99		// {
100		//	Bench:   BSONFullDocumentEncoding,
101		//	Count:   tenThousand,
102		//	Size:    57340000,
103		//	Runtime: StandardRuntime,
104		// },
105		// {
106		//	Bench:   BSONFullDocumentDecodingLazy,
107		//	Count:   tenThousand,
108		//	Size:    57340000,
109		//	Runtime: StandardRuntime,
110		// },
111		// {
112		//	Bench:   BSONFullDocumentDecoding,
113		//	Count:   tenThousand,
114		//	Size:    57340000,
115		//	Runtime: StandardRuntime,
116		// },
117		// {
118		//	Bench:   BSONFullReaderDecoding,
119		//	Count:   tenThousand,
120		//	Size:    57340000,
121		//	Runtime: StandardRuntime,
122		// },
123		{
124			Bench:   BSONFlatMapDecoding,
125			Count:   tenThousand,
126			Size:    75310000,
127			Runtime: StandardRuntime,
128		},
129		{
130			Bench:   BSONFlatMapEncoding,
131			Count:   tenThousand,
132			Size:    75310000,
133			Runtime: StandardRuntime,
134		},
135		{
136			Bench:   BSONDeepMapDecoding,
137			Count:   tenThousand,
138			Size:    19640000,
139			Runtime: StandardRuntime,
140		},
141		{
142			Bench:   BSONDeepMapEncoding,
143			Count:   tenThousand,
144			Size:    19640000,
145			Runtime: StandardRuntime,
146		},
147		// {
148		//	Bench:   BSONFullMapDecoding,
149		//	Count:   tenThousand,
150		//	Size:    57340000,
151		//	Runtime: StandardRuntime,
152		// },
153		// {
154		//	Bench:   BSONFullMapEncoding,
155		//	Count:   tenThousand,
156		//	Size:    57340000,
157		//	Runtime: StandardRuntime,
158		// },
159		{
160			Bench:   BSONFlatStructDecoding,
161			Count:   tenThousand,
162			Size:    75310000,
163			Runtime: StandardRuntime,
164		},
165		{
166			Bench:   BSONFlatStructTagsDecoding,
167			Count:   tenThousand,
168			Size:    75310000,
169			Runtime: StandardRuntime,
170		},
171		{
172			Bench:   BSONFlatStructEncoding,
173			Count:   tenThousand,
174			Size:    75310000,
175			Runtime: StandardRuntime,
176		},
177		{
178			Bench:   BSONFlatStructTagsEncoding,
179			Count:   tenThousand,
180			Size:    75310000,
181			Runtime: StandardRuntime,
182		},
183		{
184			Bench:   SingleRunCommand,
185			Count:   tenThousand,
186			Size:    160000,
187			Runtime: StandardRuntime,
188		},
189		{
190			Bench:   SingleFindOneByID,
191			Count:   tenThousand,
192			Size:    16220000,
193			Runtime: StandardRuntime,
194		},
195		{
196			Bench:   SingleInsertSmallDocument,
197			Count:   tenThousand,
198			Size:    2750000,
199			Runtime: StandardRuntime,
200		},
201		{
202			Bench:   SingleInsertLargeDocument,
203			Count:   ten,
204			Size:    27310890,
205			Runtime: StandardRuntime,
206		},
207		{
208			Bench:   MultiFindMany,
209			Count:   tenThousand,
210			Size:    16220000,
211			Runtime: StandardRuntime,
212		},
213		{
214			Bench:   MultiInsertSmallDocument,
215			Count:   tenThousand,
216			Size:    2750000,
217			Runtime: StandardRuntime,
218		},
219		{
220			Bench:              MultiInsertLargeDocument,
221			Count:              ten,
222			Size:               27310890,
223			Runtime:            StandardRuntime,
224			RequiredIterations: tenThousand,
225		},
226	}
227}
228