Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium 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 | |
| 5 """Tools for gradually enabling a feature on a deterministic set of machines. | |
| 6 | |
| 7 Add a flag to your program to control the percentage of machines that a new | |
| 8 feature should be enabled on:: | |
| 9 | |
| 10 def add_argparse_options(self, parser): | |
| 11 parser.add_argument('--myfeature-percent', type=int, default=0) | |
| 12 | |
| 13 def main(self, opts): | |
| 14 if experiments.is_active_for_host('myfeature', opts.myfeature_percent): | |
| 15 # do myfeature | |
| 16 """ | |
| 17 | |
| 18 import hashlib | |
| 19 import logging | |
| 20 import socket | |
| 21 | |
| 22 | |
| 23 def _is_active(labels, percent): | |
| 24 h = hashlib.md5() | |
| 25 for label, value in labels.iteritems(): | |
|
Vadim Sh.
2015/09/23 17:40:22
sorted(labels.iteritems())
dsansome
2015/09/24 04:34:13
Done.
| |
| 26 h.update(label) | |
| 27 h.update(value) | |
| 28 | |
| 29 digest = h.digest() | |
| 30 | |
| 31 print hash(digest) % 100 | |
|
Vadim Sh.
2015/09/23 17:40:22
remove :)
dsansome
2015/09/24 04:34:13
Oops!
| |
| 32 return (hash(digest) % 100) < percent | |
| 33 | |
| 34 | |
| 35 def is_active_for_host(experiment_name, percent): | |
| 36 ret = _is_active({ | |
| 37 'name': experiment_name, | |
| 38 'host': socket.getfqdn(), | |
|
Vadim Sh.
2015/09/23 17:40:21
This thing can return nonsense (e.g. 1.1.1.1.1.1.a
dsansome
2015/09/24 04:34:13
Nonsense is fine - it's just being hashed anyway.
| |
| 39 }, percent) | |
| 40 | |
| 41 if ret: | |
| 42 logging.info('Experiment "%s" is active', experiment_name) | |
| 43 | |
| 44 return ret | |
| OLD | NEW |