package tezt-tezos

  1. Overview
  2. Docs
Octez test framework based on Tezt

Install

Dune Dependency

Authors

Maintainers

Sources

tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65

doc/src/tezt-tezos.tezt-performance-regression/long_test.ml.html

Source file long_test.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type alert_config = {
  default_slack_webhook_url : Uri.t;
  team_slack_webhook_urls : Uri.t String_map.t;
  max_total : int;
  max_by_test : int;
  gitlab_project_url : Uri.t option;
  timeout : float;
  (* minimum delay between two alerts for the same category, in seconds *)
  rate_limit_per_category : float;
  last_alerts_filename : string;
  max_alert_size : int;
  max_alert_lines : int;
}

type config = {
  alerts : alert_config option;
  influxdb : InfluxDB.config option;
  grafana : Grafana.config option;
  test_data_path : string;
}

let default_test_data_path = "/s3data/"

let default default = Option.value ~default

(* the default is one day ( in seconds ) *)
let default_rate_limit_per_category = 86400.0

let as_alert_config json =
  let slack_webhook_urls = JSON.(json |-> "slack_webhook_urls") in
  let team_slack_webhook_urls =
    JSON.as_object slack_webhook_urls
    |> List.filter_map (fun (name, url) ->
           if name = "default" then None
           else Some (name, JSON.(url |> as_string |> Uri.of_string)))
    |> List.fold_left
         (fun acc (k, v) -> String_map.add k v acc)
         String_map.empty
  in
  {
    default_slack_webhook_url =
      JSON.(slack_webhook_urls |-> "default" |> as_string |> Uri.of_string);
    team_slack_webhook_urls;
    max_total = JSON.(json |-> "max_total" |> as_int_opt |> default 100);
    max_by_test = JSON.(json |-> "max_by_test" |> as_int_opt |> default 2);
    gitlab_project_url =
      JSON.(
        json |-> "gitlab_project_url" |> as_string_opt
        |> Option.map Uri.of_string);
    timeout = JSON.(json |-> "timeout" |> as_float_opt |> default 20.);
    rate_limit_per_category =
      JSON.(
        json |-> "rate_limit_per_category" |> as_float_opt
        |> Option.value ~default:default_rate_limit_per_category);
    last_alerts_filename =
      JSON.(
        json |-> "last_alerts_filename" |> as_string_opt
        |> Option.value ~default:"last_alerts.json");
    max_alert_size =
      JSON.(
        json |-> "max_alert_size" |> as_int_opt |> Option.value ~default:1000);
    max_alert_lines =
      JSON.(
        json |-> "max_alert_lines" |> as_int_opt |> Option.value ~default:20);
  }

let read_config_file filename =
  let json = JSON.parse_file filename in
  {
    alerts = JSON.(json |-> "alerts" |> as_opt |> Option.map as_alert_config);
    influxdb =
      JSON.(json |-> "influxdb" |> as_opt |> Option.map InfluxDB.config_of_json);
    grafana =
      JSON.(json |-> "grafana" |> as_opt |> Option.map Grafana.config_of_json);
    test_data_path =
      JSON.(
        json |-> "test_data_path" |> as_string_opt
        |> default default_test_data_path);
  }

let default_config =
  {
    alerts = None;
    influxdb = None;
    grafana = None;
    test_data_path = default_test_data_path;
  }

let config = ref default_config

let test_data_path () = !config.test_data_path

let init () =
  (config :=
     match
       match Sys.getenv_opt "TEZT_CONFIG" with
       | Some "" | None -> (
           if Sys.file_exists "tezt_config.json" then Some "tezt_config.json"
           else
             match Sys.getenv_opt "HOME" with
             | Some home ->
                 let filename = home // ".tezt_config.json" in
                 if Sys.file_exists filename then Some filename else None
             | None -> None)
       | Some _ as x -> x
     with
     | None ->
         Log.warn "No configuration file found, using default configuration." ;
         default_config
     | Some filename -> (
         Log.info "Using configuration file: %s" filename ;
         try read_config_file filename
         with JSON.Error error ->
           Log.error
             "Failed to read configuration file: %s"
             (JSON.show_error error) ;
           exit 1)) ;
  if !config.alerts = None then
    Log.warn "Alerts are not configured and will thus not be sent." ;
  if !config.influxdb = None then
    Log.warn
      "InfluxDB is not configured: data points will not be sent and previous \
       data points will not be read. Also, Grafana dashboards will not be \
       updated." ;
  if !config.grafana = None then
    Log.warn
      "Grafana is not configured: Grafana dashboards will not be updated."

