Esempio n. 1
0
  def test_deterministic_coder(self):
    coder = coders.FastPrimitivesCoder()
    deterministic_coder = coders.DeterministicFastPrimitivesCoder(coder, 'step')
    self.check_coder(deterministic_coder, *self.test_values_deterministic)
    for v in self.test_values_deterministic:
      self.check_coder(coders.TupleCoder((deterministic_coder, )), (v, ))
    self.check_coder(
        coders.TupleCoder(
            (deterministic_coder, ) * len(self.test_values_deterministic)),
        tuple(self.test_values_deterministic))

    with self.assertRaises(TypeError):
      self.check_coder(deterministic_coder, dict())
    with self.assertRaises(TypeError):
      self.check_coder(deterministic_coder, [1, dict()])

    self.check_coder(
        coders.TupleCoder((deterministic_coder, coder)), (1, dict()),
        ('a', [dict()]))

    self.check_coder(deterministic_coder, test_message.MessageA(field1='value'))

    if dataclasses is not None:
      self.check_coder(
          deterministic_coder, [FrozenDataClass(1, 2), MyNamedTuple(1, 2)])

      with self.assertRaises(TypeError):
        self.check_coder(deterministic_coder, UnFrozenDataClass(1, 2))
      with self.assertRaises(TypeError):
        self.check_coder(
            deterministic_coder, FrozenDataClass(UnFrozenDataClass(1, 2), 3))
      with self.assertRaises(TypeError):
        self.check_coder(
            deterministic_coder, MyNamedTuple(UnFrozenDataClass(1, 2), 3))
Esempio n. 2
0
    def test_deterministic_coder(self):
        coder = coders.FastPrimitivesCoder()
        deterministic_coder = coders.DeterministicFastPrimitivesCoder(
            coder, 'step')
        self.check_coder(deterministic_coder, *self.test_values_deterministic)
        for v in self.test_values_deterministic:
            self.check_coder(coders.TupleCoder((deterministic_coder, )), (v, ))
        self.check_coder(
            coders.TupleCoder(
                (deterministic_coder, ) * len(self.test_values_deterministic)),
            tuple(self.test_values_deterministic))

        self.check_coder(deterministic_coder, {})
        self.check_coder(deterministic_coder, {2: 'x', 1: 'y'})
        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder, {1: 'x', 'y': 2})
        self.check_coder(deterministic_coder, [1, {}])
        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder, [1, {1: 'x', 'y': 2}])

        self.check_coder(coders.TupleCoder((deterministic_coder, coder)),
                         (1, {}), ('a', [{}]))

        self.check_coder(deterministic_coder,
                         test_message.MessageA(field1='value'))

        self.check_coder(
            deterministic_coder,
            [MyNamedTuple(1, 2), MyTypedNamedTuple(1, 'a')])

        if dataclasses is not None:
            self.check_coder(deterministic_coder, FrozenDataClass(1, 2))

            with self.assertRaises(TypeError):
                self.check_coder(deterministic_coder, UnFrozenDataClass(1, 2))
            with self.assertRaises(TypeError):
                self.check_coder(deterministic_coder,
                                 FrozenDataClass(UnFrozenDataClass(1, 2), 3))
            with self.assertRaises(TypeError):
                self.check_coder(deterministic_coder,
                                 MyNamedTuple(UnFrozenDataClass(1, 2), 3))

        self.check_coder(deterministic_coder, list(MyEnum))
        self.check_coder(deterministic_coder, list(MyIntEnum))
        self.check_coder(deterministic_coder, list(MyIntFlag))
        self.check_coder(deterministic_coder, list(MyFlag))

        self.check_coder(
            deterministic_coder,
            [DefinesGetAndSetState(1),
             DefinesGetAndSetState((1, 2, 3))])

        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder, DefinesGetState(1))
        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder,
                             DefinesGetAndSetState({
                                 1: 'x',
                                 'y': 2
                             }))
Esempio n. 3
0
  def test_default_fallback_path(self):
    """Test fallback path picks a matching coder if no coder is registered."""

    coder = coders_registry.get_coder(DummyClass)
    # No matching coder, so picks the last fallback coder which is a
    # FastPrimitivesCoder.
    self.assertEqual(coder, coders.FastPrimitivesCoder())
    self.assertEqual(DummyClass(), coder.decode(coder.encode(DummyClass())))
Esempio n. 4
0
 def test_fast_primitives_coder(self):
   coder = coders.FastPrimitivesCoder(coders.SingletonCoder(len))
   self.check_coder(coder, None, 1, -1, 1.5, b'str\0str', u'unicode\0\u0101')
   self.check_coder(coder, (), (1, 2, 3))
   self.check_coder(coder, [], [1, 2, 3])
   self.check_coder(coder, dict(), {'a': 'b'}, {0: dict(), 1: len})
   self.check_coder(coder, set(), {'a', 'b'})
   self.check_coder(coder, True, False)
   self.check_coder(coder, len)
   self.check_coder(coders.TupleCoder((coder,)), ('a',), (1,))
