Add missing export and import source files to password_manager's browser target.
[chromium-blink-merge.git] / third_party / ots / test / perf.cc
blobde7ee4145a5748b00e0469d7c14d4b61bb051260
1 // Copyright (c) 2009 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.
5 #include <fcntl.h>
6 #include <sys/stat.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9 #include <time.h>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
15 #include "opentype-sanitiser.h"
16 #include "ots-memory-stream.h"
18 namespace {
20 int Usage(const char *argv0) {
21 std::fprintf(stderr, "Usage: %s <ttf file>\n", argv0);
22 return 1;
25 } // namespace
27 int main(int argc, char **argv) {
28 if (argc != 2) return Usage(argv[0]);
30 const int fd = ::open(argv[1], O_RDONLY);
31 if (fd < 0) {
32 ::perror("open");
33 return 1;
36 struct stat st;
37 ::fstat(fd, &st);
39 uint8_t *data = new uint8_t[st.st_size];
40 if (::read(fd, data, st.st_size) != st.st_size) {
41 std::fprintf(stderr, "Failed to read file!\n");
42 return 1;
45 // A transcoded font is usually smaller than an original font.
46 // However, it can be slightly bigger than the original one due to
47 // name table replacement and/or padding for glyf table.
48 static const size_t kPadLen = 20 * 1024;
49 uint8_t *result = new uint8_t[st.st_size + kPadLen];
51 int num_repeat = 250;
52 if (st.st_size < 1024 * 1024) {
53 num_repeat = 2500;
55 if (st.st_size < 1024 * 100) {
56 num_repeat = 5000;
59 struct timeval start, end, elapsed;
60 ::gettimeofday(&start, 0);
61 for (int i = 0; i < num_repeat; ++i) {
62 ots::MemoryStream output(result, st.st_size + kPadLen);
63 ots::OTSContext context;
64 bool r = context.Process(&output, data, st.st_size);
65 if (!r) {
66 std::fprintf(stderr, "Failed to sanitise file!\n");
67 return 1;
70 ::gettimeofday(&end, 0);
71 timersub(&end, &start, &elapsed);
73 long long unsigned us
74 = ((elapsed.tv_sec * 1000 * 1000) + elapsed.tv_usec) / num_repeat;
75 std::fprintf(stderr, "%llu [us] %s (%llu bytes, %llu [byte/us])\n",
76 us, argv[1], static_cast<long long>(st.st_size),
77 (us ? st.st_size / us : 0));
79 return 0;