benchmark.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2013 Google Inc. 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. // Author: jdtang@google.com (Jonathan Tang)
  16. //
  17. #include <dirent.h>
  18. #include <fstream>
  19. #include <iostream>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string>
  23. #include <time.h>
  24. #include "gumbo.h"
  25. static const int kNumReps = 10;
  26. int main(int argc, char** argv) {
  27. if (argc != 1) {
  28. std::cout << "Usage: benchmarks\n";
  29. exit(EXIT_FAILURE);
  30. }
  31. DIR* dir;
  32. struct dirent* file;
  33. if ((dir = opendir("benchmarks")) == NULL) {
  34. std::cout << "Couldn't find 'benchmarks' directory. "
  35. << "Run from root of distribution.\n";
  36. exit(EXIT_FAILURE);
  37. }
  38. while ((file = readdir(dir)) != NULL) {
  39. std::string filename(file->d_name);
  40. if (filename.length() > 5 && filename.compare(filename.length() - 5, 5, ".html") == 0) {
  41. std::string full_filename = "benchmarks/" + filename;
  42. std::ifstream in(full_filename.c_str(), std::ios::in | std::ios::binary);
  43. if (!in) {
  44. std::cout << "File " << full_filename << " couldn't be read!\n";
  45. exit(EXIT_FAILURE);
  46. }
  47. std::string contents;
  48. in.seekg(0, std::ios::end);
  49. contents.resize(in.tellg());
  50. in.seekg(0, std::ios::beg);
  51. in.read(&contents[0], contents.size());
  52. in.close();
  53. clock_t start_time = clock();
  54. for (int i = 0; i < kNumReps; ++i) {
  55. GumboOutput* output = gumbo_parse(contents.c_str());
  56. gumbo_destroy_output(&kGumboDefaultOptions, output);
  57. }
  58. clock_t end_time = clock();
  59. std::cout << filename << ": "
  60. << (1000000 * (end_time - start_time) / (kNumReps * CLOCKS_PER_SEC))
  61. << " microseconds.\n";
  62. }
  63. }
  64. closedir(dir);
  65. }