module Alerts : sig
  type category = string

  val load : alert_config -> unit

  val dump : alert_config -> unit

  val may_send_rate_limit : alert_config -> category option -> bool

  val add : category option -> float -> unit
end = struct
  type category = string

  module Map = Map.Make (String)

  (* a map with the last messages used for rate limit
   * and dumped on disc on_exit *)
  let last_messages = ref Map.empty

  let default_alert_category = ""

  let may_send_rate_limit alert_cfg category =
    let now = Unix.gettimeofday () in
    let c = Option.value ~default:default_alert_category category in
    match Map.find_opt c !last_messages with
    | None -> true
    | Some t -> now >= t +. alert_cfg.rate_limit_per_category

  let add category now =
    let category = Option.value ~default:default_alert_category category in
    last_messages := Map.add category now !last_messages

  let encode (c, t) = `O [("category", `String c); ("time", `Float t)]

  let decode json =
    let category = JSON.(json |-> "category" |> as_string) in
    let now = Unix.gettimeofday () in
    let time =
      JSON.(json |-> "time" |> as_float_opt |> Option.value ~default:now)
    in
    (category, time)

  let to_json_array al =
    JSON.annotate ~origin:"Alert List" (`A (List.map encode al))

  (* we load the alerts and filter out all alerts older than one [rate_limit_per_category] *)
  let load_alerts config json =
    let now = Unix.gettimeofday () in
    JSON.as_list json |> List.map decode
    |> List.filter (fun (_, alert_time) ->
           now -. alert_time < config.rate_limit_per_category)
    |> List.fold_left (fun a (k, v) -> Map.add k v a) Map.empty

  let read_file config =
    if Sys.file_exists config.last_alerts_filename then
      JSON.(parse_file config.last_alerts_filename |> load_alerts config)
    else Map.empty

  let load config = last_messages := read_file config

  let dump config =
    let now = Unix.gettimeofday () in
    let al =
      !last_messages
      |> Map.filter (fun _ t -> now < t +. config.rate_limit_per_category)
      |> Map.bindings
    in
    let ja = to_json_array al in
    JSON.encode_to_file config.last_alerts_filename ja
end

type category = Alerts.category

type timeout = Seconds of int | Minutes of int | Hours of int | Days of int

let with_timeout timeout promise () =
  let timeout =
    match timeout with
    | Seconds x -> x
    | Minutes x -> x * 60
    | Hours x -> x * 60 * 60
    | Days x -> x * 60 * 60 * 24
  in
  Lwt.pick
    [
      promise;
      (let* () = Lwt_unix.sleep (float timeout) in
       Test.fail "test did not finish before its timeout");
    ]

(* [data_points] is a map from measurement to lists.
   The order of those lists is unspecified. *)
type current_test = {
  title : string;
  filename : string;
  team : string option;
  mutable data_points : InfluxDB.data_point list String_map.t;
  mutable alert_count : int;
}

(* Using a global variable will make it hard to refactor to run
   multiple tests concurrently if we want to. But, running multiple
   tests would affect time measurements so it is not advisable anyway. *)
let current_test = ref None

let total_alert_count = ref 0

