Skip to content

Commit a99b768

Browse files
filipeomredianthus
authored andcommitted
Testcomp script zulip webhook integration
1 parent 6efe62e commit a99b768

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

bench/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ $ dune exec -- testcomp/testcomp.exe 5 # timeout of 5 seconds
2626

2727
A folder `testcomp-results-YYYY-MM-DD_HHhMMhSSs` has been created with a lot of output. It contains `results-report/index.html` which is the recommended way to visualize the results.
2828

29+
### Zulip notification:
30+
31+
You can set up the script to notify you or a stream on Zulip using the Zulip Slack incoming webhook integration.
32+
For information on creating webhooks, see [this](https://zulip.com/integrations/doc/slack_incoming#zulip-slack-incoming-webhook-integration).
33+
Next, just set the `ZULIP_WEBHOOK` environment variable with the generated webhook and launch the script:
34+
35+
```shell-session
36+
export ZULIP_WEBHOOK="https://saussice.zulipchat.com/api/v1/external/slack_incoming?api_key=...&stream=germany&topic=bratwurst"
37+
dune exec ./testcomp/testcomp.exe
38+
```
39+
2940
## Generate the report by hand
3041

3142
```shell-session

bench/testcomp/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(executable
44
(name testcomp)
55
(modules testcomp whitelist)
6-
(libraries bos fpath report tool yaml unix smtml))
6+
(libraries bos fpath report tool yaml unix smtml cohttp cohttp-lwt-unix lwt))
77

88
(rule
99
(deps whitelist.txt)

bench/testcomp/testcomp.ml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,73 @@ let runs =
146146
files;
147147
!results
148148

149+
let notify_finished runs =
150+
let open Cohttp in
151+
let open Cohttp_lwt_unix in
152+
let headers =
153+
let headers = Header.init () in
154+
Header.add_list headers
155+
[ ("Content-type", "application/json"); ("User-Agent", "Owibot/1.0") ]
156+
in
157+
let send url body =
158+
let body = Cohttp_lwt.Body.of_string (Yojson.to_string body) in
159+
Client.post ~body ~headers url
160+
in
161+
let head () =
162+
let open Bos in
163+
let cmd = Cmd.(v "git" % "rev-parse" % "--short" % "HEAD") in
164+
let output = OS.Cmd.run_out ~err:OS.Cmd.err_run_out cmd in
165+
match OS.Cmd.out_string ~trim:true output with
166+
| Ok (stdout, (_, `Exited 0)) -> stdout
167+
| Error (`Msg err) ->
168+
Format.eprintf "ERROR: %s@." err;
169+
"unknown"
170+
| Ok (stdout, (_, (`Exited _ | `Signaled _))) ->
171+
Format.eprintf "%s@\nWARN: Unable to fetch git HEAD@." stdout;
172+
"unknown"
173+
in
174+
let text =
175+
Format.asprintf "Using tool=*%s* and timeout=*%F*@\n@\n%a@."
176+
(Tool.to_reference_name tool)
177+
timeout Report.Runs.pp_quick_results runs
178+
in
179+
(* Notify on `ZULIP_WEBHOOK` *)
180+
match Bos.OS.Env.var "ZULIP_WEBHOOK" with
181+
| None -> Format.eprintf "%s" text
182+
| Some url ->
183+
let url = Uri.of_string url in
184+
let title =
185+
Format.sprintf "Benchmark results (commit hash=%s) :octopus:" (head ())
186+
in
187+
let body =
188+
(* Using Yojson just to ensure we're sending correct json *)
189+
`Assoc
190+
[ ( "blocks"
191+
, `List
192+
[ `Assoc
193+
[ ("type", `String "header")
194+
; ( "text"
195+
, `Assoc
196+
[ ("type", `String "plain_text")
197+
; ("text", `String title)
198+
; ("emoji", `Bool true)
199+
] )
200+
]
201+
; `Assoc
202+
[ ("type", `String "section")
203+
; ( "text"
204+
, `Assoc
205+
[ ("type", `String "mrkdwn"); ("text", `String text) ]
206+
)
207+
]
208+
] )
209+
]
210+
in
211+
let result, _ = Lwt_main.run @@ send url body in
212+
let status = Response.status result in
213+
Format.eprintf "Server responded: %s@." (Code.string_of_status status)
214+
149215
let () =
150216
let runs = ok_or_fail runs in
217+
notify_finished runs;
151218
Report.Gen.full_report runs output_dir reference_name |> ok_or_fail

0 commit comments

Comments
 (0)