1 /*
2  * Copyright © 2017 Collabora Ltd.
3  *
4  * This file is part of vkmark.
5  *
6  * vkmark is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation, either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * vkmark is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with vkmark. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors:
20  *   Alexandros Frantzis <alexandros.frantzis@collabora.com>
21  */
22 
23 #include "benchmark_collection.h"
24 
25 #include "benchmark.h"
26 #include "scene_collection.h"
27 #include "scene.h"
28 #include "log.h"
29 #include "util.h"
30 
31 namespace
32 {
33 
get_name_from_description(std::string const & s)34 std::string get_name_from_description(std::string const& s)
35 {
36     auto const elems = Util::split(s, ':');
37 
38     return !elems.empty() ? elems[0] : "";
39 }
40 
get_options_from_description(std::string const & s)41 std::vector<Benchmark::OptionPair> get_options_from_description(std::string const& s)
42 {
43     std::vector<Benchmark::OptionPair> options;
44 
45     auto const elems = Util::split(s, ':');
46 
47     if (elems.empty())
48         return options;
49 
50     for (auto iter = elems.begin() + 1;
51          iter != elems.end();
52          ++iter)
53     {
54         auto const opt = Util::split(*iter, '=');
55         if (opt.size() == 2)
56             options.emplace_back(opt[0], opt[1]);
57         else
58             Log::info("Warning: ignoring invalid option string '%s' "
59                       "in benchmark description\n",
60                       iter->c_str());
61     }
62 
63     return options;
64 }
65 
66 }
67 
BenchmarkCollection(SceneCollection & scene_collection)68 BenchmarkCollection::BenchmarkCollection(SceneCollection& scene_collection)
69     : scene_collection{scene_collection},
70       contains_normal_scenes_{false}
71 {
72 }
73 
74 BenchmarkCollection::~BenchmarkCollection() = default;
75 
add(std::vector<std::string> const & benchmark_strings)76 void BenchmarkCollection::add(std::vector<std::string> const& benchmark_strings)
77 {
78     for (auto const& bstr : benchmark_strings)
79     {
80         auto const scene_name = get_name_from_description(bstr);
81         auto const options = get_options_from_description(bstr);
82         auto& scene = scene_collection.get_scene_by_name(scene_name);
83 
84         if (!scene.name().empty())
85             contains_normal_scenes_ = true;
86 
87         benchmarks_.push_back(std::make_unique<Benchmark>(scene, options));
88     }
89 }
90 
benchmarks() const91 std::vector<Benchmark*> BenchmarkCollection::benchmarks() const
92 {
93     std::vector<Benchmark*> benchmarks_raw;
94 
95     for (auto const& b : benchmarks_)
96         benchmarks_raw.push_back(b.get());
97 
98     return benchmarks_raw;
99 }
100 
contains_normal_scenes() const101 bool BenchmarkCollection::contains_normal_scenes() const
102 {
103     return contains_normal_scenes_;
104 }
105