Buggy version - can't find the segfault right now.
[ocaml-ancient.git] / test_ancient_weblogs.ml
1 (* Load in large weblogs and see if they can still be used.
2  * $Id: test_ancient_weblogs.ml,v 1.2 2006-09-27 18:39:44 rich Exp $
3  *)
4
5 open Printf
6
7 open ExtList
8
9 let gc_stats = true (* If true, print GC stats before processing each day. *)
10
11 let (//) = Filename.concat
12
13 let rec range a b =
14   if a > b then []
15   else a :: range (succ a) b
16
17 (* Cartesian join of two lists. *)
18 let cartesian xs ys =
19   List.flatten (
20     List.map (
21       fun x ->
22         List.map (
23           fun y -> x, y
24         ) ys
25     ) xs
26   )
27
28 let file_readable filename =
29   try Unix.access filename [Unix.R_OK]; true
30   with Unix.Unix_error _ -> false
31
32 (* Suppress warning messages. *)
33 let () = Weblogs.quiet := true
34
35 let gc_compact () =
36   eprintf "compacting ... %!";
37   Gc.compact ();
38   if gc_stats then (
39     let stat = Gc.stat () in
40     let live_words = stat.Gc.live_words in
41     eprintf "live words = %d (%d MB)\n%!"
42       live_words (live_words * 8 / 1024 / 1024)
43   )
44
45 (* Find the list of files.  Some which should exist don't, so
46  * warnings about those so we can chase up.
47  *)
48 let files =
49   let dir = "/home/rich/oversized-logfiles/perrys" in
50   let drivers =
51     [ "burns"; "gronholm"; "rohrl"; "sainz"; "solberg"; "vatanen" ] in
52   let dates = range 1 31 in
53   let dates = List.map (fun day -> sprintf "200608%02d" day) dates in
54   let files = cartesian drivers dates in
55   let files =
56     List.map (fun (driver, date) ->
57                 sprintf "%s-perrys-access.log.%s.gz" driver date) files in
58   let files =
59     List.filter_map (
60       fun filename ->
61         let path = dir // filename in
62         if not (file_readable path) then (
63           prerr_endline ("warning: " ^ filename ^ " not found - ignored");
64           None
65         ) else (
66           Some path
67         )
68     ) files in
69
70   eprintf "number of files = %d\n%!" (List.length files);
71
72   files
73
74 let md =
75   let fd =
76     Unix.openfile "test_ancient_weblogs.data"
77       [Unix.O_RDWR; Unix.O_CREAT; Unix.O_TRUNC] 0o644 in
78   Ancient.attach fd
79
80 (* Load each file into memory and make it ancient. *)
81 let () =
82   List.iteri (
83     fun key filename ->
84       let () =
85         let basename = Filename.basename filename in
86         eprintf "Importing logfile %s\n%!" basename;
87         let rows = Weblogs.import_file filename in
88         ignore (Ancient.share md key rows) in
89       gc_compact ()
90   ) files;
91
92
93
94
95   Ancient.detach md