1# Copyright 2018 the V8 project authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from . import base
6
7
8# Alphabet size determines the hashing radix. Choosing a prime number prevents
9# clustering of the hashes.
10HASHING_ALPHABET_SIZE = 2 ** 7 -1
11
12def radix_hash(capacity, key):
13  h = 0
14  for character in key:
15    h = (h * HASHING_ALPHABET_SIZE + ord(character)) % capacity
16
17  return h
18
19
20class ShardProc(base.TestProcFilter):
21  """Processor distributing tests between shards.
22  It hashes the unique test identifiers uses the hash to shard tests.
23  """
24  def __init__(self, myid, shards_count):
25    """
26    Args:
27      myid: id of the shard within [0; shards_count - 1]
28      shards_count: number of shards
29    """
30    super(ShardProc, self).__init__()
31
32    assert myid >= 0 and myid < shards_count
33
34    self._myid = myid
35    self._shards_count = shards_count
36
37  def _filter(self, test):
38    return self._myid != radix_hash(self._shards_count, test.procid)
39