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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. const int rows_cnt = 20;
  22. const int pdp_per_cdp = 60;
  23. void
  24. rspamd_rrd_test_func ()
  25. {
  26. gchar tmpfile[PATH_MAX];
  27. struct rrd_rra_def rra[4];
  28. struct rrd_ds_def ds[2];
  29. GArray ar;
  30. GError *err = NULL;
  31. struct rspamd_rrd_file *rrd;
  32. gdouble ticks;
  33. gint i;
  34. gdouble t[2], cnt = 0.0;
  35. rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd");
  36. unlink (tmpfile);
  37. /* Create sample rrd */
  38. ticks = rspamd_get_calendar_ticks ();
  39. g_assert ((rrd = rspamd_rrd_create (tmpfile, 2, 4, 1, ticks, &err)) != NULL);
  40. /* Add RRA */
  41. rrd_make_default_rra ("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]);
  42. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]);
  43. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]);
  44. rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]);
  45. ar.data = rra;
  46. ar.len = sizeof (rra);
  47. g_assert (rspamd_rrd_add_rra (rrd, &ar, &err));
  48. /* Add DS */
  49. rrd_make_default_ds ("test", "COUNTER", 1, &ds[0]);
  50. rrd_make_default_ds ("test1", "COUNTER", 1, &ds[1]);
  51. ar.data = ds;
  52. ar.len = sizeof (ds);
  53. g_assert (rspamd_rrd_add_ds (rrd, &ar, &err));
  54. /* Finalize */
  55. g_assert (rspamd_rrd_finalize (rrd, &err));
  56. /* Close */
  57. rspamd_rrd_close (rrd);
  58. /* Reopen */
  59. g_assert ((rrd = rspamd_rrd_open (tmpfile, &err)) != NULL);
  60. /* Add some points */
  61. for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i ++) {
  62. t[0] = i;
  63. t[1] = cnt ++;
  64. ar.data = t;
  65. ar.len = sizeof (t);
  66. ticks += 1.0;
  67. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  68. }
  69. /* Add some more points */
  70. for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i ++) {
  71. t[0] = i + rspamd_time_jitter (1.0, 0.0);
  72. t[1] = cnt ++;
  73. ar.data = t;
  74. ar.len = sizeof (t);
  75. ticks += 1.0;
  76. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  77. }
  78. /* Add undefined interval */
  79. ticks += 200;
  80. /* Add some more points */
  81. for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i ++) {
  82. t[0] = i;
  83. t[1] = cnt ++;
  84. ar.data = t;
  85. ar.len = sizeof (t);
  86. ticks += 1.0;
  87. g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err));
  88. }
  89. /* Finish */
  90. rspamd_rrd_close (rrd);
  91. /* unlink (tmpfile); */
  92. }