diff --git a/python/kudu/client.pyx b/python/kudu/client.pyx index 261fdbf1ff..ce897a2f76 100644 --- a/python/kudu/client.pyx +++ b/python/kudu/client.pyx @@ -20,6 +20,7 @@ from libcpp.string cimport string from libcpp cimport bool as c_bool +from libcpp.map cimport map cimport cpython from cython.operator cimport dereference as deref @@ -1453,6 +1454,22 @@ cdef class Scanner: result.schema = schema return result + def get_resource_metrics(self): + """ + Return the cumulative resource metrics since the scan was started. + + Returns + ------- + metrics : Dictionary + """ + _map = self.scanner.GetResourceMetrics().Get() + + # Convert map to python dictionary + result = {} + for it in _map: + result[frombytes(it.first)] = it.second + return result + def open(self): """ Returns a reference to itself to facilitate chaining diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd index 9c9899f945..83a9b0375b 100644 --- a/python/kudu/libkudu_client.pxd +++ b/python/kudu/libkudu_client.pxd @@ -21,6 +21,7 @@ from libc.stdint cimport * from libcpp cimport bool as c_bool from libcpp.string cimport string from libcpp.vector cimport vector +from libcpp.map cimport map # This must be included for cerr and other things to work cdef extern from "": @@ -631,6 +632,7 @@ cdef extern from "kudu/client/client.h" namespace "kudu::client" nogil: Status AddExclusiveUpperBound(const KuduPartialRow& key) KuduSchema GetProjectionSchema() + const ResourceMetrics& GetResourceMetrics() string ToString() cdef cppclass KuduScanToken: @@ -681,3 +683,11 @@ cdef extern from "kudu/client/client.h" namespace "kudu::client" nogil: KuduWriteOperation* release_failed_op() c_bool was_possibly_successful() + +cdef extern from "kudu/client/resource_metrics.h" namespace "kudu::client" nogil: + + cdef cppclass ResourceMetrics: + ResourceMetrics() + + map[string, int64_t] Get() + int64_t GetMetric(const string& name) diff --git a/python/kudu/tests/test_scanner.py b/python/kudu/tests/test_scanner.py index 72a22a47e3..f010f36e23 100644 --- a/python/kudu/tests/test_scanner.py +++ b/python/kudu/tests/test_scanner.py @@ -215,6 +215,21 @@ def test_read_mode(self): # Avoid tight looping time.sleep(0.05) + def test_resource_metrics(self): + """ + Test getting the resource metrics after scanning. + """ + + # Build scanner and read through all batches and retrieve metrics. + scanner = self.table.scanner() + scanner.set_fault_tolerant().open() + scanner.read_all_tuples() + metrics = scanner.get_resource_metrics() + + # Confirm that the scanner returned cache hit and miss values. + self.assertTrue('cfile_cache_hit_bytes' in metrics) + self.assertTrue('cfile_cache_miss_bytes' in metrics) + def verify_pred_type_scans(self, preds, row_indexes, count_only=False): # Using the incoming list of predicates, verify that the row returned # matches the inserted tuple at the row indexes specified in a