let http ~timeout method_ ?headers ?body url =
  let http_call =
    let headers = Option.map Cohttp.Header.of_list headers in
    let body = Option.map (fun s -> `String s) body in
    Cohttp_lwt_unix.Client.call ?headers ?body method_ url
  in
  let timeout =
    let* () = Lwt_unix.sleep timeout in
    failwith "timeout"
  in
  Lwt.pick [http_call; timeout]

let http_post_json url body =
  http
    `POST
    ~headers:[("Content-Type", "application/json")]
    ~body:(JSON.encode_u body)
    url

let with_buffer size f =
  let buffer = Buffer.create size in
  f buffer ;
  Buffer.contents buffer

module Slack = struct
  let encode_entities buffer s =
    for i = 0 to String.length s - 1 do
      match s.[i] with
      | '&' -> Buffer.add_string buffer "&amp;"
      | '<' -> Buffer.add_string buffer "&lt;"
      | '>' -> Buffer.add_string buffer "&gt;"
      | c -> Buffer.add_char buffer c
    done

  type message_item =
    | Text of string
    | Newline
    | Link of {url : Uri.t; text : string}

  type message = message_item list

  let encode_message_item buffer = function
    | Text s -> encode_entities buffer s
    | Newline -> Buffer.add_char buffer '\n'
    | Link {url; text} ->
        Buffer.add_char buffer '<' ;
        Buffer.add_string buffer (Uri.to_string url) ;
        Buffer.add_char buffer '|' ;
        encode_entities buffer text ;
        Buffer.add_char buffer '>'

  let send_message ~timeout webhook_url message =
    let message =
      with_buffer 256 @@ fun buffer ->
      List.iter (encode_message_item buffer) message
    in
    let body = `O [("text", `String message)] in
    let send () =
      let* response, body = http_post_json ~timeout webhook_url body in
      match response.status with
      | #Cohttp.Code.success_status -> Cohttp_lwt.Body.drain_body body
      | status ->
          let* body = Cohttp_lwt.Body.to_string body in
          Log.debug "Response body from Slack: %s" body ;
          Log.warn
            "Failed to send message: Slack responded with %s"
            (Cohttp.Code.string_of_status status) ;
          unit
    in
    Lwt.catch send @@ fun exn ->
    Log.warn "Failed to send message to Slack: %s" (Printexc.to_string exn) ;
    unit
end

let shorten_message ~max_size ~max_lines message =
  let max_size = max 1 max_size in
  let max_lines = max 1 max_lines in
  let message_length = String.length message in
  let rec shortened_length newlines i =
    if i >= max_size then max_size
    else if i >= message_length then message_length
    else
      match message.[i] with
      | '\n' ->
          let newlines = newlines + 1 in
          if newlines >= max_lines then i else shortened_length newlines (i + 1)
      | _ -> shortened_length newlines (i + 1)
  in
  let new_length = shortened_length 0 0 in
  if new_length < String.length message then
    String.sub message 0 (max 0 new_length) ^ "[...]"
  else message

let alert_s ?category ~log message =
  if log then Log.error "Alert: %s" message ;
  match !config.alerts with
  | None -> ()
  | Some alert_cfg ->
      let message =
        shorten_message
          ~max_size:alert_cfg.max_alert_size
          ~max_lines:alert_cfg.max_alert_lines
          message
      in
      let may_send_rate_limit = Alerts.may_send_rate_limit alert_cfg category in
      let may_send =
        !total_alert_count < alert_cfg.max_total
        &&
        match !current_test with
        | None -> true
        | Some {alert_count; _} -> alert_count < alert_cfg.max_by_test
      in
      if not may_send_rate_limit then
        Log.debug "Alert not sent because of rate limit." ;
      if may_send && may_send_rate_limit then
        let () = Alerts.add category (Unix.gettimeofday ()) in
        let () = Alerts.dump alert_cfg in
        let slack_webhook_url =
          match !current_test with
          | Some {team = Some team; _} -> (
              match
                String_map.find_opt team alert_cfg.team_slack_webhook_urls
              with
              | None ->
                  Log.warn
                    "No Slack webhook configured for team %S, will use the \
                     default."
                    team ;
                  alert_cfg.default_slack_webhook_url
              | Some url -> url)
          | _ -> alert_cfg.default_slack_webhook_url
        in
        let message : Slack.message =
          match !current_test with
          | None -> [Text "Alert: "; Text message]
          | Some {title; filename; _} -> (
              let text =
                let message =
                  (* If [message] is multi-line, put "Alert from test" on its own line. *)
                  if String.contains message '\n' then "\n" ^ message
                  else message
                in
                Slack.Text (sf "Alert from test %S: %s" title message)
              in
              match alert_cfg.gitlab_project_url with
              | None -> [text]
              | Some gitlab_project_url ->
                  let new_issue_url =
                    let issue_title = sf "Fix test: %s" title in
                    let issue_description =
                      sf "Test: %s\nFile: %s\nAlert: %s" title filename message
                    in
                    let url =
                      let path = Uri.path gitlab_project_url in
                      Uri.with_path gitlab_project_url (path ^ "/-/issues/new")
                    in
                    Uri.add_query_params'
                      url
                      [
                        ("issue[title]", issue_title);
                        ("issue[description]", issue_description);
                      ]
                  in
                  [
                    text;
                    Newline;
                    Link {url = new_issue_url; text = "create issue"};
                  ])
        in
        (* Using [Background.register] is not just about returning type [unit]
           instead of a promise, it also prevents the timeout of the test from
           canceling the alert. *)
        Background.register
        @@ Slack.send_message
             ~timeout:alert_cfg.timeout
             slack_webhook_url
             message

