andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/test/field_trial.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cassert> |
| 15 | #include <cstdio> |
| 16 | #include <cstdlib> |
| 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
Henrik Kjellander | 78f65d0 | 2015-10-28 17:17:40 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/field_trial.h" |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/field_trial_default.h" |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 25 | bool field_trials_initiated_ = false; |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 26 | } // namespace |
| 27 | |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 28 | namespace test { |
| 29 | // Note: this code is copied from src/base/metrics/field_trial.cc since the aim |
| 30 | // is to mimic chromium --force-fieldtrials. |
| 31 | void InitFieldTrialsFromString(const std::string& trials_string) { |
| 32 | static const char kPersistentStringSeparator = '/'; |
| 33 | |
pbos | 675117a | 2015-07-07 15:22:27 | [diff] [blame] | 34 | // Catch an error if this is called more than once. |
pbos | 3b5caf9 | 2015-07-10 00:21:06 | [diff] [blame] | 35 | assert(!field_trials_initiated_); |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 36 | field_trials_initiated_ = true; |
| 37 | |
pbos | 849f72f | 2015-07-09 14:48:14 | [diff] [blame] | 38 | if (trials_string.empty()) |
stefan | b2f1caf | 2015-07-07 11:20:34 | [diff] [blame] | 39 | return; |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 40 | |
| 41 | size_t next_item = 0; |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 42 | std::map<std::string, std::string> field_trials; |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 43 | while (next_item < trials_string.length()) { |
| 44 | size_t name_end = trials_string.find(kPersistentStringSeparator, next_item); |
| 45 | if (name_end == trials_string.npos || next_item == name_end) |
| 46 | break; |
| 47 | size_t group_name_end = trials_string.find(kPersistentStringSeparator, |
| 48 | name_end + 1); |
| 49 | if (group_name_end == trials_string.npos || name_end + 1 == group_name_end) |
| 50 | break; |
| 51 | std::string name(trials_string, next_item, name_end - next_item); |
| 52 | std::string group_name(trials_string, name_end + 1, |
| 53 | group_name_end - name_end - 1); |
| 54 | next_item = group_name_end + 1; |
| 55 | |
| 56 | // Fail if duplicate with different group name. |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 57 | if (field_trials.find(name) != field_trials.end() && |
| 58 | field_trials.find(name)->second != group_name) { |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 59 | break; |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 60 | } |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 61 | |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 62 | field_trials[name] = group_name; |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 63 | |
| 64 | // Successfully parsed all field trials from the string. |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 65 | if (next_item == trials_string.length()) { |
| 66 | webrtc::field_trial::InitFieldTrialsFromString(trials_string.c_str()); |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 67 | return; |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 68 | } |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 69 | } |
pbos | 3b5caf9 | 2015-07-10 00:21:06 | [diff] [blame] | 70 | // Using fprintf as LOG does not print when this is called early in main. |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 71 | fprintf(stderr, "Invalid field trials string.\n"); |
| 72 | |
pbos | 3b5caf9 | 2015-07-10 00:21:06 | [diff] [blame] | 73 | // Using abort so it crashes in both debug and release mode. |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 74 | abort(); |
| 75 | } |
pbos | 675117a | 2015-07-07 15:22:27 | [diff] [blame] | 76 | |
| 77 | ScopedFieldTrials::ScopedFieldTrials(const std::string& config) |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 78 | : previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) { |
pbos | 675117a | 2015-07-07 15:22:27 | [diff] [blame] | 79 | assert(field_trials_initiated_); |
| 80 | field_trials_initiated_ = false; |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 81 | current_field_trials_ = config; |
| 82 | InitFieldTrialsFromString(current_field_trials_); |
pbos | 675117a | 2015-07-07 15:22:27 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | ScopedFieldTrials::~ScopedFieldTrials() { |
pbos | 3b5caf9 | 2015-07-10 00:21:06 | [diff] [blame] | 86 | // Should still be initialized, since InitFieldTrials is called from ctor. |
| 87 | // That's why we don't restore the flag. |
| 88 | assert(field_trials_initiated_); |
phoglund | a2932fd | 2016-01-08 13:04:57 | [diff] [blame] | 89 | webrtc::field_trial::InitFieldTrialsFromString(previous_field_trials_); |
pbos | 675117a | 2015-07-07 15:22:27 | [diff] [blame] | 90 | } |
| 91 | |
andresp@webrtc.org | bd49ac2 | 2014-05-16 09:39:51 | [diff] [blame] | 92 | } // namespace test |
| 93 | } // namespace webrtc |