2017-05-26 04:48:44 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# ryw_benchmark.py
|
|
|
|
#
|
|
|
|
# This source file is part of the FoundationDB open source project
|
|
|
|
#
|
|
|
|
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
#
|
2017-05-26 04:48:44 +08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
#
|
2017-05-26 04:48:44 +08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
#
|
2017-05-26 04:48:44 +08:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
|
2022-04-27 23:01:20 +08:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
2017-05-26 04:48:44 +08:00
|
|
|
from python_tests import PythonTest
|
|
|
|
|
|
|
|
import fdb
|
2022-04-27 23:01:20 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
fdb.api_version(400)
|
|
|
|
|
2018-01-25 11:06:58 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class RYWBenchmark(PythonTest):
|
|
|
|
tests = {
|
2022-04-27 23:01:20 +08:00
|
|
|
"get_single": "RYW: get single cached value throughput",
|
|
|
|
"get_many_sequential": "RYW: get sequential cached values throughput",
|
|
|
|
"get_range_basic": "RYW: get range cached values throughput",
|
|
|
|
"single_clear_get_range": "RYW: get range cached values with clears throughput",
|
|
|
|
"clear_range_get_range": "RYW: get range cached values with clear ranges throughput",
|
|
|
|
"interleaved_sets_gets": "RYW: interleaved sets and gets on a single key throughput",
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 07:26:29 +08:00
|
|
|
def __init__(self, key_count=10000, key_size=16):
|
2017-05-26 04:48:44 +08:00
|
|
|
super(RYWBenchmark, self).__init__()
|
2017-06-28 07:26:29 +08:00
|
|
|
self.key_count = key_count
|
|
|
|
self.key_size = key_size
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
try:
|
2022-04-27 23:01:20 +08:00
|
|
|
db = fdb.open(None, "DB")
|
2017-05-26 04:48:44 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2022-04-26 23:28:12 +08:00
|
|
|
except Exception:
|
2022-04-27 23:01:20 +08:00
|
|
|
self.result.add_error(self.get_error("fdb.open failed"))
|
2017-05-26 04:48:44 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.test_performance(db)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2022-04-26 23:28:12 +08:00
|
|
|
except Exception:
|
2022-04-27 23:01:20 +08:00
|
|
|
self.result.add_error(self.get_error("Failed to complete all tests"))
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-06-28 07:26:29 +08:00
|
|
|
def key(self, num):
|
2022-04-27 23:01:20 +08:00
|
|
|
return "%0*d" % (self.key_size, num)
|
2017-06-28 07:26:29 +08:00
|
|
|
|
2018-01-25 11:06:58 +08:00
|
|
|
# Adds the stack trace to an error message
|
2017-05-26 04:48:44 +08:00
|
|
|
def get_error(self, message):
|
2022-04-26 23:28:12 +08:00
|
|
|
error_message = message + "\n" + traceback.format_exc()
|
|
|
|
print(error_message)
|
|
|
|
return error_message
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def test_performance(self, db):
|
|
|
|
tr = db.create_transaction()
|
|
|
|
self.insert_data(tr)
|
|
|
|
|
|
|
|
if not self.args.tests_to_run:
|
|
|
|
self.args.tests_to_run = RYWBenchmark.tests.keys()
|
|
|
|
else:
|
|
|
|
for t in self.args.tests_to_run:
|
2018-01-25 11:06:58 +08:00
|
|
|
if t not in RYWBenchmark.tests:
|
2017-05-26 04:48:44 +08:00
|
|
|
raise Exception("Unknown RYW benchmark test '%s'" % t)
|
|
|
|
|
|
|
|
num_runs = 25
|
|
|
|
|
|
|
|
for test in self.args.tests_to_run:
|
|
|
|
time.sleep(5)
|
2022-04-27 23:01:20 +08:00
|
|
|
print("Running test %s" % test)
|
2017-05-26 04:48:44 +08:00
|
|
|
results = []
|
|
|
|
|
2022-04-27 23:01:20 +08:00
|
|
|
fxn_name = "run_%s" % test
|
|
|
|
assert hasattr(self, fxn_name), (
|
|
|
|
"Test function %s is not implemented" % fxn_name
|
|
|
|
)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
for x in range(0, num_runs):
|
|
|
|
try:
|
|
|
|
results.append(getattr(self, fxn_name)(tr))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2022-04-26 23:28:12 +08:00
|
|
|
except Exception:
|
2022-04-27 23:01:20 +08:00
|
|
|
self.result.add_error(
|
|
|
|
self.get_error(
|
|
|
|
"Performance test failed: " + RYWBenchmark.tests[test]
|
|
|
|
)
|
|
|
|
)
|
2017-05-26 04:48:44 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
if len(results) == num_runs:
|
|
|
|
median = sorted(results)[num_runs / 2]
|
2022-04-27 23:01:20 +08:00
|
|
|
self.result.add_kpi(RYWBenchmark.tests[test], int(median), "keys/s")
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def insert_data(self, tr):
|
2017-06-28 07:26:29 +08:00
|
|
|
del tr[:]
|
2017-05-26 04:48:44 +08:00
|
|
|
for i in range(0, 10000):
|
2022-04-27 23:01:20 +08:00
|
|
|
tr[self.key(i)] = "foo"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def run_get_single(self, tr, count=10000):
|
2018-01-25 11:06:58 +08:00
|
|
|
start = time.time()
|
|
|
|
for i in range(count):
|
|
|
|
tr.get(self.key(5001)).wait()
|
|
|
|
return count / (time.time() - start)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def run_get_many_sequential(self, tr, count=10000):
|
2018-01-25 11:06:58 +08:00
|
|
|
start = time.time()
|
|
|
|
for j in range(count):
|
|
|
|
tr.get(self.key(j)).wait()
|
|
|
|
return count / (time.time() - start)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-06-28 07:26:29 +08:00
|
|
|
def run_get_range_basic(self, tr, count=100):
|
2018-01-25 11:06:58 +08:00
|
|
|
start = time.time()
|
|
|
|
for i in range(count):
|
|
|
|
list(tr.get_range(self.key(0), self.key(self.key_count)))
|
|
|
|
return self.key_count * count / (time.time() - start)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-06-28 07:26:29 +08:00
|
|
|
def run_single_clear_get_range(self, tr, count=100):
|
2018-01-25 11:06:58 +08:00
|
|
|
for i in range(0, self.key_count, 2):
|
|
|
|
tr.clear(self.key(i))
|
|
|
|
start = time.time()
|
|
|
|
for i in range(0, count):
|
|
|
|
list(tr.get_range(self.key(0), self.key(self.key_count)))
|
|
|
|
kpi = self.key_count * count / 2 / (time.time() - start)
|
|
|
|
self.insert_data(tr)
|
|
|
|
return kpi
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-06-28 07:26:29 +08:00
|
|
|
def run_clear_range_get_range(self, tr, count=100):
|
2018-01-25 11:06:58 +08:00
|
|
|
for i in range(0, self.key_count, 4):
|
|
|
|
tr.clear_range(self.key(i), self.key(i + 1))
|
|
|
|
start = time.time()
|
|
|
|
for i in range(0, count):
|
|
|
|
list(tr.get_range(self.key(0), self.key(self.key_count)))
|
|
|
|
kpi = self.key_count * count * 3 / 4 / (time.time() - start)
|
|
|
|
self.insert_data(tr)
|
|
|
|
return kpi
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
def run_interleaved_sets_gets(self, tr, count=10000):
|
2018-01-25 11:06:58 +08:00
|
|
|
start = time.time()
|
2022-04-27 23:01:20 +08:00
|
|
|
tr["foo"] = str(1)
|
2018-01-25 11:06:58 +08:00
|
|
|
for i in range(count):
|
2022-04-27 23:01:20 +08:00
|
|
|
old = int(tr.get("foo").wait())
|
|
|
|
tr.set("foo", str(old + 1))
|
2018-01-25 11:06:58 +08:00
|
|
|
return count / (time.time() - start)
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2022-04-27 23:01:20 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(
|
|
|
|
"Running RYW Benchmark test on Python version %d.%d.%d%s%d"
|
|
|
|
% (
|
|
|
|
sys.version_info[0],
|
|
|
|
sys.version_info[1],
|
|
|
|
sys.version_info[2],
|
|
|
|
sys.version_info[3][0],
|
|
|
|
sys.version_info[4],
|
|
|
|
)
|
|
|
|
)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
tests = sorted(RYWBenchmark.tests.keys())
|
2022-04-27 23:01:20 +08:00
|
|
|
assert len(tests) > 0, "RYW benchmark test has no test_functions"
|
|
|
|
test_string = ", ".join(tests[:-1])
|
2017-05-26 04:48:44 +08:00
|
|
|
if len(tests) > 1:
|
2022-04-27 23:01:20 +08:00
|
|
|
test_string += ", and "
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
test_string += tests[-1]
|
|
|
|
|
2022-04-27 23:01:20 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--tests-to-run",
|
|
|
|
nargs="*",
|
|
|
|
help="Names of tests to run. Can be any of %s. By default, all tests are run."
|
|
|
|
% test_string,
|
|
|
|
)
|
2017-05-26 04:48:44 +08:00
|
|
|
RYWBenchmark().run(parser=parser)
|