let alert ?category = Printf.ksprintf (alert_s ?category ~log:true)

let alert_exn exn x =
  Printf.ksprintf
    (fun s ->
      Log.error "Alert: %s: %s" s (Printexc.to_string exn) ;
      alert_s ~category:s ~log:false s)
    x

let add_data_point data_point =
  match !current_test with
  | None ->
      invalid_arg
        "Long_test.add_data_point: not running a test registered with Long_test"
  | Some test ->
      (* Title has already been checked for newline characters in [register]. *)
      let data_point = InfluxDB.add_tag "test" test.title data_point in
      Log.debug "Data point: %s" (InfluxDB.show_data_point data_point) ;
      let previous_data_points =
        String_map.find_opt data_point.measurement test.data_points
        |> Option.value ~default:[]
      in
      test.data_points <-
        String_map.add
          data_point.measurement
          (data_point :: previous_data_points)
          test.data_points

let send_data_points () =
  match (!current_test, !config.influxdb) with
  | None, _ | _, None -> unit
  | Some test, Some config ->
      let write () =
        let data_points =
          test.data_points |> String_map.bindings |> List.map snd
          |> List.flatten
        in
        test.data_points <- String_map.empty ;
        match data_points with
        | [] -> unit
        | _ ->
            let* () = InfluxDB.write config data_points in
            Log.debug
              "Successfully sent %d data points."
              (List.length data_points) ;
            unit
      in
      Lwt.catch write (fun exn ->
          alert_exn exn "failed to send data points to InfluxDB" ;
          unit)

let unsafe_query select extract_data =
  match !config.influxdb with
  | None ->
      Log.debug
        "InfluxDB is not configured, will not perform query: %s"
        (InfluxDB.show_select select) ;
      none
  | Some config ->
      let query () =
        let* result = InfluxDB.query config select in
        some (extract_data result)
      in
      Lwt.catch query (fun exn ->
          Log.debug "Query: %s" (InfluxDB.show_select select) ;
          alert_exn exn "failed to perform InfluxDB query" ;
          none)

let log_unsafe_query select =
  let* result = unsafe_query select Fun.id in
  Option.iter
    (fun result ->
      Log.debug "Query: %s" (InfluxDB.show_select select) ;
      match result with
      | [] -> Log.debug "No results for this query."
      | _ -> Log.debug "%s" (InfluxDB.show_query_result result))
    result ;
  unit

let query select extract_data =
  match !current_test with
  | None ->
      invalid_arg
        "Long_test.query: not running a test registered with Long_test"
  | Some {title; _} ->
      let rec add_clause (select : InfluxDB.select) =
        match select.from with
        | Select sub_select ->
            {select with from = Select (add_clause sub_select)}
        | Measurement _ -> (
            let where_test : InfluxDB.where = Tag ("test", EQ, title) in
            match select.where with
            | None -> {select with where = Some where_test}
            | Some where -> {select with where = Some (And (where, where_test))}
            )
      in
      unsafe_query (add_clause select) extract_data

