001: /*
002: * <copyright>
003: *
004: * Copyright 2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.core.persist;
027:
028: import java.io.Serializable;
029:
030: import org.cougaar.core.plugin.ComponentPlugin;
031: import org.cougaar.core.service.LoggingService;
032: import org.cougaar.util.UnaryPredicate;
033:
034: /**
035: * This component tests {@link
036: * org.cougaar.core.service.BlackboardService#persistNow()}
037: */
038: public class TestFullSnapshot extends ComponentPlugin {
039: private LoggingService logger;
040:
041: private static class Item implements Serializable {
042: byte[] bytes = new byte[10000];
043: }
044:
045: public void setupSubscriptions() {
046: logger = (LoggingService) getServiceBroker().getService(this ,
047: LoggingService.class, null);
048: }
049:
050: public void execute() {
051: if (logger.isShoutEnabled())
052: logger.shout("Running TestFullSnapShot");
053:
054: Item[] items = new Item[1000];
055: // Publish a bunch
056: for (int i = 0; i < items.length; i++) {
057: items[i] = new Item();
058: blackboard.publishAdd(items[i]);
059: }
060:
061: // Force a persist
062: try {
063: blackboard.persistNow();
064: } catch (PersistenceNotEnabledException pnee) {
065: logger.error("Persistence not enabled", pnee);
066: }
067:
068: printBBSize();
069:
070: // publishRemove a bunch
071: for (int i = 0; i < items.length; i++) {
072: blackboard.publishRemove(items[i]);
073: items[i] = null;
074: }
075: blackboard.closeTransaction();
076:
077: try {
078: Thread.sleep(5000);
079: } catch (InterruptedException ie) {
080: }
081:
082: blackboard.openTransaction();
083:
084: // Now force another persist
085: try {
086: blackboard.persistNow();
087: } catch (PersistenceNotEnabledException pnee) {
088: logger.error("Persistence not enabled", pnee);
089: }
090: printBBSize();
091: }
092:
093: private void printBBSize() {
094: Runtime rt = Runtime.getRuntime();
095: long heap = rt.totalMemory() - rt.freeMemory();
096: if (logger.isShoutEnabled())
097: logger.shout(blackboard.query(new UnaryPredicate() {
098: public boolean execute(Object o) {
099: return true;
100: }
101: }).size() + " objects on blackboard, heap = " + heap);
102: }
103: }
|