001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded.benchmark;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.content.FxContent;
038: import com.flexive.shared.content.FxPK;
039: import com.flexive.shared.exceptions.FxApplicationException;
040: import com.flexive.shared.interfaces.ContentEngine;
041: import com.flexive.shared.scripting.FxScriptInfo;
042: import com.flexive.shared.scripting.FxScriptEvent;
043: import com.flexive.shared.structure.FxType;
044: import com.flexive.shared.value.FxString;
045: import static org.testng.Assert.assertEquals;
046: import org.testng.annotations.Test;
047:
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: /**
052: * Benchmarks content instances.
053: *
054: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: * @version $Rev: 181 $
056: */
057: @Test(groups="benchmark")
058: public class ContentBenchmark {
059: public static int scriptCtr1 = 0;
060: public static int scriptCtr2 = 0;
061: private static final String SCRIPT1 = "ContentBenchmark.test1.gy";
062: private static final String SCRIPT2 = "ContentBenchmark.test2.gy";
063:
064: public void createContactDataBenchmark()
065: throws FxApplicationException {
066: // register some scripts
067: final FxScriptInfo script1 = EJBLookup
068: .getScriptingEngine()
069: .createScript(FxScriptEvent.AfterContentCreate,
070: SCRIPT1, "",
071: "com.flexive.tests.embedded.benchmark.ContentBenchmark.scriptCtr1++");
072: final FxScriptInfo script2 = EJBLookup
073: .getScriptingEngine()
074: .createScript(FxScriptEvent.AfterContentCreate,
075: SCRIPT2, "",
076: "com.flexive.tests.embedded.benchmark.ContentBenchmark.scriptCtr2++");
077: final long contactDataId = CacheAdmin.getEnvironment().getType(
078: FxType.CONTACTDATA).getId();
079: EJBLookup.getScriptingEngine().createTypeScriptMapping(
080: script1.getId(), contactDataId, true, true);
081: EJBLookup.getScriptingEngine().createTypeScriptMapping(
082: script2.getId(), contactDataId, true, true);
083:
084: final List<FxPK> result = new ArrayList<FxPK>();
085: final ContentEngine ci = EJBLookup.getContentEngine();
086: final long startTime = System.currentTimeMillis();
087: final int runs = 100;
088: try {
089: for (int i = 0; i < runs; i++) {
090: final FxContent content = createContactData(ci);
091: result.add(ci.save(content));
092: }
093: assertEquals(scriptCtr1, runs);
094: assertEquals(scriptCtr2, runs);
095: } finally {
096: FxBenchmarkUtils.logExecutionTime("createContactData",
097: startTime, 100);
098: final long deleteStart = System.currentTimeMillis();
099: for (FxPK pk : result) {
100: try {
101: ci.remove(pk);
102: } catch (Exception e) {
103: System.err.println("Failed to remove content " + pk
104: + ": " + e.getMessage());
105: }
106: }
107: FxBenchmarkUtils.logExecutionTime("deleteContactData",
108: deleteStart, 100);
109: EJBLookup.getScriptingEngine()
110: .removeScript(script1.getId());
111: EJBLookup.getScriptingEngine()
112: .removeScript(script2.getId());
113: }
114: }
115:
116: private FxContent createContactData(ContentEngine ci)
117: throws FxApplicationException {
118: final FxContent content = ci.initialize(FxType.CONTACTDATA);
119: content.setValue("/name", new FxString(false, "Peter"));
120: content.setValue("/surname", new FxString(false, "Bones"));
121: content.setValue("/email",
122: new FxString(false, "root@localhost"));
123: content.setValue("/address/street", new FxString(false,
124: "Loewengasse 2/8"));
125: content.setValue("/address/zipCode",
126: new FxString(false, "1030"));
127: content
128: .setValue("/address/city",
129: new FxString(false, "Vienna"));
130: return content;
131: }
132:
133: public void getContentData() throws FxApplicationException {
134: final FxContent content = createContactData(EJBLookup
135: .getContentEngine());
136: final long startTime = System.currentTimeMillis();
137: for (int i = 0; i < 100000; i++) {
138: content.getData("/name");
139: content.getData("/email");
140: content.getData("/address/zipCode");
141: content.getData("/address/street");
142: }
143: FxBenchmarkUtils.logExecutionTime("getContentData", startTime,
144: 100000 * 4);
145: }
146: }
|