module Stats = struct
  type _ t =
    | Int : InfluxDB.func -> int t
    | Float : InfluxDB.func -> float t
    | Pair : 'a t * 'b t -> ('a * 'b) t
    | Convert : 'a t * ('b -> 'a) * ('a -> 'b) -> 'b t

  let count = Int COUNT

  let mean = Float MEAN

  let median = Float MEDIAN

  let stddev = Float STDDEV

  let _2 a b = Pair (a, b)

  let _3 a b c =
    Convert
      ( Pair (a, Pair (b, c)),
        (fun (a, b, c) -> (a, (b, c))),
        fun (a, (b, c)) -> (a, b, c) )

  let rec functions : 'a. 'a t -> _ =
    fun (type a) (stats : a t) ->
     match stats with
     | Int func | Float func -> [func]
     | Pair (a, b) -> functions a @ functions b
     | Convert (stats, _, _) -> functions stats

  let rec get : 'a. _ -> 'a t -> 'a =
    fun (type a) result_data_point (stats : a t) ->
     let result : a =
       match stats with
       | Int func ->
           InfluxDB.get
             (InfluxDB.column_name_of_func func)
             JSON.as_int
             result_data_point
       | Float func ->
           InfluxDB.get
             (InfluxDB.column_name_of_func func)
             JSON.as_float
             result_data_point
       | Pair (a, b) -> (get result_data_point a, get result_data_point b)
       | Convert (stats, _, decode) -> decode (get result_data_point stats)
     in
     result

  let show stats values =
    let rec gather : 'a. 'a t -> 'a -> _ =
      fun (type a) (stats : a t) (values : a) ->
       match stats with
       | Int func -> [(InfluxDB.column_name_of_func func, string_of_int values)]
       | Float func ->
           [(InfluxDB.column_name_of_func func, string_of_float values)]
       | Pair (a, b) ->
           let v, w = values in
           gather a v @ gather b w
       | Convert (stats, encode, _) -> gather stats (encode values)
    in
    gather stats values
    |> List.map (fun (name, value) -> sf "%s = %s" name value)
    |> String.concat ", "
end

let get_previous_stats ?limit ?(minimum_count = 3) ?(tags = []) measurement
    field stats =
  Option.iter
    (fun limit ->
      if limit < minimum_count then
        invalid_arg
        @@ sf
             "Long_test.get_previous_stats: limit = %d must be at least equal \
              to minimum_count = %d"
             limit
             minimum_count)
    limit ;
  let stats = Stats.(_2 count) stats in
  let select =
    let where =
      match tags with
      | [] -> None
      | head :: tail ->
          let where_tag (tag, value) = InfluxDB.Tag (tag, EQ, value) in
          let where =
            List.fold_left
              (fun acc tag -> InfluxDB.And (acc, where_tag tag))
              (where_tag head)
              tail
          in
          Some where
    in
    InfluxDB.(
      select
        (List.map
           (fun func -> Function (func, Field field))
           (Stats.functions stats))
        ~from:
          (Select
             (select
                [Field field]
                ?where
                ~from:(Measurement measurement)
                ~order_by:Time_desc
                ?limit))
        ~order_by:Time_desc)
  in
  let* result =
    query select @@ fun result ->
    match result with
    | [] -> None
    | _ :: _ :: _ -> failwith "InfluxDB result contains multiple series"
    | [[]] -> failwith "InfluxDB result contains no values"
    | [_ :: _ :: _] -> failwith "InfluxDB result contains multiple values"
    | [[value]] ->
        let ((count, _) as stats) = Stats.get value stats in
        if count < minimum_count then None else Some stats
  in
  return (Option.join result)

let has_tags (tags : (InfluxDB.tag * string) list)
    (data_point : InfluxDB.data_point) =
  let has_tag (tag, expected_value) =
    match List.assoc_opt tag data_point.tags with
    | None -> false
    | Some value -> String.equal value expected_value
  in
  List.for_all has_tag tags

