1// Copyright 2013-2020 Aerospike, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package aerospike_test
16
17import (
18	"runtime"
19
20	. "github.com/aerospike/aerospike-client-go"
21
22	"testing"
23)
24
25var r *Record
26var rs []*Record
27var err error
28
29type OBJECT struct {
30	Price  int
31	DBName string
32	// Blob   []byte
33	Blob []int64
34}
35
36func benchGet(times int, client *Client, key *Key) {
37	for i := 0; i < times; i++ {
38		if r, err = client.Get(nil, key); err != nil {
39			panic(err)
40		}
41	}
42}
43
44func benchBatchGet(times int, client *Client, keys []*Key) {
45	for i := 0; i < times; i++ {
46		if rs, err = client.BatchGet(nil, keys); err != nil {
47			panic(err)
48		}
49	}
50}
51
52func benchPut(times int, client *Client, key *Key, wp *WritePolicy) {
53	dbName := NewBin("dbname", "CouchDB")
54	price := NewBin("price", 0)
55	keywords := NewBin("keywords", []string{"concurrent", "fast"})
56	for i := 0; i < times; i++ {
57		if err = client.PutBins(wp, key, dbName, price, keywords); err != nil {
58			panic(err)
59		}
60	}
61}
62
63func Benchmark_Get(b *testing.B) {
64	client, err := NewClientWithPolicy(clientPolicy, *host, *port)
65	if err != nil {
66		b.Fail()
67	}
68
69	key, _ := NewKey(*namespace, "test", "Aerospike")
70	// obj := &OBJECT{198, "Jack Shaftoe and Company", []byte(bytes.Repeat([]byte{32}, 1000))}
71	// obj := &OBJECT{198, "Jack Shaftoe and Company", []int64{1}}
72	client.Delete(nil, key)
73	client.PutBins(nil, key, NewBin("b", []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "a", "b"}))
74	// client.PutBins(nil, key, NewBin("b", 1))
75	// client.PutObject(nil, key, &obj)
76
77	b.N = 100
78	runtime.GC()
79	b.ResetTimer()
80	benchGet(b.N, client, key)
81}
82
83func Benchmark_Put(b *testing.B) {
84	client, err := NewClient(*host, *port)
85	if err != nil {
86		b.Fail()
87	}
88
89	key, _ := NewKey(*namespace, "test", "Aerospike")
90	writepolicy := NewWritePolicy(0, 0)
91
92	b.N = 100
93	runtime.GC()
94	b.ResetTimer()
95	benchPut(b.N, client, key, writepolicy)
96}
97
98func Benchmark_BatchGet(b *testing.B) {
99	client, err := NewClientWithPolicy(clientPolicy, *host, *port)
100	if err != nil {
101		b.Fail()
102	}
103
104	var keys []*Key
105	for i := 0; i < 10; i++ {
106		key, _ := NewKey(*namespace, "test", i)
107		if err := client.PutBins(nil, key, NewBin("b", 1)); err == nil {
108			keys = append(keys, key)
109		}
110	}
111
112	b.N = 1e4
113	runtime.GC()
114	b.ResetTimer()
115	benchBatchGet(b.N, client, keys)
116}
117