You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamd_rrd_test.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "tests.h"
  18. #include "rrd.h"
  19. #include "rspamd.h"
  20. #include "ottery.h"
  21. #include <unistd.h>
  22. const int rows_cnt = 20;
  23. const int pdp_per_cdp = 60;
  24. void
  25. rspamd_rrd_test_func (void)
  26. {
  27. gchar tmpfile[PATH_MAX];
  28. struct rrd_rra_def rra[4];
  29. struct rrd_ds_def ds[2];
  30. GArray ar;
  31. GError *err = NULL;
  32. struct rspamd_rrd_file *rrd;
  33. gdouble ticks;
  34. gint i;
  35. gdouble t[2], cnt = 0.0;
  36. rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd");
  37. unlink (tmpfile);
  38. /* Create sample rrd */
  39. ticks = rspamd_get_calendar_ticks ();
  40. g_assert ((rrd = rspamd_rrd_create (tmpfile, 2, 4, 1, ticks, &err)) != NULL);
  41. /* Add RRA */
  42. rrd_make_default_rra ("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]);
  43. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]);
  44. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]);
  45. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]);
  46. ar.data = rra;
  47. ar.len = sizeof (rra);
  48. g_assert (rspamd_rrd_add_rra (rrd, &ar, &err));
  49. /* Add DS */
  50. rrd_make_default_ds ("test", "COUNTER", 1, &ds[0]);
  51. rrd_make_default_ds ("test1", "COUNTER", 1, &ds[1]);
  52. ar.data = ds;
  53. ar.len = sizeof (ds);
  54. g_assert (rspamd_rrd_add_ds (rrd, &ar, &err));
  55. /* Finalize */
  56. g_assert (rspamd_rrd_finalize (rrd, &err));
  57. /* Close */
  58. rspamd_rrd_close (rrd);
  59. /* Reopen */
  60. g_assert ((rrd = rspamd_rrd_open (tmpfile, &err)) != NULL);
  61. /* Add some points */
  62. for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i ++) {
  63. t[0] = i;
  64. t[1] = cnt ++;
  65. ar.data = t;
  66. ar.len = sizeof (t);
  67. ticks += 1.0;
  68. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  69. }
  70. /* Add some more points */
  71. for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i ++) {
  72. t[0] = i + rspamd_time_jitter (1.0, 0.0);
  73. t[1] = cnt ++;
  74. ar.data = t;
  75. ar.len = sizeof (t);
  76. ticks += 1.0;
  77. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  78. }
  79. /* Add undefined interval */
  80. ticks += 200;
  81. /* Add some more points */
  82. for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i ++) {
  83. t[0] = i;
  84. t[1] = cnt ++;
  85. ar.data = t;
  86. ar.len = sizeof (t);
  87. ticks += 1.0;
  88. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  89. }
  90. /* Finish */
  91. rspamd_rrd_close (rrd);
  92. /* unlink (tmpfile); */
  93. }