You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/02/25 23:39:43 UTC

[GitHub] [beam] y1chi commented on a change in pull request #13857: [BEAM-10029] Spanner IO read and write performance tests

y1chi commented on a change in pull request #13857:
URL: https://github.com/apache/beam/pull/13857#discussion_r583277558



##########
File path: sdks/python/apache_beam/io/gcp/experimental/spannerio_write_perf_test.py
##########
@@ -0,0 +1,146 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+
+"""
+A pipeline that writes data from Synthetic Source to a Spanner.
+Besides of the standard options, there are options with special meaning:
+* spanner_instance - Spanner Instance ID.
+* spanner_database - Spanner Database ID.
+* input_options - options for Synthetic Source:
+num_records - number of rows to be inserted,
+value_size - the length of a single row,
+key_size - required option, but its value has no meaning.
+
+Example test run on DataflowRunner:
+
+python -m apache_beam.io.gcp.experimental.spannerio_write_perf_test \
+  --test-pipeline-options="
+  --runner=TestDataflowRunner
+  --project='...'
+  --region='...'
+  --temp_location='gs://...'
+  --sdk_location=build/apache-beam.tar.gz
+  --publish_to_big_query=true
+  --metrics_dataset='...'
+  --metrics_table='...'
+  --spanner_instance='...'
+  --spanner_database='...'
+  --input_options='{
+    \"num_records\": 10,
+    \"key_size\": 1,
+    \"value_size\": 1024
+    }'"
+
+This setup will result in a table of 1MB size.
+"""
+
+from __future__ import absolute_import
+
+import logging
+import random
+import uuid
+
+from apache_beam import FlatMap
+from apache_beam import Map
+from apache_beam import ParDo
+from apache_beam.io import Read
+from apache_beam.io.gcp.experimental.spannerio import WriteMutation
+from apache_beam.io.gcp.experimental.spannerio import WriteToSpanner
+from apache_beam.testing.load_tests.load_test import LoadTest
+from apache_beam.testing.load_tests.load_test_metrics_utils import CountMessages
+from apache_beam.testing.load_tests.load_test_metrics_utils import MeasureTime
+from apache_beam.testing.synthetic_pipeline import SyntheticSource
+
+# pylint: disable=wrong-import-order, wrong-import-position
+try:
+  from google.cloud import spanner
+except ImportError:
+  spanner = None
+# pylint: enable=wrong-import-order, wrong-import-position
+
+
+class SpannerWritePerfTest(LoadTest):
+  TEST_DATABASE = None
+
+  def __init__(self):
+    super(SpannerWritePerfTest, self).__init__()
+    self.project = self.pipeline.get_option('project')
+    self.spanner_instance = self.pipeline.get_option('spanner_instance')
+    self.spanner_database = self.pipeline.get_option('spanner_database')
+    self._init_setup()
+
+  def _generate_table_name(self):
+    self.TEST_DATABASE = "{}_{}".format(
+        self.spanner_database, ''.join(random.sample(uuid.uuid4().hex, 4)))
+    return self.TEST_DATABASE
+
+  def _create_database(self):
+    spanner_client = spanner.Client()
+    instance = self._SPANNER_INSTANCE = spanner_client.instance(
+        self.spanner_instance)
+    database = instance.database(
+        self.TEST_DATABASE,
+        ddl_statements=[
+            """CREATE TABLE test (
+            id      STRING(99) NOT NULL,
+            data    BYTES(MAX) NOT NULL
+         ) PRIMARY KEY (id)""",
+        ])
+    database.create()
+
+  def _init_setup(self):
+    """Create database."""
+    self._generate_table_name()
+    self._create_database()
+
+  def test(self):
+    def format_record(record):
+      import base64
+      return base64.b64encode(record[1])
+
+    def make_insert_mutations(element, i):

Review comment:
       Do you mind explain the necessity of feeding the WriteMutation class as an argument instead of directly using it in function?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org