Esempio n. 5
0
  def test_deterministic_coder(self):
    coder = coders.FastPrimitivesCoder()
    deterministic_coder = coders.DeterministicFastPrimitivesCoder(coder, 'step')
    self.check_coder(deterministic_coder, 'a', 1, 1.5, (1, 2, 3))
    with self.assertRaises(TypeError):
      self.check_coder(deterministic_coder, dict())
    with self.assertRaises(TypeError):
      self.check_coder(deterministic_coder, [1, dict()])

    self.check_coder(coders.TupleCoder((deterministic_coder, coder)),
                     (1, dict()), ('a', [dict()]))
Esempio n. 6
0
    def test_deterministic_coder(self):
        coder = coders.FastPrimitivesCoder()
        deterministic_coder = coders.DeterministicFastPrimitivesCoder(
            coder, 'step')
        self.check_coder(deterministic_coder, *self.test_values_deterministic)
        for v in self.test_values_deterministic:
            self.check_coder(coders.TupleCoder((deterministic_coder, )), (v, ))
        self.check_coder(
            coders.TupleCoder(
                (deterministic_coder, ) * len(self.test_values_deterministic)),
            tuple(self.test_values_deterministic))

        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder, dict())
        with self.assertRaises(TypeError):
            self.check_coder(deterministic_coder, [1, dict()])

        self.check_coder(coders.TupleCoder((deterministic_coder, coder)),
                         (1, dict()), ('a', [dict()]))
Esempio n. 7
0
 def test_fast_primitives_coder_large_int(self):
     coder = coders.FastPrimitivesCoder()
     self.check_coder(coder, 10**100)
Esempio n. 8
0
 def test_fast_primitives_coder(self):
     coder = coders.FastPrimitivesCoder(coders.SingletonCoder(len))
     self.check_coder(coder, *self.test_values)
     for v in self.test_values:
         self.check_coder(coders.TupleCoder((coder, )), (v, ))
def run_coder_benchmarks(
    num_runs, input_size, seed, verbose, filter_regex='.*'):
  random.seed(seed)

  # TODO(BEAM-4441): Pick coders using type hints, for example:
  # tuple_coder = typecoders.registry.get_coder(typing.Tuple[int, ...])
  benchmarks = [
      coder_benchmark_factory(coders.FastPrimitivesCoder(), small_int),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), large_int),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), small_string),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), large_string),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), small_list),
      coder_benchmark_factory(
          coders.IterableCoder(coders.FastPrimitivesCoder()), small_list),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), large_list),
      coder_benchmark_factory(
          coders.IterableCoder(coders.FastPrimitivesCoder()), large_list),
      coder_benchmark_factory(
          coders.IterableCoder(coders.FastPrimitivesCoder()), large_iterable),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), small_tuple),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), large_tuple),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), small_dict),
      coder_benchmark_factory(coders.FastPrimitivesCoder(), large_dict),
      coder_benchmark_factory(
          coders.ProtoCoder(test_message.MessageWithMap),
          small_message_with_map),
      coder_benchmark_factory(
          coders.ProtoCoder(test_message.MessageWithMap),
          large_message_with_map),
      coder_benchmark_factory(
          coders.DeterministicProtoCoder(test_message.MessageWithMap),
          small_message_with_map),
      coder_benchmark_factory(
          coders.DeterministicProtoCoder(test_message.MessageWithMap),
          large_message_with_map),
      coder_benchmark_factory(
          coders.WindowedValueCoder(coders.FastPrimitivesCoder()),
          wv_with_one_window),
      coder_benchmark_factory(
          coders.WindowedValueCoder(
              coders.FastPrimitivesCoder(), coders.IntervalWindowCoder()),
          wv_with_multiple_windows),
      coder_benchmark_factory(
          coders.WindowedValueCoder(
              coders.FastPrimitivesCoder(), coders.GlobalWindowCoder()),
          globally_windowed_value),
      coder_benchmark_factory(
          coders.LengthPrefixCoder(coders.FastPrimitivesCoder()), small_int)
  ]

  suite = [
      utils.BenchmarkConfig(b, input_size, num_runs) for b in benchmarks
      if re.search(filter_regex, b.__name__, flags=re.I)
  ]
  utils.run_benchmarks(suite, verbose=verbose)
Esempio n. 10
0
def run_coder_benchmarks(num_runs, input_size, seed, verbose):
    random.seed(seed)

    # TODO(BEAM-4441): Pick coders using type hints, for example:
    # tuple_coder = typecoders.registry.get_coder(typehints.Tuple[int, ...])
    benchmarks = [
        coder_benchmark_factory(coders.FastPrimitivesCoder(), small_int),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), large_int),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), small_string),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), large_string),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), small_list),
        coder_benchmark_factory(
            coders.IterableCoder(coders.FastPrimitivesCoder()), small_list),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), large_list),
        coder_benchmark_factory(
            coders.IterableCoder(coders.FastPrimitivesCoder()), large_list),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), small_tuple),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), large_tuple),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), small_dict),
        coder_benchmark_factory(coders.FastPrimitivesCoder(), large_dict),
        coder_benchmark_factory(
            coders.WindowedValueCoder(coders.FastPrimitivesCoder()),
            wv_with_one_window),
        coder_benchmark_factory(
            coders.WindowedValueCoder(coders.FastPrimitivesCoder()),
            wv_with_multiple_windows),
    ]

    suite = [
        utils.BenchmarkConfig(b, input_size, num_runs) for b in benchmarks
    ]
    utils.run_benchmarks(suite, verbose=verbose)