[libgee] Add commandline options to the benchmark



commit 4098723e7be2af32e97d87a1734ca22d0caeb3ec
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sun Aug 17 01:10:29 2014 +0200

    Add commandline options to the benchmark

 benchmark/collections.vala |  114 +++++++++++++++++++++++++++++++++++---------
 1 files changed, 91 insertions(+), 23 deletions(-)
---
diff --git a/benchmark/collections.vala b/benchmark/collections.vala
index 91c3b6b..401bf7f 100644
--- a/benchmark/collections.vala
+++ b/benchmark/collections.vala
@@ -1,3 +1,4 @@
+public delegate void RunTest(FileStream output, bool verbose, size_t size, size_t tries);
 public delegate Gee.Collection<T> CreateCollection<T>();
 public delegate void Prepare<T>(Gee.Collection<T> collection);
 public delegate void Run<T>(Gee.Collection<T> collection);
@@ -11,7 +12,14 @@ struct CollectionTest<T> {
        CreateCollection<T> create;
 }
 
-internal void test<T>(string name, CollectionTest[] cols, Prepare<T> prepare, Run<T> run, size_t size, 
size_t tries) {
+class RunTestWrap {
+       public RunTestWrap(owned RunTest run_test) {
+               this.run_test = (owned)run_test;
+       }
+       public RunTest run_test;
+}
+
+internal void test<T>(FileStream output, bool verbose, string name, CollectionTest[] cols, Prepare<T> 
prepare, Run<T> run, size_t size, size_t tries) {
        GLib.Timer timer = new GLib.Timer ();
        for (size_t i = 0; i < tries; i++) {
                for (size_t j = 0; j < cols.length; j++) {
@@ -21,37 +29,37 @@ internal void test<T>(string name, CollectionTest[] cols, Prepare<T> prepare, Ru
                        timer.start();
                        run(col);
                        timer.stop();
-                       stdout.printf("%s, %s, %ld, %lf\n", name, cols[j].name, (long)size, timer.elapsed());
+                       output.printf("%s, %s, %ld, %lf\n", name, cols[j].name, (long)size, timer.elapsed());
                }
        }
 }
 
-internal void run_uint_tests(size_t size, size_t tries) {
+internal void run_list_uint_tests(FileStream output, bool verbose, size_t size, size_t tries) {
        CollectionTest<uint>[] cols = new CollectionTest<uint>[3];
        cols[0] = CollectionTest<uint>("ArrayList", () => {return new Gee.ArrayList<uint>();});
        cols[1] = CollectionTest<uint>("LinkedList", () => {return new Gee.LinkedList<uint>();});
        //cols[2] = CollectionTest<uint>("UnrolledLinkedList [5/0.8]", () => {return new 
Gee.UnrolledLinkedList<uint>();});
        //cols[2] = CollectionTest<uint>("UnrolledLinkedList [13/0.8]", () => {return new 
Gee.UnrolledLinkedList<uint>();});
        cols[2] = CollectionTest<uint>("UnrolledLinkedList [29/0.8]", () => {return new 
Gee.UnrolledLinkedList<uint>();});
-       test<uint>("Sequential uint add", cols, (col) => {}, (col) => {
+       test<uint>(output, verbose, "Sequential uint add", cols, (col) => {}, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
        }, size, tries);
-       test<uint>("Prepending uint", cols, (col) => {}, (col) => {
+       test<uint>(output, verbose, "Prepending uint", cols, (col) => {}, (col) => {
                var list = (Gee.List<uint>)col;
                for (size_t i = 0; i < size; i++) {
                        list.insert(0, (uint)i);
                }
        }, size, tries);
-       test<uint>("Random insert", cols, (col) => {}, (col) => {
+       test<uint>(output, verbose, "Random insert", cols, (col) => {}, (col) => {
                uint r = 0xdeadbeefU;
                var list = (Gee.List<uint>)col;
                for (size_t i = 0; i < size; i++) {
                        list.insert((int)(r % (i + 1)), (uint)i);
                }
        }, size, tries);
-       test<uint>("Doubling uint (add)", cols, (col) => {
+       test<uint>(output, verbose, "Doubling uint (add)", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -62,7 +70,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        iter.add (iter.get ());
                }
        }, size, tries);
-       test<uint>("Doubling uint (insert)", cols, (col) => {
+       test<uint>(output, verbose, "Doubling uint (insert)", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -73,7 +81,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        iter.insert (iter.get ());
                }
        }, size, tries);
-       test<uint>("Sequential uint remove", cols, (col) => {
+       test<uint>(output, verbose, "Sequential uint remove", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -83,7 +91,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        list.remove_at((int)i);
                }
        }, size, tries);
-       test<uint>("Sequential uint remove (reversed)", cols, (col) => {
+       test<uint>(output, verbose, "Sequential uint remove (reversed)", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -93,7 +101,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        list.remove_at((int)0);
                }
        }, size, tries);
-       test<uint>("Random uint remove", cols, (col) => {
+       test<uint>(output, verbose, "Random uint remove", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -104,7 +112,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        list.remove_at((int)(r % (i + 1)));
                }
        }, size, tries);
-       test<uint>("Halfing uint", cols, (col) => {
+       test<uint>(output, verbose, "Halfing uint", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -115,7 +123,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        iter.next ();
                }
        }, size, tries);
-       test<uint>("Foreach uint", cols, (col) => {
+       test<uint>(output, verbose, "Foreach uint", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -124,7 +132,7 @@ internal void run_uint_tests(size_t size, size_t tries) {
                        col.foreach ((i) => {return true;});
                }
        }, size, tries);
-       test<uint>("Iterator foreach uint", cols, (col) => {
+       test<uint>(output, verbose, "Iterator foreach uint", cols, (col) => {
                for (size_t i = 0; i < size; i++) {
                        col.add((uint)i);
                }
@@ -135,14 +143,74 @@ internal void run_uint_tests(size_t size, size_t tries) {
        }, size, tries);
 }
 
-int main() {
-       uint tries = 100;
-       run_uint_tests(32, tries);
-       run_uint_tests(64, tries);
-       run_uint_tests(128, tries);
-       run_uint_tests(256, tries);
-       run_uint_tests(512, tries);
-       run_uint_tests(1024, tries);
-       run_uint_tests(2048, tries);
+internal uint tries = 100;
+internal uint[]? sizes;
+[CCode (array_length = false, array_null_terminated = true)]
+internal string[]? sizes_str = null;
+[CCode (array_length = false, array_null_terminated = true)]
+internal string[]? tests = null;
+internal string? output = null;
+internal bool verbose = false;
+internal const GLib.OptionEntry[] options = {
+       {"size", 's', 0, OptionArg.STRING_ARRAY, ref sizes_str, "Size for which to test", "SIZES..." },
+       {"tries", 'n', 0, OptionArg.INT, ref tries, "Number of measurements", "TRIES"},
+       {"test", 't', 0, OptionArg.STRING_ARRAY, ref tests, "Test sets", "TESTSETS"},
+       {"output", 'o', 0, OptionArg.FILENAME, ref output, "Output data to file (appends at the end)", 
"FILE"},
+       {"verbose", 'v', 0, OptionArg.NONE, ref verbose, "Verbose"},
+       {null}
+};
+
+int main(string[] args) {
+       var all_tests = new Gee.HashMap<unowned string, RunTestWrap>();
+       all_tests["lists"] = new RunTestWrap(run_list_uint_tests);
+       sizes = new uint[]{32, 64, 128, 256, 512, 1024, 2048};
+       {
+               var tmp = all_tests.keys.to_array ();
+               tmp += null;
+               tests = (owned)tmp;
+       }
+       var optctx = new OptionContext ("- Collections benchmark");
+       optctx.set_help_enabled (true);
+       optctx.add_main_entries (options, null);
+       try {
+               optctx.parse (ref args);
+               if (sizes_str != null) {
+                       sizes = new uint[0];
+                       foreach (unowned string size_str in sizes_str) {
+                               uint64 val;
+                               if (!uint64.try_parse(size_str, out val)) {
+                                       throw new OptionError.BAD_VALUE ("Cannot parse integer value '%s' for 
-s", size_str);
+                               }
+                               if (val >= int.MAX) {
+                                       throw new OptionError.BAD_VALUE ("Value '%s' for -s is out of range", 
size_str);
+                               }
+                               sizes += (uint)val;
+                       }
+               }
+               foreach (unowned string test in tests) {
+                       if (!all_tests.has_key (test)) {
+                               throw new OptionError.BAD_VALUE ("Unknown test set '%s'", test);
+                       }
+               }
+       } catch (OptionError e) {
+               stderr.printf ("error: %s\n", e.message);
+               stderr.printf ("Run '%s --help' to see a full list of available command line options.\n", 
args[0]);
+               return 2;
+       }
+       unowned FileStream output_stream = stdout;
+       FileStream? file = null;
+       if (output != null) {
+               file = FileStream.open(output, "a");
+               if (file == null) {
+                       stderr.printf ("Failed to open output file: %s", strerror(errno));
+               }
+               output_stream = file;
+       }
+       foreach (unowned string test in tests) {
+               RunTestWrap run_test = all_tests[test];
+               foreach (uint size in sizes) {
+                       run_test.run_test (output_stream, verbose, size, tries);
+               }
+       }
        return 0;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]