initial code repo
[stor4nfv.git] / src / ceph / src / test / objectstore / test_transaction.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2016 Casey Bodley <cbodley@redhat.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include "os/ObjectStore.h"
16 #include <gtest/gtest.h>
17 #include "common/Clock.h"
18 #include "include/utime.h"
19 #include <boost/tuple/tuple.hpp>
20
21 TEST(Transaction, MoveConstruct)
22 {
23   auto a = ObjectStore::Transaction{};
24   a.nop();
25   ASSERT_FALSE(a.empty());
26
27   // move-construct in b
28   auto b = std::move(a);
29   ASSERT_TRUE(a.empty());
30   ASSERT_FALSE(b.empty());
31 }
32
33 TEST(Transaction, MoveAssign)
34 {
35   auto a = ObjectStore::Transaction{};
36   a.nop();
37   ASSERT_FALSE(a.empty());
38
39   auto b = ObjectStore::Transaction{};
40   b = std::move(a); // move-assign to b
41   ASSERT_TRUE(a.empty());
42   ASSERT_FALSE(b.empty());
43 }
44
45 TEST(Transaction, CopyConstruct)
46 {
47   auto a = ObjectStore::Transaction{};
48   a.nop();
49   ASSERT_FALSE(a.empty());
50
51   auto b = a; // copy-construct in b
52   ASSERT_FALSE(a.empty());
53   ASSERT_FALSE(b.empty());
54 }
55
56 TEST(Transaction, CopyAssign)
57 {
58   auto a = ObjectStore::Transaction{};
59   a.nop();
60   ASSERT_FALSE(a.empty());
61
62   auto b = ObjectStore::Transaction{};
63   b = a; // copy-assign to b
64   ASSERT_FALSE(a.empty());
65   ASSERT_FALSE(b.empty());
66 }
67
68 TEST(Transaction, Swap)
69 {
70   auto a = ObjectStore::Transaction{};
71   a.nop();
72   ASSERT_FALSE(a.empty());
73
74   auto b = ObjectStore::Transaction{};
75   std::swap(a, b); // swap a and b
76   ASSERT_TRUE(a.empty());
77   ASSERT_FALSE(b.empty());
78 }
79
80 ObjectStore::Transaction generate_transaction()
81 {
82   auto a = ObjectStore::Transaction{};
83   a.nop();
84
85   coll_t cid;
86   object_t obj("test_name");
87   snapid_t snap(0);
88   hobject_t hoid(obj, "key", snap, 0, 0, "nspace");
89   ghobject_t oid(hoid);
90
91   coll_t acid;
92   object_t aobj("another_test_name");
93   snapid_t asnap(0);
94   hobject_t ahoid(obj, "another_key", snap, 0, 0, "another_nspace");
95   ghobject_t aoid(hoid);
96   std::set<string> keys;
97   keys.insert("any_1");
98   keys.insert("any_2");
99   keys.insert("any_3");
100
101   bufferlist bl;
102   bl.append_zero(4096);
103
104   a.write(cid, oid, 1, 4096, bl, 0);
105
106   a.omap_setkeys(acid, aoid, bl);
107
108   a.omap_rmkeys(cid, aoid, keys);
109
110   a.touch(acid, oid);
111
112   return a;
113 }
114
115 TEST(Transaction, MoveRangesDelSrcObj)
116 {
117   auto t = ObjectStore::Transaction{};
118   t.nop();
119
120   coll_t c(spg_t(pg_t(1,2), shard_id_t::NO_SHARD));
121
122   ghobject_t o1(hobject_t("obj", "", 123, 456, -1, ""));
123   ghobject_t o2(hobject_t("obj2", "", 123, 456, -1, ""));
124   vector<std::pair<uint64_t, uint64_t>> move_info = {
125     make_pair(1, 5),
126     make_pair(10, 5)
127   };
128
129   t.touch(c, o1);
130   bufferlist bl;
131   bl.append("some data");
132   t.write(c, o1, 1, bl.length(), bl);
133   t.write(c, o1, 10, bl.length(), bl);
134
135   t.clone(c, o1, o2);
136   bl.append("some other data");
137   t.write(c, o2, 1, bl.length(), bl);
138 }
139
140 TEST(Transaction, GetNumBytes)
141 {
142   auto a = ObjectStore::Transaction{};
143   a.nop();
144   ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
145
146   coll_t cid;
147   object_t obj("test_name");
148   snapid_t snap(0);
149   hobject_t hoid(obj, "key", snap, 0, 0, "nspace");
150   ghobject_t oid(hoid);
151
152   coll_t acid;
153   object_t aobj("another_test_name");
154   snapid_t asnap(0);
155   hobject_t ahoid(obj, "another_key", snap, 0, 0, "another_nspace");
156   ghobject_t aoid(hoid);
157   std::set<string> keys;
158   keys.insert("any_1");
159   keys.insert("any_2");
160   keys.insert("any_3");
161
162   bufferlist bl;
163   bl.append_zero(4096);
164
165   a.write(cid, oid, 1, 4096, bl, 0);
166   ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
167
168   a.omap_setkeys(acid, aoid, bl);
169   ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
170
171   a.omap_rmkeys(cid, aoid, keys);
172   ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
173
174   a.touch(acid, oid);
175   ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
176 }
177
178 void bench_num_bytes(bool legacy)
179 {
180   const int max = 2500000;
181   auto a = generate_transaction();
182
183   if (legacy) {
184     cout << "get_encoded_bytes_test: ";
185   } else {
186     cout << "get_encoded_bytes: ";
187   }
188
189   utime_t start = ceph_clock_now();
190   if (legacy) {
191     for (int i = 0; i < max; ++i) {
192       a.get_encoded_bytes_test();
193     }
194   } else {
195     for (int i = 0; i < max; ++i) {
196       a.get_encoded_bytes();
197     }
198   }
199
200   utime_t end = ceph_clock_now();
201   cout << max << " encodes in " << (end - start) << std::endl;
202
203 }
204
205 TEST(Transaction, GetNumBytesBenchLegacy)
206 {
207    bench_num_bytes(true);
208 }
209
210 TEST(Transaction, GetNumBytesBenchCurrent)
211 {
212    bench_num_bytes(false);
213 }