• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/tools/benchmark/benchmark_utils.h"
16 
17 #include <string>
18 #include <vector>
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/profiling/time.h"
23 
24 namespace tflite {
25 namespace benchmark {
26 namespace {
27 
TEST(BenchmarkHelpersTest,SleepForNegativeSeconds)28 TEST(BenchmarkHelpersTest, SleepForNegativeSeconds) {
29   const auto start_ts = tflite::profiling::time::NowMicros();
30   // The following should return immediately.
31   util::SleepForSeconds(-5.0);
32   const auto end_ts = tflite::profiling::time::NowMicros();
33 
34   // As we don't have a mocked clock, we simply expect <1 sec has elapsed, which
35   // is admittedly not quite accurate.
36   EXPECT_LT(end_ts - start_ts, 1000000);
37 }
38 
TEST(BenchmarkHelpersTest,SleepForSomeSeconds)39 TEST(BenchmarkHelpersTest, SleepForSomeSeconds) {
40   const auto start_ts = tflite::profiling::time::NowMicros();
41   // The following should return after 2.0 secs
42   util::SleepForSeconds(2.0);
43   const auto end_ts = tflite::profiling::time::NowMicros();
44 
45   // As we don't have a mocked clock, we simply expect >1.9 sec has elapsed.
46   EXPECT_GT(end_ts - start_ts, 1900000);
47 }
48 
TEST(BenchmarkHelpersTest,SplitAndParseFailed)49 TEST(BenchmarkHelpersTest, SplitAndParseFailed) {
50   std::vector<int> results;
51   const bool splitted = util::SplitAndParse("hello;world", ';', &results);
52 
53   EXPECT_FALSE(splitted);
54 }
55 
TEST(BenchmarkHelpersTest,SplitAndParseString)56 TEST(BenchmarkHelpersTest, SplitAndParseString) {
57   std::vector<std::string> results;
58   const bool splitted = util::SplitAndParse("hello,world", ',', &results);
59 
60   EXPECT_TRUE(splitted);
61   EXPECT_EQ(2, results.size());
62 
63   EXPECT_EQ("hello", results[0]);
64   EXPECT_EQ("world", results[1]);
65 }
66 
TEST(BenchmarkHelpersTest,SplitAndParseInts)67 TEST(BenchmarkHelpersTest, SplitAndParseInts) {
68   std::vector<int> results;
69   const bool splitted = util::SplitAndParse("1,2", ',', &results);
70 
71   EXPECT_TRUE(splitted);
72   EXPECT_EQ(2, results.size());
73 
74   EXPECT_EQ(1, results[0]);
75   EXPECT_EQ(2, results[1]);
76 }
77 
78 }  // namespace
79 }  // namespace benchmark
80 }  // namespace tflite
81