1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 Casey Bodley <cbodley@redhat.com>
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.
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>
21 TEST(Transaction, MoveConstruct)
23 auto a = ObjectStore::Transaction{};
25 ASSERT_FALSE(a.empty());
27 // move-construct in b
28 auto b = std::move(a);
29 ASSERT_TRUE(a.empty());
30 ASSERT_FALSE(b.empty());
33 TEST(Transaction, MoveAssign)
35 auto a = ObjectStore::Transaction{};
37 ASSERT_FALSE(a.empty());
39 auto b = ObjectStore::Transaction{};
40 b = std::move(a); // move-assign to b
41 ASSERT_TRUE(a.empty());
42 ASSERT_FALSE(b.empty());
45 TEST(Transaction, CopyConstruct)
47 auto a = ObjectStore::Transaction{};
49 ASSERT_FALSE(a.empty());
51 auto b = a; // copy-construct in b
52 ASSERT_FALSE(a.empty());
53 ASSERT_FALSE(b.empty());
56 TEST(Transaction, CopyAssign)
58 auto a = ObjectStore::Transaction{};
60 ASSERT_FALSE(a.empty());
62 auto b = ObjectStore::Transaction{};
63 b = a; // copy-assign to b
64 ASSERT_FALSE(a.empty());
65 ASSERT_FALSE(b.empty());
68 TEST(Transaction, Swap)
70 auto a = ObjectStore::Transaction{};
72 ASSERT_FALSE(a.empty());
74 auto b = ObjectStore::Transaction{};
75 std::swap(a, b); // swap a and b
76 ASSERT_TRUE(a.empty());
77 ASSERT_FALSE(b.empty());
80 ObjectStore::Transaction generate_transaction()
82 auto a = ObjectStore::Transaction{};
86 object_t obj("test_name");
88 hobject_t hoid(obj, "key", snap, 0, 0, "nspace");
92 object_t aobj("another_test_name");
94 hobject_t ahoid(obj, "another_key", snap, 0, 0, "another_nspace");
95 ghobject_t aoid(hoid);
96 std::set<string> keys;
102 bl.append_zero(4096);
104 a.write(cid, oid, 1, 4096, bl, 0);
106 a.omap_setkeys(acid, aoid, bl);
108 a.omap_rmkeys(cid, aoid, keys);
115 TEST(Transaction, MoveRangesDelSrcObj)
117 auto t = ObjectStore::Transaction{};
120 coll_t c(spg_t(pg_t(1,2), shard_id_t::NO_SHARD));
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 = {
131 bl.append("some data");
132 t.write(c, o1, 1, bl.length(), bl);
133 t.write(c, o1, 10, bl.length(), bl);
136 bl.append("some other data");
137 t.write(c, o2, 1, bl.length(), bl);
140 TEST(Transaction, GetNumBytes)
142 auto a = ObjectStore::Transaction{};
144 ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
147 object_t obj("test_name");
149 hobject_t hoid(obj, "key", snap, 0, 0, "nspace");
150 ghobject_t oid(hoid);
153 object_t aobj("another_test_name");
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");
163 bl.append_zero(4096);
165 a.write(cid, oid, 1, 4096, bl, 0);
166 ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
168 a.omap_setkeys(acid, aoid, bl);
169 ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
171 a.omap_rmkeys(cid, aoid, keys);
172 ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
175 ASSERT_TRUE(a.get_encoded_bytes() == a.get_encoded_bytes_test());
178 void bench_num_bytes(bool legacy)
180 const int max = 2500000;
181 auto a = generate_transaction();
184 cout << "get_encoded_bytes_test: ";
186 cout << "get_encoded_bytes: ";
189 utime_t start = ceph_clock_now();
191 for (int i = 0; i < max; ++i) {
192 a.get_encoded_bytes_test();
195 for (int i = 0; i < max; ++i) {
196 a.get_encoded_bytes();
200 utime_t end = ceph_clock_now();
201 cout << max << " encodes in " << (end - start) << std::endl;
205 TEST(Transaction, GetNumBytesBenchLegacy)
207 bench_num_bytes(true);
210 TEST(Transaction, GetNumBytesBenchCurrent)
212 bench_num_bytes(false);