let get_pending_data_points ?(tags = []) measurement =
  match !current_test with
  | None ->
      invalid_arg
        "Long_test.get_pending_data_points: not running a test registered with \
         Long_test"
  | Some test ->
      test.data_points
      |> String_map.find_opt measurement
      |> Option.value ~default:[]
      |> List.filter (has_tags tags)

type check = Mean | Median

let check_regression ?(previous_count = 10) ?(minimum_previous_count = 3)
    ?(margin = 0.2) ?(check = Mean) ?(stddev = false) ?data_points ?(tags = [])
    measurement field =
  if !current_test = None then
    invalid_arg
      "Long_test.check_regression: not running a test registered with Long_test" ;
  let current_values =
    let data_points =
      match data_points with
      | Some list -> List.filter (has_tags tags) list
      | None -> get_pending_data_points ~tags measurement
    in
    let get_field (data_point : InfluxDB.data_point) =
      match
        List.assoc_opt field (data_point.first_field :: data_point.other_fields)
      with
      | None | Some (String _) -> None
      | Some (Float f) -> Some f
    in
    List.filter_map get_field data_points
  in
  match current_values with
  | [] -> unit
  | _ :: _ -> (
      let current_value =
        match check with
        | Mean -> Statistics.mean current_values
        | Median -> Statistics.median current_values
      in
      let get_previous stats handle_values =
        let* values =
          get_previous_stats
            ~limit:previous_count
            ~minimum_count:minimum_previous_count
            ~tags
            measurement
            field
            stats
        in
        match values with
        | None ->
            Log.debug "Not enough previous data points." ;
            unit
        | Some values ->
            Log.debug
              "Previous data points: %s"
              (Stats.show Stats.(_2 count stats) values) ;
            handle_values values ;
            unit
      in
      let get_previous_with_stddev stats handle_values =
        if stddev then
          get_previous Stats.(_2 stats stddev) @@ fun (count, (values, _)) ->
          handle_values (count, values)
        else get_previous stats handle_values
      in
      let get_previous_and_check name stats =
        get_previous_with_stddev stats
        @@ fun (previous_count, previous_value) ->
        if current_value > previous_value *. (1. +. margin) then
          let tags =
            match tags with
            | [] -> ""
            | _ ->
                "["
                ^ (List.map (fun (k, v) -> sf "%S = %S" k v) tags
                  |> String.concat ", ")
                ^ "]"
          in
          alert
            "New measurement: %s(%S%s.%S) = %g\n\
             Previous %d measurements: %s = %g\n\
             Difference: +%d%% (alert threshold: %d%%)"
            name
            measurement
            tags
            field
            current_value
            previous_count
            name
            previous_value
            (int_of_float ((current_value *. 100. /. previous_value) -. 100.))
            (int_of_float (margin *. 100.))
      in
      match check with
      | Mean -> get_previous_and_check "mean" Stats.mean
      | Median -> get_previous_and_check "median" Stats.median)

let check_time_preconditions measurement =
  if !current_test = None then
    invalid_arg "Long_test.time: not running a test registered with Long_test" ;
  if String.contains measurement '\n' then
    invalid_arg "Long_test.time: newline character in measurement"

let measure_and_check_regression ?previous_count ?minimum_previous_count ?margin
    ?check ?stddev ?(repeat = 1) ?(tags = []) measurement f =
  check_time_preconditions measurement ;
  if repeat <= 0 then unit
  else
    let data_points = ref [] in
    for _ = 1 to repeat do
      let duration = f () in
      let data_point =
        InfluxDB.data_point ~tags measurement ("duration", Float duration)
      in
      add_data_point data_point ;
      data_points := data_point :: !data_points
    done ;
    check_regression
      ?previous_count
      ?minimum_previous_count
      ?margin
      ?check
      ?stddev
      ~tags
      ~data_points:!data_points
      measurement
      "duration"

