speed_test.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* xdelta3 - delta compression tools and library
  2. Copyright 2016 Joshua MacDonald
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "test.h"
  14. usize_t bench_speed(const uint8_t *from_buf, const size_t from_len,
  15. const uint8_t *to_buf, const size_t to_len,
  16. uint8_t *delta_buf, const size_t delta_alloc,
  17. int flags) {
  18. usize_t delta_size;
  19. int ret = xd3_encode_memory(to_buf, to_len, from_buf, from_len,
  20. delta_buf, &delta_size, delta_alloc, flags);
  21. if (ret != 0) {
  22. fprintf(stderr, "encode failure: %d: %s\n", ret, xd3_strerror(ret));
  23. abort();
  24. }
  25. return delta_size;
  26. }
  27. int main(int argc, char **argv) {
  28. int repeat, level;
  29. char *from, *to;
  30. uint8_t *from_buf = NULL, *to_buf = NULL, *delta_buf = NULL;
  31. size_t from_len = 0, to_len, delta_alloc, delta_size = 0;
  32. long start, finish;
  33. int i, ret;
  34. int flags;
  35. if (argc != 5) {
  36. fprintf(stderr, "usage: speed_test LEVEL COUNT FROM TO\n");
  37. return 1;
  38. }
  39. level = atoi(argv[1]);
  40. repeat = atoi(argv[2]);
  41. from = argv[3];
  42. to = argv[4];
  43. flags = (level << XD3_COMPLEVEL_SHIFT) & XD3_COMPLEVEL_MASK;
  44. if ((strcmp(from, "null") != 0 &&
  45. (ret = read_whole_file(from, &from_buf, &from_len))) ||
  46. (ret = read_whole_file(to, &to_buf, &to_len))) {
  47. fprintf(stderr, "read_whole_file error\n");
  48. goto exit;
  49. }
  50. delta_alloc = to_len * 11 / 10;
  51. delta_buf = main_malloc(delta_alloc);
  52. start = get_millisecs_now();
  53. for (i = 0; i < repeat; ++i) {
  54. delta_size = bench_speed(from_buf, from_len,
  55. to_buf, to_len, delta_buf, delta_alloc, flags);
  56. }
  57. finish = get_millisecs_now();
  58. fprintf(stderr,
  59. "STAT: encode %3.2f ms from %s to %s repeat %d %zdbit delta %zd\n",
  60. (double)(finish - start) / repeat, from, to, repeat, sizeof (xoff_t) * 8, delta_size);
  61. ret = 0;
  62. if (0) {
  63. exit:
  64. ret = 1;
  65. }
  66. main_free(to_buf);
  67. main_free(from_buf);
  68. main_free(delta_buf);
  69. return ret;
  70. }