Handle some error conditions gracefully
authorMikko Rasa <mikko.rasa@movial.fi>
Wed, 11 Mar 2009 15:28:37 +0000 (17:28 +0200)
committerMikko Rasa <mikko.rasa@movial.fi>
Wed, 11 Mar 2009 15:28:37 +0000 (17:28 +0200)
Output total score with at least 4 significant digits

main.c

diff --git a/main.c b/main.c
index 69eff72..67d33f2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -54,12 +54,38 @@ int main(int argc, char **argv)
        unsigned batch=3000;
        unsigned time=1000;
        MeasureType type=NORMAL;
-       FILE *in, *out;
+       FILE *in=NULL, *out;
        float total=0;
        float weight=1.0;
        XRenderColor color;
+       int prec;
+
+       if(argc>=2)
+               in=fopen(argv[1], "r");
+       else
+       {
+               fprintf(stderr, "Benchmark script not specified\n");
+               fprintf(stderr, "Usage: %s <script>\n", argv[0]);
+               return 1;
+       }
+
+       if(!in)
+       {
+               fprintf(stderr, "Benchmark script could not be opened, aborting\n");
+               return 1;
+       }
+
+       out=fopen("result.csv", "w");
+       if(!out)
+               fprintf(stderr, "Output file could not be opened, proceeding with stdout only\n");
 
        dpy=XOpenDisplay(NULL);
+       if(!dpy)
+       {
+               fprintf(stderr, "Could not open display, aborting\n");
+               return 1;
+       }
+
        screen=DefaultScreen(dpy);
        depth=DefaultDepth(dpy, screen);
        visual=DefaultVisual(dpy, screen);
@@ -92,12 +118,6 @@ int main(int argc, char **argv)
        color.blue=0xFFFF;
        XRenderFillRectangle(dpy, PictOpSrc, srcpict, &color, 0, 0, 1, 1);
 
-       if(argc>=2)
-               in=fopen(argv[1], "r");
-       else
-               in=fopen("bench.txt", "r");
-       out=fopen("result.csv", "w");
-
        op=PictOpSrc;
        while(1)
        {
@@ -210,15 +230,20 @@ int main(int argc, char **argv)
                                if(res.ops_per_sec>0)
                                {
                                        float score=(res.ops_per_sec-res.error_margin)/weight;
-                                       fprintf(out, "%d,%d,%.0f,%lld,%s\n", res.ops_per_sec, res.error_margin, score, (long long)res.ops_per_sec*size, test_name);
+                                       if(out)
+                                               fprintf(out, "%d,%d,%.0f,%lld,%s\n", res.ops_per_sec, res.error_margin, score, (long long)res.ops_per_sec*size, test_name);
                                        total+=1/score;
                                }
                        }
                }
        }
        total=1/total;
-       printf("Total score: %.0f\n", total);
-       fprintf(out, "%.0f\n", total);
+       prec=(-log(total)/log(10))+4;
+       if(prec<0)
+               prec=0;
+       printf("Total score: %.*f\n", prec, total);
+       if(out)
+               fprintf(out, "%.*f\n", prec, total);
        fclose(out);
        fclose(in);