2 * Copyright (C) 2009 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifndef ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_STOPWATCH_H_
30 #define ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_STOPWATCH_H_
36 namespace android_test {
38 // StopWatch class to collect execution statistics.
40 // Once the watch has been created, start and stop can be called to
41 // capture an event duration.
43 // On completion, use 'sprint' to retrieve the data.
45 // If StopWatch::setPrintRawMode(true) has been called, the raw
46 // samples also are printed.
47 // The print method is thread safe to avoid mixing the result of
48 // watches on different threads. For processes, use different files
49 // that you concat after the run.
51 // If the time measure is associated with some volume of data, use
52 // setMbytes, the print method will compute the average throughput
53 // based on that value.
55 // To capture the time accurately and since the runs are not too long,
56 // we collect the raw start and stop time in an array that get
57 // processed once all the measurements have been done.
62 // StopWatch watch("my name", 20);
64 // for (int i = 0; i < 20; ++i) {
70 // char *str = buffer;
71 // size_t size = sizeof(buffer);
72 // watch.sprint(&str, &size);
77 // Time of the snapshot and its nature (start of the interval or end of it).
79 struct timespec mTime;
82 static const size_t kUseDefaultCapacity = 20;
84 // Create a stop watch. Default capacity == 2 * interval_nb
85 // @param name To be used when the results are displayed. No
86 // spaces, use _ instead.
87 // @param capacity Hint about the number of sampless that will be
88 // measured (1 sample == 1 start + 1 stop). Used
89 // to size the internal storage, when the capacity
90 // is reached, it is doubled.
91 StopWatch(const char *name, size_t capacity = kUseDefaultCapacity);
94 // A StopWatch instance measures time intervals. Use setDataSize
95 // if some volume of data is processed during these intervals, to
96 // get the average throughput (in kbytes/s) printed.
97 void setDataSize(size_t size_in_bytes) { mSizeKbytes = size_in_bytes / 1000; }
99 // Starts and stops the timer. The time between the 2 calls is an
100 // interval whose duration will be reported in sprint.
104 // Print a summary of the measurement and optionaly the raw data.
105 // The summary is commented out using a leading '#'. The raw data
106 // is a pair (time, duration). The 1st sample is always at time
108 // @param str[inout] On entry points to the begining of a buffer
109 // where to write the data. On exit points pass the last byte
111 // @param size[inout] On entry points to the size of the buffer
112 // pointed by *str. On exit *size is the amount of free space left
113 // in the buffer. If there was not enough space the data is truncated
114 // and a warning is printed.
115 void sprint(char **str, size_t *size);
117 // @return true if at least one interval was timed.
118 bool used() const { return mUsed; }
120 // Affects all the timers. Instructs all the timers to print the
121 // raw data as well as the summary.
122 static void setPrintRawMode(bool printRaw);
125 void checkCapacity();
126 double timespecToDouble(const struct timespec& time);
127 void printAverageMinMax(char **str, size_t *size);
128 void printThroughput(char **str, size_t *size);
129 // Allocate mDeltas and fill it in. Search for the min and max.
130 void processSamples();
132 char *const mName; // Name of the test.
133 struct timespec mStart;
134 size_t mNum; // # of intervals == # of start() calls.
135 struct Measurement *mData;
140 bool mAlreadyPrinted;
154 } // namespace android_test
156 #endif // ANDROID_NATIVETEST_SYSTEM_EXTRAS_TESTS_SDCARD_STOPWATCH_H_