optimization_guide_test_util.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2020 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. #include "components/optimization_guide/core/optimization_guide_test_util.h"
  5. #include "base/base64.h"
  6. #include "build/build_config.h"
  7. namespace optimization_guide {
  8. #if BUILDFLAG(IS_WIN)
  9. const char kTestAbsoluteFilePath[] = "C:\\absolute\\file\\path";
  10. const char kTestRelativeFilePath[] = "relative\\file\\path";
  11. #else
  12. const char kTestAbsoluteFilePath[] = "/absolutefilepath";
  13. const char kTestRelativeFilePath[] = "relativefilepath";
  14. #endif
  15. std::unique_ptr<proto::PredictionModel> GetMinimalDecisionTreePredictionModel(
  16. double threshold,
  17. double weight) {
  18. auto prediction_model =
  19. std::make_unique<optimization_guide::proto::PredictionModel>();
  20. prediction_model->mutable_model()->mutable_threshold()->set_value(threshold);
  21. optimization_guide::proto::DecisionTree decision_tree_model;
  22. decision_tree_model.set_weight(weight);
  23. optimization_guide::proto::TreeNode* tree_node =
  24. decision_tree_model.add_nodes();
  25. tree_node->mutable_node_id()->set_value(0);
  26. *prediction_model->mutable_model()->mutable_decision_tree() =
  27. decision_tree_model;
  28. return prediction_model;
  29. }
  30. std::unique_ptr<optimization_guide::proto::PredictionModel>
  31. GetSingleLeafDecisionTreePredictionModel(double threshold,
  32. double weight,
  33. double leaf_value) {
  34. auto prediction_model =
  35. GetMinimalDecisionTreePredictionModel(threshold, weight);
  36. prediction_model->mutable_model()
  37. ->mutable_decision_tree()
  38. ->mutable_nodes(0)
  39. ->mutable_leaf()
  40. ->mutable_vector()
  41. ->add_value()
  42. ->set_double_value(leaf_value);
  43. return prediction_model;
  44. }
  45. std::string CreateHintsConfig(
  46. const GURL& hints_url,
  47. optimization_guide::proto::OptimizationType optimization_type,
  48. optimization_guide::proto::Any* metadata) {
  49. optimization_guide::proto::Configuration config;
  50. optimization_guide::proto::Hint* hint = config.add_hints();
  51. hint->set_key(hints_url.host());
  52. hint->set_key_representation(optimization_guide::proto::HOST);
  53. optimization_guide::proto::PageHint* page_hint = hint->add_page_hints();
  54. page_hint->set_page_pattern(hints_url.path().substr(1));
  55. optimization_guide::proto::Optimization* optimization =
  56. page_hint->add_allowlisted_optimizations();
  57. optimization->set_optimization_type(optimization_type);
  58. if (metadata)
  59. *optimization->mutable_any_metadata() = *metadata;
  60. std::string encoded_config;
  61. config.SerializeToString(&encoded_config);
  62. base::Base64Encode(encoded_config, &encoded_config);
  63. return encoded_config;
  64. }
  65. } // namespace optimization_guide