let time ?previous_count ?minimum_previous_count ?margin ?check ?stddev ?repeat
    ?tags measurement f =
  measure_and_check_regression
    ?previous_count
    ?minimum_previous_count
    ?margin
    ?check
    ?stddev
    ?repeat
    ?tags
    measurement
    (fun () ->
      let start = Unix.gettimeofday () in
      f () ;
      let stop = Unix.gettimeofday () in
      stop -. start)

let measure_and_check_regression_lwt ?previous_count ?minimum_previous_count
    ?margin ?check ?stddev ?(repeat = 1) ?(tags = []) measurement f =
  check_time_preconditions measurement ;
  if repeat <= 0 then unit
  else
    let data_points = ref [] in
    let* () =
      Base.repeat repeat @@ fun () ->
      let* duration = f () in
      let data_point =
        InfluxDB.data_point ~tags measurement ("duration", Float duration)
      in
      add_data_point data_point ;
      data_points := data_point :: !data_points ;
      unit
    in
    check_regression
      ?previous_count
      ?minimum_previous_count
      ?margin
      ?check
      ?stddev
      ~tags
      ~data_points:!data_points
      measurement
      "duration"

let time_lwt ?previous_count ?minimum_previous_count ?margin ?check ?stddev
    ?repeat ?tags measurement f =
  measure_and_check_regression_lwt
    ?previous_count
    ?minimum_previous_count
    ?margin
    ?check
    ?stddev
    ?repeat
    ?tags
    measurement
    (fun () ->
      let start = Unix.gettimeofday () in
      let* () = f () in
      let stop = Unix.gettimeofday () in
      Lwt.return (stop -. start))

(* Executors are just test tags. But the type is abstract so that users of this module
   cannot use an inexistent executor by mistake. And inside this module we use
   a type constructor to avoid mistakes too. *)
type executor = Executor of string [@@unboxed]

let x86_executor1 = Executor "x86_executor1"

let x86_executor2 = Executor "x86_executor2"

let block_replay_executor = Executor "block_replay_testing_executor1"

let make_tags team executors tags =
  let misc_tags =
    match team with
    | None -> "long" :: tags
    | Some team -> "long" :: team :: tags
  in
  let executor_tags = List.map (fun (Executor x) -> x) executors in
  executor_tags @ misc_tags

(* Warning: [argument] must not be applied at registration. *)
let wrap_body title filename team timeout body argument =
  let test =
    {title; filename; team; data_points = String_map.empty; alert_count = 0}
  in
  current_test := Some test ;
  Option.iter Alerts.load !config.alerts ;
  Lwt.finalize
    (fun () ->
      Lwt.catch
        (fun () ->
          Lwt.finalize (with_timeout timeout (body argument)) send_data_points)
        (fun exn ->
          alert_s
            ~category:("exception-" ^ title)
            ~log:false
            (Printexc.to_string exn) ;
          raise exn))
    (fun () ->
      current_test := None ;
      unit)

let register ~__FILE__ ~title ~tags ?uses ?uses_node ?uses_client
    ?uses_admin_client ?team ~executors ~timeout body =
  if String.contains title '\n' then
    invalid_arg
      "Long_test.register: long test titles cannot contain newline characters" ;
  let tags = make_tags team executors tags in
  Test.register
    ~__FILE__
    ~title
    ~tags
    ?uses
    ?uses_node
    ?uses_client
    ?uses_admin_client
    (wrap_body title __FILE__ team timeout body)

let update_grafana_dashboard (dashboard : Grafana.dashboard) =
  Lwt_main.run
  @@
  match !config with
  | {influxdb = Some influxdb_config; grafana = Some grafana_config; _} ->
      let dashboard =
        (* Prefix measurements in queries with the InfluxDB measurement prefix. *)
        let update_panel = function
          | Grafana.Row _ as x -> x
          | Graph graph ->
              Graph
                {
                  graph with
                  queries =
                    List.map
                      (fun (query, alias) ->
                        ( InfluxDB.prefix_measurement influxdb_config query,
                          alias ))
                      graph.queries;
                }
        in
        {dashboard with panels = List.map update_panel dashboard.panels}
      in
      Grafana.update_dashboard grafana_config dashboard
  | _ -> unit
OCaml

Innovation. Community. Security.