11cb0ef41Sopenharmony_ci# Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci# found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cifrom . import base
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci# Alphabet size determines the hashing radix. Choosing a prime number prevents
91cb0ef41Sopenharmony_ci# clustering of the hashes.
101cb0ef41Sopenharmony_ciHASHING_ALPHABET_SIZE = 2 ** 7 -1
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cidef radix_hash(capacity, key):
131cb0ef41Sopenharmony_ci  h = 0
141cb0ef41Sopenharmony_ci  for character in key:
151cb0ef41Sopenharmony_ci    h = (h * HASHING_ALPHABET_SIZE + ord(character)) % capacity
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  return h
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass ShardProc(base.TestProcFilter):
211cb0ef41Sopenharmony_ci  """Processor distributing tests between shards.
221cb0ef41Sopenharmony_ci  It hashes the unique test identifiers uses the hash to shard tests.
231cb0ef41Sopenharmony_ci  """
241cb0ef41Sopenharmony_ci  def __init__(self, myid, shards_count):
251cb0ef41Sopenharmony_ci    """
261cb0ef41Sopenharmony_ci    Args:
271cb0ef41Sopenharmony_ci      myid: id of the shard within [0; shards_count - 1]
281cb0ef41Sopenharmony_ci      shards_count: number of shards
291cb0ef41Sopenharmony_ci    """
301cb0ef41Sopenharmony_ci    super(ShardProc, self).__init__()
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    assert myid >= 0 and myid < shards_count
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    self._myid = myid
351cb0ef41Sopenharmony_ci    self._shards_count = shards_count
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  def _filter(self, test):
381cb0ef41Sopenharmony_ci    return self._myid != radix_hash(self._shards_count, test.procid)
39