|
| 1 | +# Copyright 2024 - 2026 Block, Inc. |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style |
| 4 | +# license that can be found in the LICENSE file or at |
| 5 | +# https://opensource.org/licenses/MIT. |
| 6 | +# |
| 7 | +# frozen_string_literal: true |
| 8 | + |
| 9 | +# Run from the repository root: |
| 10 | +# bundle exec ruby -Ispec_support/lib benchmarks/proto_ingestion/record_preparation.rb |
| 11 | + |
| 12 | +require "benchmark" |
| 13 | +require "elastic_graph/indexer/event" |
| 14 | +require "elastic_graph/indexer/record_preparer" |
| 15 | +require "elastic_graph/proto_ingestion/record_preparer" |
| 16 | +require "elastic_graph/proto_ingestion/schema_definition/api_extension" |
| 17 | +require "elastic_graph/schema_definition/test_support" |
| 18 | +require "elastic_graph/spec_support/proto_publisher" |
| 19 | + |
| 20 | +# Original converter from a2fd13432, retained here as a fixed benchmark baseline. |
| 21 | +# Keeping it local makes the benchmark runnable after the PR is squashed and its branch deleted. |
| 22 | +class LegacyRecordConverter |
| 23 | + def initialize(schema) |
| 24 | + @schema = schema |
| 25 | + @types = schema.types |
| 26 | + end |
| 27 | + |
| 28 | + def convert(type_name, message) |
| 29 | + metadata = @types.fetch(type_name) |
| 30 | + if metadata["subtypes"] |
| 31 | + variant = message.class.descriptor.lookup_oneof("value").find { |field| field.has?(message) } |
| 32 | + return nil unless variant |
| 33 | + subtype = variant.subtype.name.delete_prefix("#{@schema.package_name}.") |
| 34 | + return convert(subtype, variant.get(message)).merge("__typename" => subtype) |
| 35 | + end |
| 36 | + |
| 37 | + metadata.fetch("fields").filter_map do |name, field_metadata| |
| 38 | + field = message.class.descriptor.lookup(name) |
| 39 | + next if field.label != :repeated && !field.has?(message) |
| 40 | + converted = convert_typed_value(field_metadata.fetch("type"), field.get(message), field) |
| 41 | + [name, converted] |
| 42 | + end.to_h |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + def convert_typed_value(type, value, field) |
| 48 | + return convert_value(type, value, field) unless type.start_with?("[") |
| 49 | + if value.class.respond_to?(:descriptor) |
| 50 | + field = value.class.descriptor.lookup("values") |
| 51 | + value = field.get(value) |
| 52 | + end |
| 53 | + item_type = type.delete_prefix("[").delete_suffix("]") |
| 54 | + value.map { |item| convert_typed_value(item_type, item, field) } |
| 55 | + end |
| 56 | + |
| 57 | + def convert_value(type_name, value, field) |
| 58 | + metadata = @types.fetch(type_name) |
| 59 | + if (enum_values = metadata["enum_values"]) |
| 60 | + return nil if value == 0 || field.subtype.enummodule.lookup(0) == value |
| 61 | + return enum_values.fetch(value.to_s, value) |
| 62 | + end |
| 63 | + return convert(type_name, value) unless metadata["scalar"] |
| 64 | + return JSON.parse(value) if metadata.fetch("scalar") == "Untyped" |
| 65 | + return JSON.parse(Google::Protobuf.encode_json(value)) if field.type == :message |
| 66 | + value |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +artifacts = ElasticGraph::SchemaDefinition::TestSupport.define_schema( |
| 71 | + schema_element_name_form: :snake_case, |
| 72 | + extension_modules: [ElasticGraph::ProtoIngestion::SchemaDefinition::APIExtension] |
| 73 | +) do |schema| |
| 74 | + schema.enum_type("Status") { |type| type.values "ACTIVE", "onHold" } |
| 75 | + schema.object_type("Attachment") do |type| |
| 76 | + type.field "label", "String", name_in_index: "private_label" |
| 77 | + type.field "size", "LongString" |
| 78 | + end |
| 79 | + schema.object_type("Product") do |type| |
| 80 | + type.field "id", "ID!" |
| 81 | + type.field "name", "String", name_in_index: "private_name" |
| 82 | + type.field "active", "Boolean" |
| 83 | + type.field "large", "LongString" |
| 84 | + type.field "status", "Status" |
| 85 | + type.field "at", "DateTime" |
| 86 | + type.field "payload", "Untyped" |
| 87 | + type.field("attachments", "[Attachment]") { |field| field.mapping type: "object" } |
| 88 | + type.field "grid", "[[Int]]" |
| 89 | + type.index "products" |
| 90 | + end |
| 91 | +end |
| 92 | +publisher = ElasticGraph::SpecSupport::ProtoPublisher.new(artifacts) |
| 93 | +event = ElasticGraph::Indexer::Event.new( |
| 94 | + op: "upsert", type: "Product", id: "p1", version: 1, schema_version: nil, ingestion_format: "proto", |
| 95 | + record: {"id" => "p1", "name" => "sample", "active" => false, "large" => 2**63 - 1, "status" => "onHold", |
| 96 | + "at" => "2024-02-29T12:34:56.123Z", "payload" => {"b" => [1, false, nil], "a" => 2}, |
| 97 | + "attachments" => Array.new(8) { |i| {"label" => "file-#{i}", "size" => 2**53 + i} }, "grid" => [[1, 2], [], [3]]} |
| 98 | +) |
| 99 | +message = publisher.round_trip(event).record |
| 100 | +metadata = ElasticGraph::ProtoIngestion::RuntimeMetadata::Schema.from_schema_artifacts(artifacts) |
| 101 | +legacy_types = metadata.types_by_name.transform_values do |type| |
| 102 | + if type.scalar |
| 103 | + {"scalar" => type.scalar} |
| 104 | + elsif !type.enum_values_by_name.empty? |
| 105 | + {"enum_values" => type.enum_values_by_name} |
| 106 | + elsif !type.subtypes_by_field_name.empty? |
| 107 | + {"subtypes" => type.subtypes_by_field_name.values} |
| 108 | + else |
| 109 | + {"fields" => type.fields_by_name.transform_values { |field| {"type" => field.type.delete("!"), "name_in_index" => field.name_in_index} }} |
| 110 | + end |
| 111 | +end |
| 112 | +# Both paths use the same generated schema, native message, and shared scalar preparers. |
| 113 | +# Descriptor registration, decoding, and preparer construction are outside the measured loop. |
| 114 | +converter = LegacyRecordConverter.new(Data.define(:types, :package_name).new(legacy_types, metadata.package_name)) |
| 115 | +types = metadata.types_by_name.filter_map do |name, type| |
| 116 | + next if type.fields_by_name.empty? |
| 117 | + fields = type.fields_by_name.transform_values do |field| |
| 118 | + ElasticGraph::Indexer::RecordPreparer::FieldMetadata.new(type: field.type, name_in_index: field.name_in_index) |
| 119 | + end |
| 120 | + ElasticGraph::Indexer::RecordPreparer::TypeMetadata.new(name: name, fields_by_name: fields) |
| 121 | +end |
| 122 | +scalar_preparers = artifacts.runtime_metadata.scalar_types_by_name.transform_values { |scalar| scalar.load_indexing_preparer.extension_class } |
| 123 | +shared = ElasticGraph::Indexer::RecordPreparer.new(scalar_preparers, types) |
| 124 | +native = ElasticGraph::ProtoIngestion::RecordPreparer.new(artifacts) |
| 125 | +paths = { |
| 126 | + "old conversion + shared preparation" => -> { shared.prepare_for_index("Product", converter.convert("Product", message), {}) }, |
| 127 | + "native preparation" => -> { native.prepare_for_index("Product", message, {}) } |
| 128 | +} |
| 129 | +raise "Outputs differ" unless paths.values.map(&:call).uniq.size == 1 |
| 130 | +iterations = Integer(ENV.fetch("ITERATIONS", "20000")) |
| 131 | +paths.each do |name, prepare| |
| 132 | + 1_000.times { prepare.call } |
| 133 | + GC.start |
| 134 | + allocated_before = GC.stat(:total_allocated_objects) |
| 135 | + elapsed = Benchmark.realtime { iterations.times { prepare.call } } |
| 136 | + allocations = GC.stat(:total_allocated_objects) - allocated_before |
| 137 | + puts "#{name}: #{elapsed.round(3)}s; #{(iterations / elapsed).round} records/s; #{allocations.fdiv(iterations).round(1)} objects/record" |
| 138 | +end |
| 139 | +puts "Ruby #{RUBY_VERSION}; google-protobuf #{Gem.loaded_specs.fetch("google-protobuf").version}; #{iterations} iterations; decode and initialization excluded" |
0 commit comments