]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/WriteReadBigLiteral.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / read / WriteReadBigLiteral.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.performance.read;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.VirtualGraph;
17 import org.simantics.db.WriteOnlyGraph;
18 import org.simantics.db.common.TransactionPolicyKeep;
19 import org.simantics.db.common.WriteBindings;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.request.WriteOnlyResultRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.request.Read;
24 import org.simantics.db.request.WriteOnlyResult;
25 import org.simantics.db.service.TransactionPolicySupport;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28
29 public class WriteReadBigLiteral extends ExistingDatabaseTest {
30
31         private static final int _100MB = 100000000;
32         private static final int _250MB = 250000000;
33         private static final int _500MB = 500000000;
34         private static final int _1000MB = 1000000000;
35         private static final int _2000MB = 2000000000;
36         
37         private Resource time(String label, WriteOnlyResult<Resource> request) throws DatabaseException {
38
39                 long start = System.nanoTime();
40                 Resource result = getSession().syncRequest(request);
41                 long duration = System.nanoTime() - start;
42                 System.out.println(label + " = " + 1e-9*duration);
43                 return result;
44                 
45         }
46
47         private void time(String label, Read<Object> request) throws DatabaseException {
48
49                 long start = System.nanoTime();
50                 getSession().syncRequest(request);
51                 long duration = System.nanoTime() - start;
52                 System.out.println(label + " = " + 1e-9*duration);
53                 
54         }
55         
56         public static WriteOnlyResult<Resource> writeOnly(final int size, VirtualGraph graph) throws DatabaseException {
57
58                 final byte[] literal = new byte[size];
59                 
60                 return new WriteOnlyResultRequest<Resource>(graph) {
61
62                         @Override
63                         public Resource perform(WriteOnlyGraph graph) throws DatabaseException {
64                                 Resource res = graph.newResource();
65                                 graph.claimValue(res, literal, WriteBindings.BYTE_ARRAY);
66                                 return res;
67                         }
68                         
69                 };
70                 
71         }
72         
73         public static Read<Object> read(final Resource resource) {
74
75                 return new ReadRequest() {
76                         
77                         @Override
78                         public void run(ReadGraph graph) throws DatabaseException {
79                                 
80                                 byte[] value = graph.getValue(resource, WriteBindings.BYTE_ARRAY);
81                                 System.err.println("value: " + value.length);
82
83                         }
84                         
85                 };
86                 
87         }
88         
89         public void test() throws Exception {
90                 
91                 Layer0.getInstance(getSession());
92
93                 getSession().registerService(TransactionPolicySupport.class, new TransactionPolicyKeep());
94                 
95                 Resource root = time("write 100MB", writeOnly(_100MB, null));
96                 time("response 100MB", read(root));
97                 Resource root2 = time("write 250MB", writeOnly(_250MB, null));
98                 time("response 250MB", read(root2));
99                 Resource root3 = time("write 500MB", writeOnly(_500MB, null));
100                 time("response 500MB", read(root3));
101                 Resource root4 = time("write 1000MB", writeOnly(_1000MB, null));
102                 time("response 1000MB", read(root4));
103                 Resource root5 = time("write 2000MB", writeOnly(_2000MB, null));
104                 time("response 2000MB", read(root5));
105                 
106
107 ////            System.out.println("wait");
108 ////            Thread.sleep(3000);
109 ////            System.out.println("wait done");
110 //              
111 //              // Read everything after write
112 //              time("response", HierarchicalNames.readAsync(root));
113 //              
114 ////            System.out.println("wait");
115 ////            Thread.sleep(3000);
116 ////            System.out.println("wait done");
117 //              
118 //              // Clusters are now loaded
119 //              time("async", HierarchicalNames.readAsync(root));
120 ////            time("async2", HierarchicalNames.readAsync2(root));
121 ////            time("sync", HierarchicalNames.readSync(root));
122 ////            listenTime("listenSync", HierarchicalNames.readSync(root));
123
124         }
125
126 }