You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2011/10/17 13:39:36 UTC

svn commit: r1185113 - /incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java

Author: andy
Date: Mon Oct 17 11:39:35 2011
New Revision: 1185113

URL: http://svn.apache.org/viewvc?rev=1185113&view=rev
Log: (empty)

Added:
    incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java   (with props)

Added: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java?rev=1185113&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java (added)
+++ incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java Mon Oct 17 11:39:35 2011
@@ -0,0 +1,328 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package reports;
+
+import com.hp.hpl.jena.ontology.OntModelSpec ;
+import com.hp.hpl.jena.query.Query ;
+import com.hp.hpl.jena.query.QueryExecution ;
+import com.hp.hpl.jena.query.QueryExecutionFactory ;
+import com.hp.hpl.jena.query.QueryFactory ;
+import com.hp.hpl.jena.query.QuerySolution ;
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.Syntax ;
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.rdf.model.Property ;
+import com.hp.hpl.jena.rdf.model.Resource ;
+
+
+public class ReportPerformanceIN
+{
+    /*
+     * Created on 12 Oct 2011 by David Allsopp
+     */
+    // jena-users @ incubator.apche.org
+
+    /**
+     * How much data in the model - we create 4 triples for each resource
+     * */
+    static final int RESOURCES = 25000;
+
+    /**
+     * We are querying a subset of MAX-MIN resources
+     * */
+    static final int MIN = 0;
+    static final int MAX = 500;
+
+    /**
+     * Repeat each test a few times if necessary to ensure significant durations
+     * */
+    static final int REPS = 1;
+
+    /**
+     * For the strategies that use batching, how many resources to handle in a
+     * single query.
+     * */
+    static final int BATCHSIZE = 50;
+
+
+    public static void main(String[] args)
+    {
+        Model model = loadModel();
+
+        System.out.println("Doing " + REPS + " runs, selecting " + (MAX - MIN) + " resources...\n");
+
+        benchmark(new BindingsStrategy(BATCHSIZE), model);
+        benchmark(new BruteForceStrategy(), model);
+        benchmark(new NaiveStrategy(), model);
+        benchmark(new UnionStrategy(), model);
+        benchmark(new FilterStrategy(BATCHSIZE), model);
+
+        System.out.println("\nFinished.");
+    }
+
+
+    private static void benchmark(Strategy strategy, Model model)
+    {
+        long start = System.currentTimeMillis();
+        for (int j = 0; j < REPS; j++)
+        {
+            strategy.run(model);
+        }
+        long end = System.currentTimeMillis();
+        long dur = end - start;
+        System.out.println(strategy + ": " + dur);
+
+    }
+
+
+    static Model loadModel()
+    {
+        //Model model = ModelFactory.createDefaultModel() ;
+        Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RDFS_INF);
+        System.out.print("Loading data...");
+
+        Property lat = model.createProperty("http://www.example.com/namespace#latitude");
+        Property lon = model.createProperty("http://www.example.com/namespace#longitude");
+        Property comment = model.createProperty("http://www.example.com/namespace#comment");
+
+        for (int i = 0; i < RESOURCES; i++)
+        {
+            Resource r = model.createResource("http://www.example.com/namespace#myresource" + i);
+            model.add(r, lat, Double.toString(90 * Math.random()));
+            model.add(r, lon, Double.toString(90 * Math.random()));
+            model.add(r, comment, "Random irrelevant property");
+            model.add(r, comment, "Another random irrelevant property");
+        }
+
+        System.out.println("OK");
+        System.out.println("Resources = " + RESOURCES);
+        System.out.println("Triples = " + model.size());
+        return model;
+    }
+
+
+    static int query(String sparqlQuery, Model model)
+    {
+        Query query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ);
+        QueryExecution qexec = QueryExecutionFactory.create(query,
+                                                            model);
+        ResultSet jenaResults = qexec.execSelect();
+
+        int count = 0;
+        while (jenaResults.hasNext())
+        {
+            QuerySolution qs = jenaResults.nextSolution();
+            count++;
+        }
+
+        qexec.close();
+        return count;
+    }
+
+    static abstract class Strategy
+    {
+        String name;
+
+
+        public abstract void run(Model model);
+    }
+
+    static class NaiveStrategy extends Strategy
+    {
+        @Override
+        public String toString()
+        {
+            return "Naive";
+        }
+
+
+        @Override
+        public void run(Model model)
+        {
+            int resultcount = 0;
+            for (int i = MIN; i < MAX; i++)
+            {
+                String resource = "http://www.example.com/namespace#myresource" + i;
+                String q = "SELECT ?lat ?lon {<" + resource + "> <http://www.example.com/namespace#latitude> ?lat; <http://www.example.com/namespace#longitude> ?lon}";
+                resultcount += query(q, model);
+
+            }
+            System.out.print("[" + resultcount + "] ");
+        }
+
+    }
+
+    static class FilterStrategy extends Strategy
+    {
+        int batchsize;
+
+
+        public FilterStrategy(int batchsize)
+        {
+            this.batchsize = batchsize;
+        }
+
+
+        @Override
+        public String toString()
+        {
+            return "Filter (batches of " + batchsize + ")";
+        }
+
+
+        @Override
+        public void run(Model model)
+        {
+            int batchstart = MIN;
+            int batchend = MIN + batchsize;
+
+            int resultcount = 0;
+            while (batchstart < MAX)
+            {
+                StringBuilder bq = new StringBuilder("prefix ns: <http://www.example.com/namespace#> select ?x ?lat ?lon {?x ns:latitude ?lat; ns:longitude ?lon. filter(?x in(");
+
+                for (int i = batchstart; i < batchend; i++)
+                {
+                    String resource = "ns:myresource" + i;
+                    bq.append(resource);
+                    bq.append(",");
+                }
+                bq.deleteCharAt(bq.length() - 1);
+                bq.append("))}");
+                String bquery = bq.toString();
+                // System.out.println(bquery);
+                resultcount += query(bquery, model);
+                batchstart = batchend;
+                batchend = batchstart + batchsize;
+                if (batchend > MAX)
+                {
+                    batchend = MAX;
+                }
+            }
+
+            System.out.print("[" + resultcount + "] ");
+        }
+    }
+
+    static class UnionStrategy extends Strategy
+    {
+        @Override
+        public String toString()
+        {
+            return "Union (no batching)";
+        }
+
+
+        @Override
+        public void run(Model model)
+        {
+            StringBuilder bq = new StringBuilder("prefix ns: <http://www.example.com/namespace#> select ?x ?lat ?lon {");
+            for (int i = MIN; i < MAX; i++)
+            {
+                String resource = "ns:myresource" + i;
+                bq.append("{?x ns:latitude ?lat; ns:longitude ?lon. filter(?x=" + resource + ")");
+                bq.append("}");
+                if (i < MAX - 1)
+                {
+                    bq.append(" UNION \n");
+                }
+            }
+            bq.append(" }");
+            String query = bq.toString();
+            // System.out.println(query);
+            int resultcount = query(query, model);
+            System.out.print("[" + resultcount + "] ");
+        }
+
+    }
+
+    static class BindingsStrategy extends Strategy
+    {
+        int batchsize;
+
+
+        public BindingsStrategy(int batchsize)
+        {
+            this.batchsize = batchsize;
+        }
+
+
+        @Override
+        public String toString()
+        {
+            return "Bindings (batches of " + batchsize + ")";
+        }
+
+
+        @Override
+        public void run(Model model)
+        {
+            int batchstart = MIN;
+            int batchend = MIN + batchsize;
+            int resultcount = 0;
+            while (batchstart < MAX)
+            {
+                StringBuilder bq = new StringBuilder("prefix ns: <http://www.example.com/namespace#> select ?x ?lat ?lon {?x ns:latitude ?lat; ns:longitude ?lon} BINDINGS ?x {");
+
+                for (int i = batchstart; i < batchend; i++)
+                {
+                    String resource = "ns:myresource" + i;
+                    bq.append("(");
+                    bq.append(resource);
+                    bq.append(")");
+                }
+                bq.append("}");
+                String bquery = bq.toString();
+                // System.out.println(bquery);
+                int subcount = query(bquery, model);
+                // System.out.println(subcount);
+                resultcount += subcount;
+                batchstart = batchend;
+                batchend = batchstart + batchsize;
+                if (batchend > MAX)
+                {
+                    batchend = MAX;
+                }
+            }
+            System.out.print("[" + resultcount + "] ");
+        }
+
+    }
+
+    static class BruteForceStrategy extends Strategy
+    {
+        @Override
+        public String toString()
+        {
+            return "Query all";
+        }
+
+
+        @Override
+        public void run(Model model)
+        {
+            String q = "SELECT ?x ?lat ?lon {?x <http://www.example.com/namespace#latitude> ?lat; <http://www.example.com/namespace#longitude> ?lon}";
+            int resultcount = query(q, model);
+            System.out.print("[" + resultcount + "] ");
+        }
+
+    }
+}
+

Propchange: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/reports/ReportPerformanceIN.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain