|
| 1 | +# Copyright 2022 Google LLC. |
| 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 | +# https://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 | + |
| 15 | +"""Benchmarking of Spanner Graph sampling (Cloud Spanner and Borg Spanner).""" |
| 16 | + |
| 17 | +from collections.abc import Callable, Sequence |
| 18 | + |
| 19 | +import dgf |
| 20 | +from dgf.benchmark import utils as benchmark_utils |
| 21 | +from dgf.src.data import schema as schema_lib |
| 22 | +from dgf.src.io.gcp import common as gcp_common_lib |
| 23 | +from dgf.src.io.gcp import spanner_graph_metadata as spanner_graph_metadata_lib |
| 24 | +from dgf.src.sampling import spanner_graph_sampler as spanner_graph_sampler_lib |
| 25 | + |
| 26 | +# Creates a sampler from (sampling config, schema, query engine). |
| 27 | +CreateSamplerFn = Callable[ |
| 28 | + [dgf.sampling.SimpleSamplingConfig, dgf.data.GraphSchema, str], |
| 29 | + spanner_graph_sampler_lib.SpannerGraphSampler, |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def _feature_set_schema( |
| 34 | + element_table: ( |
| 35 | + spanner_graph_metadata_lib.NodeTable |
| 36 | + | spanner_graph_metadata_lib.EdgeTable |
| 37 | + ), |
| 38 | + property_types: dict[str, str], |
| 39 | + skip_primary_keys: bool, |
| 40 | +) -> schema_lib.FeatureSetSchema: |
| 41 | + """Infers the DGF features of an element table from its properties.""" |
| 42 | + definitions = element_table.property_definitions or [] |
| 43 | + return gcp_common_lib.infer_feature_set_schema( |
| 44 | + graph_element_table={ |
| 45 | + definition.property_declaration_name: property_types[ |
| 46 | + definition.property_declaration_name |
| 47 | + ] |
| 48 | + for definition in definitions |
| 49 | + }, |
| 50 | + # The keys are identified by their property (not column) name. |
| 51 | + key_columns=[ |
| 52 | + definition.property_declaration_name |
| 53 | + for definition in definitions |
| 54 | + if definition.value_expression_sql in element_table.key_columns |
| 55 | + ], |
| 56 | + combine_as_json=False, |
| 57 | + skip_primary_keys=skip_primary_keys, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def schema_from_metadata( |
| 62 | + metadata: spanner_graph_metadata_lib.SpannerGraphMetadata, |
| 63 | +) -> dgf.data.GraphSchema: |
| 64 | + """Infers a DGF schema reading all the properties of a Spanner Graph. |
| 65 | +
|
| 66 | + The nodesets and edgesets are named after the element tables. |
| 67 | +
|
| 68 | + Args: |
| 69 | + metadata: Definition of the property graph. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + The DGF schema. |
| 73 | + """ |
| 74 | + property_types = metadata.property_types or {} |
| 75 | + return dgf.data.GraphSchema( |
| 76 | + node_sets={ |
| 77 | + node_table.name: dgf.data.NodeSchema( |
| 78 | + features=_feature_set_schema( |
| 79 | + node_table, property_types, skip_primary_keys=False |
| 80 | + ) |
| 81 | + ) |
| 82 | + for node_table in metadata.node_tables |
| 83 | + }, |
| 84 | + edge_sets={ |
| 85 | + edge_table.name: dgf.data.EdgeSchema( |
| 86 | + source=edge_table.source_node_table.node_table_name, |
| 87 | + target=edge_table.destination_node_table.node_table_name, |
| 88 | + features=_feature_set_schema( |
| 89 | + edge_table, property_types, skip_primary_keys=True |
| 90 | + ), |
| 91 | + ) |
| 92 | + for edge_table in metadata.edge_tables |
| 93 | + }, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +class SpannerGraphSamplingBenchmark(benchmark_utils.Benchmark): |
| 98 | + """Benchmarks subgraph sampling from a Spanner Graph.""" |
| 99 | + |
| 100 | + def __init__( |
| 101 | + self, |
| 102 | + *, |
| 103 | + create_sampler: CreateSamplerFn, |
| 104 | + sampler_name: str, |
| 105 | + schema: dgf.data.GraphSchema, |
| 106 | + seed_nodeset: str, |
| 107 | + seed_ids: Sequence[bytes], |
| 108 | + query_engine: str, |
| 109 | + num_hops: int, |
| 110 | + hop_width: int, |
| 111 | + batch_size: int, |
| 112 | + reverse: bool, |
| 113 | + print_query: bool, |
| 114 | + ): |
| 115 | + self._create_sampler = create_sampler |
| 116 | + self._sampler_name = sampler_name |
| 117 | + self._schema = schema |
| 118 | + self._seed_nodeset = seed_nodeset |
| 119 | + self._seed_ids = seed_ids |
| 120 | + self._query_engine = query_engine |
| 121 | + self._num_hops = num_hops |
| 122 | + self._hop_width = hop_width |
| 123 | + self._batch_size = batch_size |
| 124 | + self._reverse = reverse |
| 125 | + self._print_query = print_query |
| 126 | + self._sampler: spanner_graph_sampler_lib.SpannerGraphSampler | None = None |
| 127 | + self._next_seed = 0 |
| 128 | + self._sum_sampled_nodes = 0 |
| 129 | + self._sum_sampled_edges = 0 |
| 130 | + self._num_samples = 0 |
| 131 | + |
| 132 | + def impl_name(self) -> str: |
| 133 | + return f"{self._sampler_name} {self._query_engine}" |
| 134 | + |
| 135 | + def setup(self): |
| 136 | + self._sampler = self._create_sampler( |
| 137 | + dgf.sampling.SimpleSamplingConfig( |
| 138 | + seed_nodeset=self._seed_nodeset, |
| 139 | + num_hops=self._num_hops, |
| 140 | + hop_width=self._hop_width, |
| 141 | + reverse=self._reverse, |
| 142 | + multi_visit=False, |
| 143 | + ), |
| 144 | + self._schema, |
| 145 | + self._query_engine, |
| 146 | + ) |
| 147 | + if self._print_query: |
| 148 | + print(self._sampler.query) |
| 149 | + |
| 150 | + def run(self): |
| 151 | + """Samples one batch of seeds, different at each call.""" |
| 152 | + assert self._sampler is not None |
| 153 | + batch_seeds = [ |
| 154 | + self._seed_ids[(self._next_seed + i) % len(self._seed_ids)] |
| 155 | + for i in range(self._batch_size) |
| 156 | + ] |
| 157 | + self._next_seed += self._batch_size |
| 158 | + samples = self._sampler.sample(batch_seeds) |
| 159 | + for sample in samples: |
| 160 | + for nodeset in sample.node_sets.values(): |
| 161 | + self._sum_sampled_nodes += nodeset.num_nodes or 0 |
| 162 | + for edgeset in sample.edge_sets.values(): |
| 163 | + self._sum_sampled_edges += edgeset.num_edges() |
| 164 | + self._num_samples += len(samples) |
| 165 | + |
| 166 | + def num_units(self) -> int: |
| 167 | + return self._batch_size |
| 168 | + |
| 169 | + def details(self) -> str: |
| 170 | + num_samples = max(1, self._num_samples) |
| 171 | + return ( |
| 172 | + f"hops={self._num_hops}" |
| 173 | + f" width={self._hop_width}" |
| 174 | + f" batch={self._batch_size}" |
| 175 | + f" reverse={int(self._reverse)}" |
| 176 | + f" nodes/spl.={self._sum_sampled_nodes / num_samples:.1f}" |
| 177 | + f" edges/spl.={self._sum_sampled_edges / num_samples:.1f}" |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +def run_spanner_graph_sampling_benchmark( |
| 182 | + *, |
| 183 | + create_sampler: CreateSamplerFn, |
| 184 | + sampler_name: str, |
| 185 | + schema: dgf.data.GraphSchema, |
| 186 | + seed_nodeset: str, |
| 187 | + seed_ids: Sequence[bytes], |
| 188 | + list_query_engines: Sequence[str] = ("SQL", "GQL"), |
| 189 | + list_num_hops: Sequence[int] = (0, 1, 2), |
| 190 | + list_batch_sizes: Sequence[int] = (1, 32), |
| 191 | + hop_width: int = 10, |
| 192 | + reverse: bool = True, |
| 193 | + repetitions: int = 5, |
| 194 | + warmup_repetitions: int = 1, |
| 195 | + print_query: bool = False, |
| 196 | +) -> None: |
| 197 | + """Runs the Spanner Graph sampling benchmark suite. |
| 198 | +
|
| 199 | + A plan with 0 hops only reads the seed nodes i.e. it measures the latency of |
| 200 | + a round trip to Spanner. |
| 201 | +
|
| 202 | + Args: |
| 203 | + create_sampler: Creates the sampler to benchmark. |
| 204 | + sampler_name: Name of the sampler in the results. |
| 205 | + schema: The DGF schema of the graph. |
| 206 | + seed_nodeset: The seed nodeset. |
| 207 | + seed_ids: Ids of existing nodes of the seed nodeset. Each run samples the |
| 208 | + next batch_size ids. |
| 209 | + list_query_engines: Query engines to benchmark. |
| 210 | + list_num_hops: Number of hops to benchmark. |
| 211 | + list_batch_sizes: Number of seeds per query to benchmark. |
| 212 | + hop_width: Number of neighbors sampled per hop. |
| 213 | + reverse: Whether the edges are also traversed backward. |
| 214 | + repetitions: Number of timed queries per setup. |
| 215 | + warmup_repetitions: Number of non-timed queries per setup. |
| 216 | + print_query: Whether to print the sampling query of each setup. |
| 217 | + """ |
| 218 | + benchmarker = benchmark_utils.Benchmarker() |
| 219 | + for num_hops in list_num_hops: |
| 220 | + for batch_size in list_batch_sizes: |
| 221 | + for query_engine in list_query_engines: |
| 222 | + benchmarker.run( |
| 223 | + SpannerGraphSamplingBenchmark( |
| 224 | + create_sampler=create_sampler, |
| 225 | + sampler_name=sampler_name, |
| 226 | + schema=schema, |
| 227 | + seed_nodeset=seed_nodeset, |
| 228 | + seed_ids=seed_ids, |
| 229 | + query_engine=query_engine, |
| 230 | + num_hops=num_hops, |
| 231 | + hop_width=hop_width, |
| 232 | + batch_size=batch_size, |
| 233 | + reverse=reverse, |
| 234 | + print_query=print_query, |
| 235 | + ), |
| 236 | + repetitions=repetitions, |
| 237 | + warmup_repetitions=warmup_repetitions, |
| 238 | + ) |
| 239 | + benchmarker.add_separator() |
| 240 | + benchmarker.print_results() |
0 commit comments