├── FirstEdition ├── ClassLoading │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ ├── ClassLoaderTest.java │ │ └── PausingThreadPoolExecutor.java ├── CountFilter │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── CountFilter.java ├── Exception │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── ExceptionTest.java ├── FalseSharing │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── ContendedTest.java ├── FindFilter │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── FindFilter.java ├── JNI │ ├── Makefile │ ├── README │ ├── build.xml │ └── src │ │ ├── c │ │ ├── calc.c │ │ └── main.c │ │ └── java │ │ └── net │ │ └── sdo │ │ └── RandomTestJava.java ├── JStack │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── ParseJStack.java ├── Lambda │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ ├── AnonymousIntTest.java │ │ ├── CalcInterface.java │ │ ├── ClassLoadingDelegate.java │ │ ├── IntegerInterface.java │ │ └── LambdaIntTest.java ├── ParallelTask │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── ParallelTaskComparison.java ├── ParsingTest │ ├── DOMTest.props │ ├── FilteringDOMTest.props │ ├── JAXBSerializeTest.props │ ├── JAXBTest.props │ ├── JacksonFilteringTest.props │ ├── JacksonTest.props │ ├── JsonBuilderTest.props │ ├── JsonFilteringTest.props │ ├── JsonTest.props │ ├── NonValidSaxFilteringTest.props │ ├── NonValidSaxTest.props │ ├── README │ ├── StaxFilteringTest.props │ ├── StaxTest.props │ ├── ValidCacheSaxTest.props │ ├── ValidCacheStaxTest.props │ ├── ValidSaxTest.props │ ├── WoodstoxFilteringTest.props │ ├── WoodstoxTest.props │ ├── build.xml │ ├── docs │ │ ├── json │ │ │ └── README │ │ └── xml │ │ │ └── README │ └── src │ │ └── net │ │ └── sdo │ │ ├── AbstractParsingTest.java │ │ ├── DOMBuilderTest.java │ │ ├── DOMSerializingTest.java │ │ ├── FilteringDOMBuilderTest.java │ │ ├── JAXBParsingTest.java │ │ ├── JAXBSerializingTest.java │ │ ├── JSONBuilderTest.java │ │ ├── JSONParserTest.java │ │ ├── JsonJacksonParserTest.java │ │ ├── ParsingTest.java │ │ ├── RunParams.java │ │ ├── SAXErrorHandler.java │ │ ├── SAXNonValidatingParserTest.java │ │ ├── SAXParserTest.java │ │ ├── SAXValidatingCachingParserTest.java │ │ ├── SAXValidatingParserTest.java │ │ ├── StAXParserTest.java │ │ ├── StAXValidatingCachingParserTest.java │ │ └── jaxb │ │ ├── FindPopularItemsResponse.java │ │ ├── ObjectFactory.java │ │ └── package-info.java ├── README ├── SampleFinalizer │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ ├── CleanupClass.java │ │ └── TestCleanup.java ├── Size │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── TestSizeOf.java ├── StockBatching │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockPriceHistoryBatcher.java ├── StockBatchingForkJoin │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockBatchingForkJoin.java ├── StockBatchingPool │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockPricePool.java ├── StockCreateJDBC │ ├── README │ ├── build.xml │ ├── src │ │ └── net │ │ │ └── sdo │ │ │ └── StockPriceCreateJDBC.java │ └── stock.sql ├── StockCreateJPA │ ├── META-INF │ │ └── persistence.xml │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockPriceCreateJPA.java ├── StockEar │ ├── README │ ├── StockEar-ejb │ │ ├── build.xml │ │ ├── persistence.xml │ │ └── src │ │ │ └── net │ │ │ └── sdo │ │ │ ├── StockSessionBeanImpl.java │ │ │ ├── StockSessionBeanLocal.java │ │ │ └── StockSessionBeanRemote.java │ ├── StockEar-war │ │ ├── build.xml │ │ ├── meta │ │ │ └── web.xml │ │ └── src │ │ │ ├── jsp │ │ │ ├── error.jsp │ │ │ ├── history.jsp │ │ │ ├── longhistory.jsp │ │ │ └── option.jsp │ │ │ └── net │ │ │ └── sdo │ │ │ └── StockServlet.java │ └── build.xml ├── StockReadJPA │ ├── META-INF │ │ └── persistence.xml │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockPriceReadJPA.java ├── StockRestDriver │ ├── README │ ├── build.xml │ ├── run.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── rest │ │ └── StockRestDriver.java ├── StockRestService │ ├── README │ ├── build.xml │ └── src │ │ └── java │ │ ├── net │ │ └── sdo │ │ │ └── rest │ │ │ └── StockPriceResource.java │ │ └── org │ │ └── netbeans │ │ └── rest │ │ └── application │ │ └── config │ │ └── ApplicationConfig.java ├── StockSerialization │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── StockHistorySerializeTest.java ├── StringIntern │ ├── README │ ├── build.xml │ └── src │ │ ├── extra166y │ │ └── CustomConcurrentHashMap.java │ │ └── net │ │ └── sdo │ │ └── StringInternTest.java ├── UncontendedSync │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── UncontendedTest.java ├── api │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── stock │ │ ├── StockOptionPrice.java │ │ ├── StockPrice.java │ │ ├── StockPriceHistory.java │ │ └── StockPriceUtils.java ├── build.xml ├── impl │ ├── README │ ├── build.xml │ └── src │ │ └── net │ │ └── sdo │ │ └── stockimpl │ │ ├── MockStockPriceEntityManagerFactory.java │ │ ├── StockOptionPK.java │ │ ├── StockOptionPriceEagerEagerImpl.java │ │ ├── StockOptionPriceEagerLazyImpl.java │ │ ├── StockOptionPriceEagerLazyQueryCacheImpl.java │ │ ├── StockOptionPriceLazyEagerImpl.java │ │ ├── StockOptionPriceLazyLazyImpl.java │ │ ├── StockPriceEagerEagerImpl.java │ │ ├── StockPriceEagerLazyImpl.java │ │ ├── StockPriceEagerLazyQueryCacheImpl.java │ │ ├── StockPriceHistoryCompress.java │ │ ├── StockPriceHistoryCompressLazy.java │ │ ├── StockPriceHistoryCompressUnbuffered.java │ │ ├── StockPriceHistoryEager.java │ │ ├── StockPriceHistoryEagerTransient.java │ │ ├── StockPriceHistoryExternalizable.java │ │ ├── StockPriceHistoryImpl.java │ │ ├── StockPriceHistoryLogger.java │ │ ├── StockPriceLazyEagerImpl.java │ │ ├── StockPriceLazyLazyImpl.java │ │ └── StockPricePK.java └── setenv.csh └── SecondEdition ├── AOT ├── .attach_pid21621 ├── .attach_pid21907 ├── ListDir.java ├── clean.sh ├── listdir └── run-list.sh ├── EpsilonGC └── net │ └── sdo │ └── EpsilonTest.java ├── Exception └── exception-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── ExceptionBenchmark.java ├── FalseSharing └── falsesharing-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── FalseSharingTest.java ├── Filter └── filter-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── FilterTest.java ├── ForkJoin └── forkjoin-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── ForkJoinBenchmark.java ├── JNI └── jni-benchmark │ ├── Makefile │ ├── README │ ├── pom.xml │ └── src │ └── main │ ├── c │ └── calc.c │ ├── h │ └── net_sdo_JNIBenchmark.h │ └── java │ └── net │ └── sdo │ └── JNIBenchmark.java ├── Lambda └── lambda-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── LambdaTest.java ├── ObjectSerialization └── serial-benchmark │ ├── .out.1.1.8.0_202.single-int.swp │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── ObjectSerialization.java ├── ParsingTest └── parsing-benchmark │ ├── README │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── net │ │ └── sdo │ │ ├── AbstractParsingTest.java │ │ ├── AbstractSAXParserTest.java │ │ ├── DOMBuilderTest.java │ │ ├── DOMSerializingTest.java │ │ ├── FilteringDOMBuilderTest.java │ │ ├── JAXBParsingTest.java │ │ ├── JAXBSerializingTest.java │ │ ├── JSONB.java │ │ ├── JSONBJackson.java │ │ ├── JSONBuilderTest.java │ │ ├── JSONParserTest.java │ │ ├── JsonJacksonParserTest.java │ │ ├── ParsingTest.java │ │ ├── SAXErrorHandler.java │ │ ├── SAXNoReuseParserTest.java │ │ ├── SAXParserTest.java │ │ ├── SAXValidatingCachingParserTest.java │ │ ├── SAXValidatingParserTest.java │ │ ├── StAXParserTest.java │ │ └── StAXValidatingCachingParserTest.java │ └── resources │ ├── json-schema │ └── find.json │ ├── json │ └── inputFile.json │ ├── xml │ └── inputFile.xml │ └── xsd │ └── schema.xsd ├── Random └── random-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── RandomBenchmark.java ├── Rest └── stockrest │ ├── .dockerignore │ ├── Dockerfile │ ├── README.md │ ├── app.yaml │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── net │ │ └── sdo │ │ └── stockrest │ │ ├── Compress.java │ │ ├── GZIPWriterInterceptor.java │ │ ├── Main.java │ │ ├── StockApplication.java │ │ ├── StockResource.java │ │ └── package-info.java │ └── resources │ ├── META-INF │ ├── beans.xml │ └── microprofile-config.properties │ └── logging.properties ├── Serialization └── serialization-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── SerializationTest.java ├── StockAPI ├── README ├── pom.xml └── src │ └── main │ └── java │ └── net │ └── sdo │ └── stock │ ├── StockOptionPrice.java │ ├── StockPrice.java │ ├── StockPriceHistory.java │ └── StockPriceUtils.java ├── StockAutoPool └── stockauto-benchmark │ ├── pom.xml │ ├── src │ └── main │ │ └── java │ │ └── net │ │ └── sdo │ │ └── StockAutoPool.java │ └── test.sh ├── StockBatching ├── README ├── pom.xml └── src │ └── main │ └── java │ └── net │ └── sdo │ └── StockPriceHistoryBatcher.java ├── StockBatchingPool └── stockpool-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── StockBatchingPool.java ├── StockCreateJDBC └── stockjdbc-benchmark │ ├── pom.xml │ ├── pom.xml.mysql │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── StockCreateJDBC.java ├── StockCreateJPA └── stockjpa-benchmark │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── net │ │ └── sdo │ │ └── StockCreateJPA.java │ └── resources │ └── META-INF │ └── persistence.xml ├── StockEar ├── README ├── StockEar-ear │ └── pom.xml ├── StockEar-ejb │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── sdo │ │ │ ├── StockSessionBeanImpl.java │ │ │ ├── StockSessionBeanLocal.java │ │ │ └── StockSessionBeanRemote.java │ │ └── resources │ │ └── META-INF │ │ └── persistence.xml ├── StockEar-war │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── sdo │ │ │ └── StockServlet.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ ├── error.jsp │ │ ├── history.jsp │ │ ├── longhistory.jsp │ │ └── option.jsp └── pom.xml ├── StockImpl ├── README ├── pom.xml └── src │ └── main │ └── java │ └── net │ └── sdo │ └── stockimpl │ ├── MockStockPriceEntityManagerFactory.java │ ├── StockOptionPK.java │ ├── StockOptionPriceEagerEagerImpl.java │ ├── StockOptionPriceEagerLazyImpl.java │ ├── StockOptionPriceEagerLazyQueryCacheImpl.java │ ├── StockOptionPriceLazyEagerImpl.java │ ├── StockOptionPriceLazyLazyImpl.java │ ├── StockPriceEagerEagerImpl.java │ ├── StockPriceEagerLazyImpl.java │ ├── StockPriceEagerLazyQueryCacheImpl.java │ ├── StockPriceHistoryCompress.java │ ├── StockPriceHistoryCompressLazy.java │ ├── StockPriceHistoryCompressUnbuffered.java │ ├── StockPriceHistoryEager.java │ ├── StockPriceHistoryEagerTransient.java │ ├── StockPriceHistoryExternalizable.java │ ├── StockPriceHistoryImpl.java │ ├── StockPriceHistoryLogger.java │ ├── StockPriceLazyEagerImpl.java │ ├── StockPriceLazyLazyImpl.java │ └── StockPricePK.java ├── StockReadJPA └── stockreadjpa-benchmark │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── net │ │ └── sdo │ │ └── StockReadJPA.java │ └── resources │ └── META-INF │ └── persistence.xml ├── StockRest └── stockrest │ ├── .dockerignore │ ├── Dockerfile │ ├── app.yaml │ ├── etc │ └── checkstyle.xml │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── sdo │ │ │ └── stockrest │ │ │ ├── Main.java │ │ │ ├── StockApplication.java │ │ │ ├── StockResource.java │ │ │ └── package-info.java │ └── resources │ │ ├── META-INF │ │ ├── beans.xml │ │ └── microprofile-config.properties │ │ └── logging.properties │ └── test │ ├── java │ └── net │ │ └── sdo │ │ └── stockrest │ │ └── MainTest.java │ └── resources │ └── META-INF │ └── microprofile-config.properties ├── StringConcatenation └── stringconcat-benchmark │ ├── pom-11.xml │ ├── pom-8.xml │ └── src │ └── main │ └── java │ └── net │ └── sdo │ └── StringConcatBenchmark.java ├── StringIntern └── string-intern-benchmark │ ├── pom.xml │ └── src │ └── main │ └── java │ ├── extra166y │ └── CustomConcurrentHashMap.java │ └── net │ └── sdo │ └── StringInternTest.java └── UnsyncCollection └── unsync-collection-benchmark ├── pom.xml └── src └── main └── java └── net └── sdo └── UncontendedTest.java /FirstEdition/ClassLoading/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/classloading.jar jarfile nThreads useLargeStack 6 | 7 | jarfile -- All classes from this jar file will be loaded in the customer 8 | class loader 9 | nThreads -- Number of threads to execute simultaneously 10 | useLargeStack -- If true, then the lookup will be performed from a 11 | call stack of 40 entries 12 | 13 | In general, the examples leverage the modules from glassfish, but any 14 | large set of jar files and classes can be used. 15 | 16 | Notes on Examples: 17 | 18 | Example 12-2 19 | java -jar jars/classloading.jar /path_to/guava.jar N false 20 | where N varies from 1 to 8 21 | 22 | Example 12-3 23 | java -jar jars/classloading.jar /path_to/guava.jar N false 24 | where N varies from 1 to 8 25 | and where the CLASSPATH contains all jar files from glassfish 26 | (266 entries) 27 | -------------------------------------------------------------------------------- /FirstEdition/ClassLoading/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/ClassLoading/src/net/sdo/PausingThreadPoolExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.util.concurrent.BlockingQueue; 8 | import java.util.concurrent.CountDownLatch; 9 | import java.util.concurrent.ThreadPoolExecutor; 10 | import java.util.concurrent.TimeUnit; 11 | import java.util.concurrent.locks.Condition; 12 | import java.util.concurrent.locks.ReentrantLock; 13 | 14 | public class PausingThreadPoolExecutor extends ThreadPoolExecutor { 15 | 16 | private boolean isPaused; 17 | private ReentrantLock pauseLock = new ReentrantLock(); 18 | private Condition unpaused = pauseLock.newCondition(); 19 | private CountDownLatch latch; 20 | private BlockingQueue queue; 21 | private int latchCount; 22 | 23 | public PausingThreadPoolExecutor(int nThreads, BlockingQueue queue) { 24 | super(nThreads, nThreads, Long.MAX_VALUE, TimeUnit.DAYS, queue); 25 | this.queue = queue; 26 | } 27 | 28 | @Override 29 | protected void beforeExecute(Thread t, Runnable r) { 30 | super.beforeExecute(t, r); 31 | pauseLock.lock(); 32 | try { 33 | while (isPaused) { 34 | unpaused.await(); 35 | } 36 | } catch (InterruptedException ie) { 37 | t.interrupt(); 38 | } finally { 39 | pauseLock.unlock(); 40 | } 41 | } 42 | 43 | public void addTask(Runnable r) { 44 | latchCount++; 45 | queue.add(r); 46 | } 47 | 48 | @Override 49 | protected void afterExecute(Runnable r, Throwable t) { 50 | latch.countDown(); 51 | } 52 | 53 | public void pause() { 54 | pauseLock.lock(); 55 | try { 56 | isPaused = true; 57 | } finally { 58 | pauseLock.unlock(); 59 | } 60 | } 61 | 62 | public void resume() { 63 | pauseLock.lock(); 64 | latch = new CountDownLatch(latchCount); 65 | latchCount = 0; 66 | try { 67 | isPaused = false; 68 | unpaused.signalAll(); 69 | } finally { 70 | pauseLock.unlock(); 71 | } 72 | try { 73 | latch.await(); 74 | } catch (InterruptedException ex) { 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /FirstEdition/CountFilter/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/Exception/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/exception.jar numLoops pctExceptions 6 | 7 | Example 12-5: 8 | Run with 1000000 100 9 | 10 | Example 12-6: 11 | Run with 1000000 1 12 | -------------------------------------------------------------------------------- /FirstEdition/Exception/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/FalseSharing/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/falsesharing.jar nLoops 6 | nLoops -- number of loops to run (e.g., 1,000,000,000) 7 | nThreads -- number of threads to use 8 | 9 | Example 9-8: Note that the results are from a size of 1 billion, not 1 million 10 | -------------------------------------------------------------------------------- /FirstEdition/FalseSharing/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/FalseSharing/src/net/sdo/ContendedTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | public class ContendedTest extends Thread { 8 | private static class DataHolder { 9 | private volatile long l1 = 0; 10 | private volatile long l2 = 0; 11 | private volatile long l3 = 0; 12 | private volatile long l4 = 0; 13 | } 14 | 15 | private static DataHolder dh = new DataHolder(); 16 | private static long nLoops; 17 | 18 | public ContendedTest(Runnable r) { 19 | super(r); 20 | } 21 | 22 | public static void main(String[] args) throws Exception { 23 | nLoops = Long.parseLong(args[0]); 24 | int nThreads = Integer.parseInt(args[1]); 25 | ContendedTest[] tests = new ContendedTest[4]; 26 | tests[0] = new ContendedTest(() -> { 27 | for (long i = 0; i < nLoops; i++) { 28 | dh.l1 += i; 29 | } 30 | }); 31 | tests[1] = new ContendedTest(() -> { 32 | for (long i = 0; i < nLoops; i++) { 33 | dh.l2 += i; 34 | } 35 | }); 36 | tests[2] = new ContendedTest(() -> { 37 | for (long i = 0; i < nLoops; i++) { 38 | dh.l3 += i; 39 | } 40 | }); 41 | tests[3] = new ContendedTest(() -> { 42 | for (long i = 0; i < nLoops; i++) { 43 | dh.l4 += i; 44 | } 45 | }); 46 | long then = System.currentTimeMillis(); 47 | for (int i = 0; i < nThreads; i++) { 48 | tests[i].start(); 49 | } 50 | for (int i = 0; i < nThreads; i++) { 51 | tests[i].join(); 52 | } 53 | long now = System.currentTimeMillis(); 54 | System.out.println("Duration: " + (now - then) + " ms"); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /FirstEdition/FindFilter/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/findfilter.jar Multi|Eager 6 | 7 | Multi -- use multiple filters 8 | Eager -- use eager array calculation 9 | -------------------------------------------------------------------------------- /FirstEdition/FindFilter/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/JNI/Makefile: -------------------------------------------------------------------------------- 1 | default: bin/randtest 2 | 3 | bin/calc.o : src/c/calc.c 4 | -@mkdir bin 5 | gcc -c -O2 -Isrc/h -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -fPIC -o bin/calc.o src/c/calc.c 6 | 7 | lib/libRandomTestCLibrary.so : bin/calc.o 8 | -@mkdir lib 9 | ld -shared -o lib/libRandomTestCLibrary.so -fPIC bin/calc.o 10 | 11 | bin/randtest : src/c/main.c lib/libRandomTestCLibrary.so 12 | -@mkdir bin 13 | gcc -O2 -Isrc/h -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -fPIC -o bin/randtest src/c/main.c -L lib -l RandomTestCLibrary 14 | 15 | library: lib/libRandomTestCLibrary.so 16 | -------------------------------------------------------------------------------- /FirstEdition/JNI/README: -------------------------------------------------------------------------------- 1 | To build: 2 | For the java code, 3 | ant 4 | 5 | The C library is set up to build on Linux using gcc 6 | make library 7 | 8 | The C executable is set up to build on Linux using gcc 9 | make 10 | 11 | To run: 12 | java -DLIBPATH=lib -jar jars/random.jar 10000 100000 13 | yields the first for lines in Example 12-4 14 | 15 | env LD_LIBRARY_PATH=lib bin/randtest 10000 100000 16 | yields the C only value 17 | -------------------------------------------------------------------------------- /FirstEdition/JNI/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /FirstEdition/JNI/src/c/calc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | double calcInC(int nValues) { 9 | long d = 0; 10 | int j; 11 | for (j = 0; j < nValues; j++) { 12 | int n = rand() % 100 + 1; 13 | d += n; 14 | } 15 | double average = (double) d / nValues; 16 | return average; 17 | } 18 | 19 | JNIEXPORT jdouble JNICALL Java_net_sdo_RandomTestJava_calc0 20 | (JNIEnv *env, jclass obj, jint nValues) { 21 | return calcInC(nValues); 22 | } 23 | 24 | jclass class = NULL; 25 | jmethodID methodID; 26 | 27 | JNIEXPORT jdouble JNICALL Java_net_sdo_RandomTestJava_calcFromC 28 | (JNIEnv *env, jobject this, jint nTrials, jint nValues) { 29 | 30 | if (class == NULL) { 31 | class = (*env)->FindClass(env, "net/sdo/RandomTestJava"); 32 | if (class == NULL) { 33 | fprintf(stderr, "Can't find class"); 34 | exit(-1); 35 | } 36 | methodID = (*env)->GetMethodID(env, class, "calc", "(I)D"); 37 | if (methodID == NULL) { 38 | fprintf(stderr, "Can't find method"); 39 | exit(-1); 40 | } 41 | } 42 | double error = 0; 43 | int i; 44 | for (i = 0; i < nTrials; i++) { 45 | double average = (*env)->CallDoubleMethod(env, this, methodID, nValues); 46 | error += 50 - average; 47 | } 48 | return error; 49 | } 50 | 51 | JNIEXPORT jint JNICALL Java_net_sdo_RandomTestJava_getCRandom 52 | (JNIEnv *env, jobject this) { 53 | return rand() % 100 + 1; 54 | } 55 | -------------------------------------------------------------------------------- /FirstEdition/JNI/src/c/main.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | double calcInC(int); 11 | 12 | int main(int argc, char** argv) { 13 | struct timeval then; 14 | gettimeofday(&then, NULL); 15 | int nTrials, nValues; 16 | sscanf(argv[1], "%d", &nTrials); 17 | sscanf(argv[2], "%d", &nValues); 18 | double error = 0; 19 | int i; 20 | srand(time(NULL)); 21 | for (i = 0; i < nTrials; i++) { 22 | double average = calcInC(nValues); 23 | error += 50 - average; 24 | } 25 | struct timeval now; 26 | gettimeofday(&now, NULL); 27 | printf("Error: %lf calculated in %ld\n", error, (now.tv_sec - then.tv_sec) * 1000 + now.tv_usec / 1000 - then.tv_usec / 1000); 28 | exit(EXIT_SUCCESS); 29 | } 30 | -------------------------------------------------------------------------------- /FirstEdition/JStack/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/ParseJStack.jar [-name name] file1 ... 6 | -name -- Restrict to threads that contain the given string in their name 7 | file1 ... fileN -- Files of jstack output to parse 8 | -------------------------------------------------------------------------------- /FirstEdition/JStack/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/Lambda/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/lambda.jar nLoops classname 6 | or 7 | java -jar jars/lambda.jar nLoops class_dir classname 8 | 9 | nLoops -- number of loops to run calculation for. The second and third 10 | averages should converge to roughly the same number to show that 11 | compiler optimizations have occurred 12 | The classname argument should be either 13 | net.sdo.AnonymousIntTest or net.sdo.LambdaIntTest 14 | If the class_dir option is given, then each iteration will load the 15 | clsases in a new classloader 16 | 17 | Example 12-8: 18 | java -cp classes:classes.url net.sdo.ClassLoadingDelegate 5000 net.sdo.LambdaIntTest 19 | java -cp classes:classes.url net.sdo.ClassLoadingDelegate 5000 net.sdo.AnonymousIntTest 20 | 21 | Example 12-9: 22 | java -cp classes net.sdo.ClassLoadingDelegate 500 classes.url net.sdo.LambdaIntTest 23 | java -cp classes net.sdo.ClassLoadingDelegate 500 classes.url net.sdo.AnonymousIntTest 24 | -------------------------------------------------------------------------------- /FirstEdition/Lambda/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /FirstEdition/Lambda/src/net/sdo/CalcInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | public interface CalcInterface { 8 | int calc(); 9 | } 10 | -------------------------------------------------------------------------------- /FirstEdition/Lambda/src/net/sdo/ClassLoadingDelegate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.File; 8 | import java.net.URL; 9 | import java.net.URLClassLoader; 10 | 11 | public class ClassLoadingDelegate { 12 | public static volatile long sum; 13 | 14 | public static void main(String[] args) throws Exception { 15 | int N = Integer.parseInt(args[0]); 16 | ClassLoader cl; 17 | CalcInterface ci = null; 18 | URL[] urls = null; 19 | if (args.length > 2) { 20 | urls = new URL[]{new File(args[1]).toURL()}; 21 | } 22 | else { 23 | cl = ClassLoadingDelegate.class.getClassLoader(); 24 | ci = (CalcInterface) cl.loadClass(args[1]).newInstance(); 25 | } 26 | for (int j = 0; j < 3; j++) { 27 | sum = 0; 28 | long then = System.nanoTime(); 29 | for (int i = 0; i < N; i++) { 30 | if (args.length > 2) { 31 | cl = new URLClassLoader(urls); 32 | ci = (CalcInterface) cl.loadClass(args[2]).newInstance(); 33 | } 34 | sum += ci.calc(); 35 | } 36 | System.out.println(sum); 37 | long now = System.nanoTime(); 38 | long time = (now - then) / 1000; 39 | System.out.println("Average time " + j + ": " + ((double) (time) / N)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /FirstEdition/Lambda/src/net/sdo/IntegerInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | public interface IntegerInterface { 8 | int getInt(); 9 | } 10 | -------------------------------------------------------------------------------- /FirstEdition/ParallelTask/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/paralleltask.jar nThreads nDoubles mode \ 6 | balanced [size] 7 | nThreads - Size of the executor or fork/join pool 8 | nDoubles - Size of the double array 9 | mode - 0 for thread pool, 1 for fork/join pool 10 | unbalanced - false for the balanced test; true for the unbalanced test 11 | size - optional, size at which to switch from recursive fork/join 12 | tasks to simple array counting (default 10) 13 | 14 | Example 9-4: 15 | java -Xmx4g -Xms4g -jar jars/paralleltask.jar 1_or_4 100000000 0_or_1 false 16 | 17 | Example 9-5: 18 | java -Xmx4g -Xms4g -jar jars/paralleltask.jar 1_or_4 10000 0_or_1 true 19 | 20 | Example 9-6: 21 | java -Xmx4g -Xms4g -jar jars/paralleltask.jar 1_or_4 100000000 \ 22 | 0_or_1 false desired_size 23 | -------------------------------------------------------------------------------- /FirstEdition/ParallelTask/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/DOMTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.DOMBuilderTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/FilteringDOMTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.FilteringDOMBuilderTest 3 | targetItemCount=10 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JAXBSerializeTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.JAXBSerializingTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JAXBTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.JAXBParsingTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JacksonFilteringTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/json/ebay.json 3 | className=net.sdo.JsonJacksonParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JacksonTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/json/ebay.json 2 | className=net.sdo.JsonJacksonParserTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JsonBuilderTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/json/ebay.json 2 | className=net.sdo.JSONBuilderTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JsonFilteringTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/json/ebay.json 3 | className=net.sdo.JSONParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/JsonTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/json/ebay.json 2 | className=net.sdo.JSONParserTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/NonValidSaxFilteringTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/xml/ebay.xml 3 | className=net.sdo.SAXNonValidatingParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/NonValidSaxTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.SAXNonValidatingParserTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/StaxFilteringTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/xml/ebay.xml 3 | className=net.sdo.StAXParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/StaxTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.StAXParserTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/ValidCacheSaxTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.SAXValidatingCachingParserTest 3 | schemaFileName=docs/xsd/ebay.xsd 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/ValidCacheStaxTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.StAXValidatingCachingParserTest 3 | schemaFileName=docs/xsd/ebay.xsd 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/ValidSaxTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/xml/ebay.xml 3 | className=net.sdo.SAXValidatingParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/WoodstoxFilteringTest.props: -------------------------------------------------------------------------------- 1 | targetItemCount=10 2 | inputFile=docs/xml/ebay.xml 3 | className=net.sdo.StAXParserTest 4 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/WoodstoxTest.props: -------------------------------------------------------------------------------- 1 | inputFile=docs/xml/ebay.xml 2 | className=net.sdo.StAXParserTest 3 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/docs/json/README: -------------------------------------------------------------------------------- 1 | Obtain the JSON for the popular eBay Items are store in ebay.json 2 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/docs/xml/README: -------------------------------------------------------------------------------- 1 | Obtain the sample eBay popular items and store the unchanged XML file in 2 | ebay.xml. 3 | 4 | Trim all the whitespace from the XML and store in ebay-trim.xml 5 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/AbstractParsingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.util.ArrayList; 11 | 12 | public abstract class AbstractParsingTest { 13 | public static String ITEM_ID = "ItemID"; 14 | protected boolean isItemID = false; 15 | protected byte[] data; 16 | protected int targetItemCount; 17 | private ArrayList itemIDs; 18 | 19 | protected void engineInit(RunParams rp) throws IOException {} 20 | protected abstract void engineRun(InputStream is) throws IOException; 21 | 22 | protected boolean addItemId(String s) { 23 | itemIDs.add(s); 24 | if (itemIDs.size() == targetItemCount) { 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | public void init(RunParams rp) throws IOException { 31 | targetItemCount = rp.getTargetItemCount(); 32 | data = rp.getInput(); 33 | engineInit(rp); 34 | } 35 | 36 | public void run() throws IOException { 37 | itemIDs = new ArrayList((targetItemCount == Integer.MAX_VALUE) ? 128 : targetItemCount); 38 | isItemID = false; 39 | engineRun(new ByteArrayInputStream(data)); 40 | if (targetItemCount != Integer.MAX_VALUE && targetItemCount != itemIDs.size()) { 41 | throw new IOException("Unexpected count in document, found " + itemIDs.size()); 42 | } 43 | if (System.getProperty("debugParse") != null) { 44 | System.out.println(itemIDs); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/DOMBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.xml.parsers.DocumentBuilder; 10 | import javax.xml.parsers.DocumentBuilderFactory; 11 | import javax.xml.parsers.ParserConfigurationException; 12 | import net.sourceforge.sizeof.SizeOf; 13 | import org.w3c.dom.Document; 14 | import org.xml.sax.SAXException; 15 | 16 | public class DOMBuilderTest extends AbstractParsingTest { 17 | protected DocumentBuilderFactory dbf; 18 | protected DocumentBuilder db; 19 | 20 | @Override 21 | protected void engineInit(RunParams rp) throws IOException { 22 | dbf = DocumentBuilderFactory.newInstance(); 23 | dbf.setNamespaceAware(true); 24 | try { 25 | db = dbf.newDocumentBuilder(); 26 | } catch (ParserConfigurationException ex) { 27 | throw new IOException("Can't get document builder", ex); 28 | } 29 | db.setErrorHandler(new SAXErrorHandler()); 30 | } 31 | 32 | @Override 33 | protected void engineRun(InputStream in) throws IOException { 34 | try { 35 | db.reset(); 36 | Document doc = db.parse(in); 37 | if (System.getProperty("parseDebug") != null) { 38 | System.out.println("Waiting for input"); 39 | System.in.read(); 40 | } 41 | } catch (SAXException ex) { 42 | throw new IOException("Can't Parse" , ex); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/DOMSerializingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.io.ByteArrayOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import javax.xml.parsers.DocumentBuilder; 12 | import javax.xml.parsers.DocumentBuilderFactory; 13 | import javax.xml.parsers.ParserConfigurationException; 14 | import javax.xml.transform.Transformer; 15 | import javax.xml.transform.TransformerConfigurationException; 16 | import javax.xml.transform.TransformerException; 17 | import javax.xml.transform.TransformerFactory; 18 | import javax.xml.transform.dom.DOMSource; 19 | import javax.xml.transform.stream.StreamResult; 20 | import org.w3c.dom.Document; 21 | import org.xml.sax.SAXException; 22 | 23 | public class DOMSerializingTest extends AbstractParsingTest { 24 | protected DocumentBuilderFactory dbf; 25 | protected DocumentBuilder db; 26 | protected Document doc; 27 | private ByteArrayOutputStream output; 28 | private Transformer transformer; 29 | 30 | @Override 31 | protected void engineInit(RunParams rp) throws IOException { 32 | dbf = DocumentBuilderFactory.newInstance(); 33 | dbf.setNamespaceAware(true); 34 | try { 35 | db = dbf.newDocumentBuilder(); 36 | db.setErrorHandler(new SAXErrorHandler()); 37 | doc = db.parse(new ByteArrayInputStream(rp.getInput())); 38 | transformer = TransformerFactory.newInstance().newTransformer(); 39 | //} catch (TransformerConfigurationException | ParserConfigurationException | SAXException ex) { 40 | } catch (Exception ex) { 41 | throw new IOException("Can't get document builder", ex); 42 | } 43 | output = new ByteArrayOutputStream(); 44 | } 45 | 46 | @Override 47 | protected void engineRun(InputStream in) throws IOException { 48 | try { 49 | output.reset(); 50 | DOMSource dsource = new DOMSource(doc); 51 | StreamResult result = new StreamResult(output); 52 | transformer.transform(dsource, result); 53 | } catch (TransformerException ex) { 54 | throw new IOException("Can't Serialize", ex); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/JAXBParsingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.xml.bind.JAXBContext; 10 | import javax.xml.bind.JAXBException; 11 | import javax.xml.bind.Unmarshaller; 12 | 13 | public class JAXBParsingTest extends AbstractParsingTest { 14 | private JAXBContext jc; 15 | private Unmarshaller u; 16 | @Override 17 | protected void engineInit(RunParams rp) throws IOException { 18 | try { 19 | jc = JAXBContext.newInstance("net.sdo.jaxb"); 20 | u = jc.createUnmarshaller(); 21 | } catch (JAXBException ex) { 22 | throw new IOException("Can't create JAXB context", ex); 23 | } 24 | } 25 | 26 | @Override 27 | protected void engineRun(InputStream is) throws IOException { 28 | try { 29 | u.unmarshal(is); 30 | } catch (JAXBException ex) { 31 | throw new IOException("Can't create JAXB object", ex); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/JAXBSerializingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.io.ByteArrayOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import javax.xml.bind.JAXBContext; 12 | import javax.xml.bind.JAXBException; 13 | import javax.xml.bind.Marshaller; 14 | import javax.xml.bind.Unmarshaller; 15 | import net.sdo.jaxb.FindPopularItemsResponse; 16 | 17 | public class JAXBSerializingTest extends AbstractParsingTest { 18 | private JAXBContext jc; 19 | private FindPopularItemsResponse responseItems; 20 | private ByteArrayOutputStream os; 21 | 22 | @Override 23 | protected void engineInit(RunParams rp) throws IOException { 24 | try { 25 | jc = JAXBContext.newInstance("net.sdo.jaxb"); 26 | Unmarshaller u = jc.createUnmarshaller(); 27 | responseItems = (FindPopularItemsResponse) u.unmarshal(new ByteArrayInputStream(rp.getInput())); 28 | os = new ByteArrayOutputStream(); 29 | } catch (JAXBException ex) { 30 | throw new IOException("Can't create JAXB context", ex); 31 | } 32 | } 33 | 34 | @Override 35 | protected void engineRun(InputStream is) throws IOException { 36 | try { 37 | os.reset(); 38 | Marshaller m = jc.createMarshaller(); 39 | m.marshal(responseItems,os); 40 | } catch (JAXBException ex) { 41 | throw new IOException("Can't create JAXB object", ex); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/JSONBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.json.Json; 10 | import javax.json.JsonReader; 11 | 12 | public class JSONBuilderTest extends AbstractParsingTest { 13 | @Override 14 | protected void engineRun(InputStream in) throws IOException { 15 | JsonReader jr = Json.createReader(in); 16 | jr.read(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/JSONParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.json.Json; 10 | import javax.json.stream.JsonParser; 11 | import javax.json.stream.JsonParser.Event; 12 | import javax.json.stream.JsonParserFactory; 13 | 14 | public class JSONParserTest extends AbstractParsingTest { 15 | private JsonParserFactory factory; 16 | 17 | @Override 18 | protected void engineInit(RunParams rp) throws IOException { 19 | factory = Json.createParserFactory(null); 20 | } 21 | 22 | @Override 23 | protected void engineRun(InputStream in) throws IOException { 24 | JsonParser parser = factory.createParser(in); 25 | int itemCount = 0; 26 | while (parser.hasNext()) { 27 | Event event = parser.next(); 28 | switch (event) { 29 | case KEY_NAME: 30 | String s = parser.getString(); 31 | if (ITEM_ID.equals(s)) { 32 | isItemID = true; 33 | } 34 | break; 35 | case VALUE_STRING: 36 | if (isItemID) { 37 | if (addItemId(parser.getString())) { 38 | return; 39 | } 40 | isItemID = false; 41 | } 42 | continue; 43 | default: 44 | continue; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/JsonJacksonParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.fasterxml.jackson.core.JsonFactory; 8 | import com.fasterxml.jackson.core.JsonParser; 9 | import com.fasterxml.jackson.core.JsonToken; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | 13 | public class JsonJacksonParserTest extends AbstractParsingTest { 14 | private JsonFactory jsonFactory; 15 | 16 | @Override 17 | protected void engineInit(RunParams rp) { 18 | jsonFactory = new JsonFactory(); 19 | } 20 | 21 | @Override 22 | protected void engineRun(InputStream is) throws IOException { 23 | JsonParser parser = jsonFactory.createParser(is); 24 | int itemCount = 0; 25 | for (JsonToken token = parser.nextToken(); token != null; token = parser.nextToken()) { 26 | switch (token) { 27 | case FIELD_NAME: 28 | String s = parser.getCurrentName(); 29 | if (ITEM_ID.equals(s)) { 30 | isItemID = true; 31 | } 32 | break; 33 | case VALUE_STRING: 34 | if (isItemID) { 35 | String s2 = parser.getText(); 36 | if (addItemId(s2)) { 37 | return; 38 | } 39 | isItemID = false; 40 | } 41 | break; 42 | default: 43 | break; 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/ParsingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | public class ParsingTest { 8 | public static void main(String[] args) throws Exception { 9 | int nIterations = 1000000; 10 | int nWarmupIterations = 1000000; 11 | int curArg = 0; 12 | while (args[curArg].startsWith("-")) { 13 | if (args[curArg].equals("-n")) { 14 | nIterations = Integer.parseInt(args[++curArg]); 15 | } 16 | else if (args[curArg].equals("-w")) { 17 | nWarmupIterations = Integer.parseInt(args[++curArg]); 18 | } 19 | else { 20 | usage(); 21 | } 22 | curArg++; 23 | } 24 | RunParams rp = new RunParams(args[curArg]); 25 | AbstractParsingTest pti = (AbstractParsingTest) Class.forName(rp.getClassName()).newInstance(); 26 | pti.init(rp); 27 | for (int i = 0; i < nWarmupIterations; i++) { 28 | pti.run(); 29 | } 30 | System.gc(); 31 | System.runFinalization(); 32 | System.gc(); 33 | System.out.println("Starting Measurement"); 34 | long then = System.currentTimeMillis(); 35 | for (int i = 0; i < nIterations; i++) { 36 | pti.run(); 37 | } 38 | long now = System.currentTimeMillis(); 39 | long elapsed = now - then; 40 | System.out.println("Time to execute " + nIterations + " iterations is " + elapsed); 41 | System.out.println("Time per operation: " + ((double) elapsed) / nIterations); 42 | } 43 | 44 | private static void usage() { 45 | System.out.println("Usage:"); 46 | System.out.println("java ParsingTest TestClassName"); 47 | System.exit(-1); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/RunParams.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.FileInputStream; 8 | import java.io.FileReader; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.util.Properties; 12 | 13 | public class RunParams { 14 | private Properties props; 15 | 16 | public RunParams(String filename) throws IOException { 17 | props = new Properties(); 18 | props.load(new FileReader(filename)); 19 | } 20 | 21 | public byte[] getInput() throws IOException { 22 | byte[] b; 23 | InputStream is = new FileInputStream(props.getProperty("inputFile")); 24 | int size = is.available(); 25 | b = new byte[size]; 26 | is.read(b); 27 | is.close(); 28 | return b; 29 | } 30 | 31 | public String getClassName() { 32 | return props.getProperty("className"); 33 | } 34 | 35 | public int getTargetItemCount() { 36 | String s = props.getProperty("targetItemCount"); 37 | if (s == null) { 38 | return Integer.MAX_VALUE; 39 | } 40 | return Integer.parseInt(s); 41 | } 42 | 43 | public String getSchemaFileName() { 44 | return props.getProperty("schemaFileName"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/SAXErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import org.xml.sax.ErrorHandler; 8 | import org.xml.sax.SAXParseException; 9 | 10 | public class SAXErrorHandler implements ErrorHandler { 11 | 12 | public void warning(SAXParseException spe) throws SAXParseException { 13 | System.out.println("SAX Warning" + spe); 14 | throw spe; 15 | } 16 | 17 | public void error(SAXParseException spe) throws SAXParseException { 18 | System.out.println("SAX error" + spe); 19 | throw spe; 20 | } 21 | 22 | public void fatalError(SAXParseException spe) throws SAXParseException { 23 | System.out.println("SAX fatalError" + spe); 24 | throw spe; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/SAXNonValidatingParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import javax.xml.parsers.ParserConfigurationException; 9 | import javax.xml.parsers.SAXParser; 10 | import javax.xml.parsers.SAXParserFactory; 11 | import org.xml.sax.SAXException; 12 | import org.xml.sax.XMLReader; 13 | 14 | public class SAXNonValidatingParserTest extends SAXParserTest { 15 | private SAXParser parser; 16 | 17 | @Override 18 | protected void engineInit(RunParams rp) throws IOException { 19 | try { 20 | SAXParserFactory spf = SAXParserFactory.newInstance(); 21 | spf.setValidating(false); 22 | spf.setNamespaceAware(false); 23 | parser = spf.newSAXParser(); 24 | } catch (Exception ex) { 25 | throw new IOException("Can't initialize parser" , ex); 26 | } 27 | } 28 | 29 | @Override 30 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 31 | parser.reset(); 32 | return parser.getXMLReader(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/SAXParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.xml.parsers.ParserConfigurationException; 10 | import org.xml.sax.Attributes; 11 | import org.xml.sax.InputSource; 12 | import org.xml.sax.SAXException; 13 | import org.xml.sax.SAXParseException; 14 | import org.xml.sax.XMLReader; 15 | import org.xml.sax.helpers.DefaultHandler; 16 | 17 | public abstract class SAXParserTest extends AbstractParsingTest { 18 | 19 | protected abstract XMLReader getReader() throws ParserConfigurationException, SAXException; 20 | 21 | private static class SAXDoneException extends SAXParseException { 22 | 23 | public SAXDoneException(String s) { 24 | super(s, null); 25 | } 26 | } 27 | 28 | protected class CustomizedInnerHandler extends DefaultHandler { 29 | @Override 30 | public void startElement(String space, String name, String raw, Attributes atts) { 31 | if (name.length() == 0) { 32 | name = raw; 33 | } 34 | if (name.equalsIgnoreCase(ITEM_ID)) { 35 | isItemID = true; 36 | } 37 | } 38 | 39 | @Override 40 | public void characters(char[] ch, int start, int length) throws SAXDoneException { 41 | if (isItemID) { 42 | String s = new String(ch, start, length); 43 | isItemID = false; 44 | if (addItemId(s)) { 45 | throw new SAXDoneException("Done"); 46 | } 47 | } 48 | } 49 | } 50 | 51 | @Override 52 | protected void engineRun(InputStream ins) throws IOException { 53 | try { 54 | InputSource source = new InputSource(); 55 | XMLReader reader = getReader(); 56 | CustomizedInnerHandler handler = new CustomizedInnerHandler(); 57 | reader.setContentHandler(handler); 58 | source.setByteStream(ins); 59 | reader.parse(source); 60 | } catch (SAXDoneException sde) { 61 | int i = 0; 62 | } catch (Exception ex) { 63 | throw new IOException("Can't complete parsing", ex); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/SAXValidatingCachingParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import javax.xml.XMLConstants; 9 | import javax.xml.parsers.ParserConfigurationException; 10 | import javax.xml.parsers.SAXParser; 11 | import javax.xml.parsers.SAXParserFactory; 12 | import javax.xml.transform.Source; 13 | import javax.xml.transform.stream.StreamSource; 14 | import javax.xml.validation.Schema; 15 | import javax.xml.validation.SchemaFactory; 16 | import org.xml.sax.SAXException; 17 | import org.xml.sax.XMLReader; 18 | 19 | public class SAXValidatingCachingParserTest extends SAXParserTest { 20 | private SAXParser parser; 21 | 22 | @Override 23 | protected void engineInit(RunParams rp) throws IOException { 24 | try { 25 | SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 26 | StreamSource ss = new StreamSource(rp.getSchemaFileName()); 27 | Schema schema = sf.newSchema(new Source[]{ss}); 28 | SAXParserFactory spf = SAXParserFactory.newInstance(); 29 | spf.setValidating(false); 30 | spf.setNamespaceAware(true); 31 | spf.setSchema(schema); 32 | parser = spf.newSAXParser(); 33 | } catch (Exception ex) { 34 | throw new IOException("Can't initialize parser", ex); 35 | } 36 | } 37 | 38 | @Override 39 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 40 | parser.reset(); 41 | XMLReader xr = parser.getXMLReader(); 42 | return xr; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/SAXValidatingParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.sun.org.apache.xerces.internal.jaxp.JAXPConstants; 8 | import java.io.IOException; 9 | import javax.xml.XMLConstants; 10 | import javax.xml.parsers.ParserConfigurationException; 11 | import javax.xml.parsers.SAXParser; 12 | import javax.xml.parsers.SAXParserFactory; 13 | import org.xml.sax.SAXException; 14 | import org.xml.sax.XMLReader; 15 | 16 | public class SAXValidatingParserTest extends SAXParserTest { 17 | private SAXParser parser; 18 | 19 | @Override 20 | protected void engineInit(RunParams rp) throws IOException { 21 | try { 22 | SAXParserFactory spf = SAXParserFactory.newInstance(); 23 | spf.setValidating(true); 24 | spf.setNamespaceAware(true); 25 | parser = spf.newSAXParser(); 26 | } catch (Exception ex) { 27 | throw new IOException("Can't initialize parser", ex); 28 | } 29 | } 30 | 31 | @Override 32 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 33 | parser.reset(); 34 | parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 35 | XMLReader xr = parser.getXMLReader(); 36 | xr.setErrorHandler(new SAXErrorHandler()); 37 | return xr; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/StAXParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import javax.xml.stream.XMLInputFactory; 10 | import javax.xml.stream.XMLStreamConstants; 11 | import javax.xml.stream.XMLStreamException; 12 | import javax.xml.stream.XMLStreamReader; 13 | 14 | public class StAXParserTest extends AbstractParsingTest { 15 | protected XMLInputFactory staxFactory; 16 | 17 | @Override 18 | protected void engineInit(RunParams rp) throws IOException { 19 | staxFactory = XMLInputFactory.newInstance(); 20 | staxFactory.setProperty (XMLInputFactory.IS_VALIDATING, Boolean.FALSE); 21 | } 22 | 23 | @Override 24 | protected void engineRun(InputStream ins) throws IOException { 25 | try { 26 | XMLStreamReader reader = staxFactory.createXMLStreamReader(ins); 27 | while (reader.hasNext()) { 28 | reader.next(); 29 | int state = reader.getEventType(); 30 | switch (state) { 31 | case XMLStreamConstants.START_ELEMENT: 32 | String s = reader.getLocalName(); 33 | if (ITEM_ID.equals(s)) { 34 | isItemID = true; 35 | } 36 | break; 37 | case XMLStreamConstants.CHARACTERS: 38 | if (isItemID) { 39 | String id = reader.getText(); 40 | isItemID = false; 41 | if (addItemId(id)) { 42 | return; 43 | } 44 | } 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | } catch (XMLStreamException ex) { 51 | throw new IOException("STAX Parsing Exception ", ex); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /FirstEdition/ParsingTest/src/net/sdo/jaxb/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // 6 | 7 | @javax.xml.bind.annotation.XmlSchema(namespace = "urn:ebay:apis:eBLBaseComponents", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 8 | package net.sdo.jaxb; 9 | -------------------------------------------------------------------------------- /FirstEdition/README: -------------------------------------------------------------------------------- 1 | JavaPerformanceTuning 2 | ===================== 3 | 4 | Examples for O'Reilly & Associates Java Performance Tuning: The Definitive Guide 5 | 6 | Directories: 7 | api -- Basic Interfaces for common stock applications 8 | impl -- Implementations of api with different features (lazy, eager, mock entity, etc.) 9 | StockBatching -- StockBatching application 10 | 11 | Mapping of Applications to Text Examples 12 | Consult the individual README in each directory for more details 13 | 14 | Chapter 4 15 | Example 1: Download NetBeans, and time its startup with various options. 16 | Experiment with other applications you are interested in. 17 | Example 2: See StockBatching 18 | Example 3: See StockEar 19 | 20 | Chapter 5 21 | Example 1: See StockBatching 22 | Example 2: See StockEar 23 | Example 3: See StockEar 24 | 25 | Chapter 6 26 | Example 1: See StockEar 27 | Example 2: See StockEar 28 | 29 | Chapter 7 30 | Example 2: See Size 31 | Example 3: See StringIntern 32 | Example 4: See StockBatching 33 | 34 | Code for the sample finalization technique can be found in SampleFinalizer 35 | 36 | Chapter 9 37 | Example 1: See StockBatchingPool 38 | Example 2: See StockEar 39 | Example 3: See StockEar 40 | Example 4: See ParallelTask 41 | Example 5: See ParallelTask 42 | Example 6: See ParallelTask 43 | Example 7: See StockBatchingForkJoin 44 | Example 8: See FalseSharing 45 | 46 | Sample code for parsing JStack output can be found in JStack 47 | 48 | Chapter 10 49 | Example 1: See StockEar 50 | Example 2: See StockEar 51 | Example 3 - 10: See ParsingTest 52 | Example 11-13: See StockSerialization 53 | Example 14-15: See StockRestService and StockRestDriver 54 | 55 | Chapter 11 56 | Example 1: See StockCreateJDBC 57 | Example 2: See StockCreateJPA 58 | Example 3 - 6: See StockReadJPA 59 | 60 | Chatper 12 61 | Example 1: See StockSerialization 62 | Example 2-3: See ClassLoading 63 | Example 4: See JNI 64 | Example 5-6: See Exception 65 | Example 7: See UncontendedSync 66 | Example 8-9: See Lambda 67 | Example 10: See FindFilter 68 | Example 11: See CountFilter 69 | -------------------------------------------------------------------------------- /FirstEdition/SampleFinalizer/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/SampleFinalizer/src/net/sdo/TestCleanup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | public class TestCleanup { 8 | 9 | public static void main(String[] args) { 10 | CleanupClass c1 = new CleanupClass(); 11 | CleanupClass c2 = new CleanupClass(); 12 | System.out.println("C1 is " + c1); 13 | System.out.println("C2 is " + c2); 14 | c1.close(); 15 | c1 = null; 16 | c2 = null; 17 | for (int i = 0; i < 1000000; i++) { 18 | byte[] b = new byte[1024]; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /FirstEdition/Size/README: -------------------------------------------------------------------------------- 1 | To build: 2 | This examples uses the SizeOf library from SourceForge: 3 | http://sizeof.sourceforge.net/ 4 | Download the library from that site and set 5 | SIZEOF=/path_to/SizeOf.jar 6 | 7 | To run: 8 | java -javaagent:$SIZEOF -jar jars/size.jar 9 | This produces output for various sized objects referenced in the text 10 | (which can vary depending on the 32-/64-bit setting of the JVM 11 | and whether or not CompressedOops are in use). 12 | 13 | Example 7-2: 14 | Size of a, b and c classes 15 | -------------------------------------------------------------------------------- /FirstEdition/Size/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /FirstEdition/StockBatching/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Even if you are not using an entity manager, building this requires a 3 | JPA implementation (since the mock entity manager still uses JPA 4 | interfaces). Download an implementation (e.g., the reference implementation 5 | from GlassFish) and in your environment set 6 | 7 | JAVAX_PERSISTENCE=path_to/javax.persistence.jar 8 | 9 | Then run 10 | ant 11 | 12 | To run: 13 | In general, to use a MockEntityManager, run 14 | 15 | java -DMockEntityManager=MockEntityManager \ 16 | -jar jars/StockBatching.jar [num_stocks] \ 17 | [startDate] [endDate] [mode] [save] 18 | 19 | where 20 | num_stocks -- optional, default 10000 21 | startDate -- optional, default 01/01/12 22 | endDate -- optional, default 12/13/12 23 | mode -- optional, 0 is standard Impl; 1 is Impl with logging 24 | save -- optional, number of entries to keep in the heap 25 | 26 | Notes on Examples: 27 | Example 4-2: 28 | To limit the impact of writing to System.out, redirect to /dev/null 29 | 30 | time java -client -DMockEntityManager=MockEntityManager \ 31 | -jar jars/StockBatcher.jar 32 | time java -server -DMockEntityManager=MockEntityManager \ 33 | -jar jars/StockBatcher.jar 34 | time java -XX:+TieredCompilation -DMockEntityManager=MockEntityManager \ 35 | -jar jars/StockBatcher.jar 36 | 37 | Each command is run with an argument of 1, 10, 100, and so on to produce 38 | the results in the table. 39 | 40 | Example 5-1: 41 | To run, e.g. with 100 entries saved in a 2GB heap: 42 | time java -Xmx2g -Xms2g \ 43 | -XX:+UseConcMarkSweepGC -XX:+UseParNewGC \ 44 | -XX:+TieredCompilation -DMockEntityManager=MockEntityManager \ 45 | -jar jars/StockBatcher.jar 50000 1/1/01 12/31/06 0 100 46 | 47 | For the throughput collector, remove the CMS arguments. For G1, replace 48 | the CMS arguments with -XX:+UseG1GC 49 | 50 | Example 7-4: 51 | Run with this argument: 52 | -DMockEntityManager=StdRandomMockEntityManager 53 | -------------------------------------------------------------------------------- /FirstEdition/StockBatching/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /FirstEdition/StockBatchingForkJoin/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Even if you are not using an entity manager, building this requires a 3 | JPA implementation (since the mock entity manager still uses JPA 4 | interfaces). Download an implementation (e.g., the reference implementation 5 | from GlassFish) and in your environment set 6 | 7 | JAVAX_PERSISTENCE=path_to/javax.persistence.jar 8 | 9 | Then run 10 | ant 11 | 12 | To run: 13 | In general, to use a MockEntityManager, run 14 | 15 | java -DMockEntityManager=MockEntityManager \ 16 | -jar jars/StockBatchingForkJoin.jar \ 17 | [size_of_fork/join_pool] 18 | 19 | The fork/join pool size is optional and defaults to the number of CPUs 20 | on the machine. Remember that the operation will also use the calling 21 | thread, so the effective number of tasks running simultaneously is one 22 | more than the pool size. 23 | -------------------------------------------------------------------------------- /FirstEdition/StockBatchingForkJoin/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /FirstEdition/StockBatchingPool/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Even if you are not using an entity manager, building this requires a 3 | JPA implementation (since the mock entity manager still uses JPA 4 | interfaces). Download an implementation (e.g., the reference implementation 5 | from GlassFish) and in your environment set 6 | 7 | JAVAX_PERSISTENCE=path_to/javax.persistence.jar 8 | 9 | Then run 10 | ant 11 | 12 | To run: 13 | In general, to use a MockEntityManager, run 14 | 15 | java -DMockEntityManager=MockEntityManager \ 16 | -jar jars/StockBatchingPool.jar \ 17 | [core size] [max size] [0|1|2] 18 | 19 | where 20 | core size -- optional, default 1, core size of ThreadPoolExecutor 21 | max size -- optional, default # of CPUs on machine, max size of TPE 22 | 0|1|2 -- optional, default 0 23 | 0: Use a LinkedBlockingQueue 24 | 1: Use a SynchronousQueue 25 | 2: Use an ArrayBlockingQueue of 10,000 elements 26 | 27 | Notes on Examples: 28 | 29 | Example 9-1: 30 | java -jar jars/StockBatchingPool.jar N N 31 | where N is the number of threads desired in the pool 32 | 33 | 34 | -------------------------------------------------------------------------------- /FirstEdition/StockBatchingPool/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /FirstEdition/StockCreateJDBC/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /FirstEdition/StockCreateJDBC/stock.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE STOCKPRICE CASCADE CONSTRAINTS; 2 | CREATE TABLE STOCKPRICE ( 3 | SYMBOL VARCHAR(16) not null, 4 | PRICEDATE DATE not null, 5 | LOW DECIMAL(30,2) not null, 6 | HIGH DECIMAL(30,2) not null, 7 | OPENINGPRICE NUMERIC(30,2) not null, 8 | CLOSINGPRICE NUMERIC(30,2) not null, 9 | ISYEARHIGH INTEGER, 10 | ISYEARLOW INTEGER, 11 | primary key (symbol, pricedate) 12 | ); 13 | 14 | DROP TABLE STOCKOPTIONPRICE; 15 | CREATE TABLE STOCKOPTIONPRICE ( 16 | PRICE NUMERIC(30,2) not null, 17 | EXPIRATIONPERIOD INTEGER, 18 | PRICEDATE DATE not null, 19 | SYMBOL VARCHAR(16) not null, 20 | primary key (symbol, pricedate, expirationperiod), 21 | CONSTRAINT FK_OPTION 22 | FOREIGN KEY (PRICEDATE, SYMBOL) 23 | REFERENCES STOCKPRICE (PRICEDATE, SYMBOL) 24 | ); 25 | -------------------------------------------------------------------------------- /FirstEdition/StockCreateJPA/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.eclipse.persistence.jpa.PersistenceProvider 5 | lib/stockimpl.jar 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /FirstEdition/StockCreateJPA/README: -------------------------------------------------------------------------------- 1 | To build: 2 | This is a standalone (Java SE) application for JPA. 3 | In your environment, set 4 | JAVAX_PERSISTENCE=path_to/javax.persistence.jar 5 | 6 | The META-INF/persistence.xml file contains information that JPA will 7 | use for the database connection. Edit it for the appropriate values for 8 | your database. That file is based on the eclipselink JPA reference 9 | implementation; it will need other changes for Hibernate or other 10 | JPA implementations (chaging the provider class and eclipselink 11 | property names). 12 | 13 | Although the example doesn't use JDBC directly, you will need the 14 | appropriate JDBC driver to compile the application (so that the 15 | driver gets set into the classpath). In your environment, set 16 | JDBC_JAR=/path_to/ojdbc6.jar 17 | (or whichever driver you are using). 18 | 19 | To run: 20 | Use the schema file in StockCreateJDBC to create the necessary schema 21 | in the database. 22 | 23 | Running a Java SE program with JPA requires that the JPA classes be 24 | "enhanced". Eclipselink does this at run time using the javaagent 25 | argument. Other implementations may require that the jar file be 26 | pre-processed; check your provider documentation to see if that is 27 | required. 28 | 29 | java -javaagent:${ECLIPSELINK_JAR} -jar jars/StockCreateJPA.jar \ 30 | nThreads nStocks startDate endDate nTransactions reuseEM reuseTX 31 | 32 | nThreads -- Number of threads to use for parallel insert 33 | nStocks -- Number of stocks to insert 34 | startDate -- Starting date of range 35 | endDate -- Ending date of range 36 | nTransactions -- Number of stocks per transaction 37 | reuseEM -- If true, don't create a new EM for each transaction 38 | reuseTX -- If true, don't create a new transaction 39 | 40 | Notes on Examples: 41 | 42 | Example 11-2: 43 | java -javaagent:${ECLIPSELINK_JAR} -jar jars/StockCreateJPA.jar \ 44 | 1 128 1/1/13 12/31/13 128 true true 45 | 46 | To explore changes in statement caching and batch sizes, change the 47 | values in persistence.xml 48 | -------------------------------------------------------------------------------- /FirstEdition/StockCreateJPA/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 42 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-ejb/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-ejb/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.eclipse.persistence.jpa.PersistenceProvider 5 | StockDB 6 | lib/stockimpl.jar 7 | false 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-ejb/src/net/sdo/StockSessionBeanLocal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.util.Date; 8 | import javax.ejb.Local; 9 | import net.sdo.stock.StockPriceHistory; 10 | 11 | @Local 12 | public interface StockSessionBeanLocal { 13 | StockPriceHistory getHistory(String symbol, Date startDate, 14 | Date endDate, boolean doMock, int impl); 15 | } 16 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-ejb/src/net/sdo/StockSessionBeanRemote.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.util.Date; 8 | import javax.ejb.Remote; 9 | import net.sdo.stock.StockPriceHistory; 10 | 11 | @Remote 12 | public interface StockSessionBeanRemote { 13 | StockPriceHistory getHistory(String symbol, Date startDate, 14 | Date endDate, boolean doMock, int impl); 15 | } 16 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/meta/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | StockServlet 5 | net.sdo.StockServlet 6 | 7 | 8 | StockServlet 9 | /StockServlet 10 | 11 | 12 | 13 | 30 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/src/jsp/error.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="java.io.PrintWriter"%> 6 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 7 | 8 | 9 | 10 | 11 | 12 | Error 13 | 14 | 15 |

Error from StockPrice Servlet!

16 | The following error was returned from the StockPrice servlet 17 |
18 |             <% 
19 |                 PrintWriter pw = new PrintWriter(out);
20 |                 exception.printStackTrace(pw);
21 |             %>
22 |         
23 | 24 | 25 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/src/jsp/history.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 6 | 7 | 8 | 9 | 10 | 11 | Stock Price History 12 | 13 | 14 |

Stock Price History for <%=history.getSymbol()%>

15 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 16 |
17 |
High: <%=history.getHighPrice()%> 18 |
Low: <%=history.getLowPrice()%> 19 |
Average: <%=history.getAveragePrice()%> 20 |
Standard Deviation: <%=history.getStdDev()%> 21 | 22 | 23 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/src/jsp/longhistory.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="java.math.BigDecimal"%> 6 | <%@page import="java.util.Date"%> 7 | <%@page import="net.sdo.stock.StockPrice"%> 8 | <%@page import="java.util.Collection"%> 9 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 10 | 11 | 12 | 13 | 14 | 15 | Stock History Page 16 | 17 | 18 |

Stock Price History for <%=history.getSymbol()%>

19 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | <% 29 | Collection prices = history.getPrices( 30 | history.getFirstDate(), history.getLastDate()); 31 | for (StockPrice sp : prices) { 32 | Date date = sp.getDate(); 33 | BigDecimal highPrice = sp.getHigh(); 34 | BigDecimal lowPrice = sp.getLow(); 35 | BigDecimal closePrice = sp.getClosingPrice(); 36 | BigDecimal openPrice = sp.getOpeningPrice(); 37 | %> 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | <% 46 | } 47 | %> 48 | 49 | 50 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/StockEar-war/src/jsp/option.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="net.sdo.stock.StockOptionPrice"%> 6 | <%@page import="java.math.BigDecimal"%> 7 | <%@page import="java.util.Date"%> 8 | <%@page import="net.sdo.stock.StockPrice"%> 9 | <%@page import="java.util.Collection"%> 10 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 11 | 12 | 13 | 14 | 15 | 16 | Stock History Page 17 | 18 | 19 |

Stock Option Price History for <%=history.getSymbol()%>

20 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 21 |
DateOpenHighLowClose
<%=date%><%=openPrice%><%=highPrice%><%=lowPrice%><%=closePrice%>
22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | <% 30 | Collection prices = history.getPrices( 31 | history.getFirstDate(), history.getLastDate()); 32 | for (StockPrice sp : prices) { 33 | %> 34 | 35 | <% 36 | Collection optionPrices = sp.getOptions(); 37 | for (StockOptionPrice sop : optionPrices) { 38 | BigDecimal price = sop.getPrice(); 39 | %> 40 | 41 | <% 42 | } 43 | %> 44 | 45 | <% 46 | } 47 | %> 48 | 49 | 50 | -------------------------------------------------------------------------------- /FirstEdition/StockEar/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /FirstEdition/StockReadJPA/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /FirstEdition/StockRestDriver/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Building this requires the Faban driver kit downloaded from 3 | http://faban.org 4 | You must set the following environment variable: 5 | FABAN_DRIVER=/path_to/fabandriver.jar 6 | 7 | To run: 8 | Edit the run.xml file and change the 9 | outputDir -- Where you want the output saved 10 | HOST -- server where the service is deployed 11 | POST -- port where the service is deployed 12 | In the operationMix, set the test you want to execute to 1 13 | and set all other tests to 0 (or, alternately, set those values 14 | to execute a mix of operations) 15 | 16 | Set your clsaspath to incude fabancommon.jar, fabandriver.jar, and 17 | jars/driver.jar 18 | java -Dbenchmark.config=run.xml com.sun.faban.driver.engine.MasterImpl 19 | 20 | The output directory will have a set of numbered directories (starting at 21 | 1, increasing monotonically for each run). The summary.xml file in each 22 | directory will have the repsonse times and so on. 23 | 24 | Example 10-14 and 10-15: 25 | The various scenarios are chosen by setting the operationMix as described 26 | above. 27 | 28 | The caching in scenario 15 is enabled by setting the CACHE property 29 | to true in the run.xml file. 30 | -------------------------------------------------------------------------------- /FirstEdition/StockRestDriver/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /FirstEdition/StockRestDriver/run.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1 4 | 5 | 30 6 | 30 7 | 30 8 | 9 | /home/sdo/workload/benchmarks/stock_rest/output 10 | false 11 | 12 | 100 13 | false 14 | false 15 | 16 | 17 | 5 18 | 19 | 20 | 1 21 | 22 | 23 | 24 | HOST 25 | localhost 26 | 27 | 28 | PORT 29 | 8080 30 | 31 | 32 | CACHE 33 | false 34 | 35 | 36 | 37 | StockRest1Yr0 38 | 39 | 40 | StockRest1YrNoData0 41 | 42 | 43 | StockRest5Calls0 44 | 45 | 46 | StockRest2Calls0 47 | 48 | 49 | StockRest10Calls0 50 | 51 | 52 | StockRest2Yr0 53 | 54 | 55 | StockRest6Months0 56 | 57 | 58 | StockRest5Yr1 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /FirstEdition/StockRestService/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Building this requires a JavaEE application server that supplies 3 | servlet, JPA, and EJB interfaces. 4 | You must set the following environment variables: 5 | JAVAX_PERSISTENCE=/path_to/javax.persistence.jar 6 | JAVAX_RS=/path_to/javax.ws.rs-api.jar 7 | JSON_P=/path_to/javax.json.jar 8 | 9 | To deploy: 10 | Set up and configure a JavaEE application server. Even if you 11 | intend only to use the mock entity manager, the application server must 12 | define a JDBC data source with the name StockDB. 13 | 14 | Deploy to any JavaEE application server. For a simple deployment with 15 | glassfish, copy the jars/StockEar.ear to the glassfish domain's 16 | autodeploy directory. 17 | 18 | If you want to use the mock entity manager, configure the app server 19 | to use 20 | -DMockEntityManager=MockEntityManager 21 | as one of its JVM parameters (along with whatever parameters you are 22 | interested in testing) 23 | 24 | You must have a REST client to test this with. One simple way to 25 | do that is to use NetBean to generate a dummy Rest Client to access 26 | the service. 27 | 28 | For load testing, fhb cannot generate the required tests, but a faban 29 | driver (used for the examples) is found in StockRestDriver. Or utilize 30 | any load generator to generate rest calls to 31 | http://localhost:8080/StockPriceRestService/webresources/StockPrice?mode=1&startDate=01%2F01%2F2013&endDate=12%2F31%2F2017&cache=true&symbol= 32 | -------------------------------------------------------------------------------- /FirstEdition/StockRestService/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /FirstEdition/StockRestService/src/java/org/netbeans/rest/application/config/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package org.netbeans.rest.application.config; 6 | 7 | import java.util.Set; 8 | import javax.ws.rs.core.Application; 9 | 10 | @javax.ws.rs.ApplicationPath("webresources") 11 | public class ApplicationConfig extends Application { 12 | 13 | public Set> getClasses() { 14 | return getRestResourceClasses(); 15 | } 16 | 17 | /** 18 | * Do not modify this method. It is automatically generated by NetBeans REST support. 19 | */ 20 | private Set> getRestResourceClasses() { 21 | Set> resources = new java.util.HashSet>(); 22 | resources.add(net.sdo.rest.StockPriceResource.class); 23 | return resources; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /FirstEdition/StockSerialization/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Even if you are not using an entity manager, building this requires a 3 | JPA implementation (since the mock entity manager still uses JPA 4 | interfaces). Download an implementation (e.g., the reference implementation 5 | from GlassFish) and in your environment set 6 | 7 | JAVAX_PERSISTENCE=path_to/javax.persistence.jar 8 | 9 | Then run 10 | ant 11 | 12 | To run: 13 | java -DMockEntityManager=MockEntityManager \ 14 | -jar jars/StockSerialization.jar 15 | 16 | Notes on Examples: 17 | Example 10-11: 18 | The regular SPH is labeled eager transient in the output 19 | The transient histogram is labeled eagaer transient 20 | 21 | Example 10-12: 22 | No compresion is the regular SPH case 23 | Compression/Decompression is the serialize/deserialize case 24 | Compression only is the lazy compression case 25 | 26 | Example 12-1: 27 | Unbuffered compression/decompression output 28 | -------------------------------------------------------------------------------- /FirstEdition/StockSerialization/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /FirstEdition/StringIntern/README: -------------------------------------------------------------------------------- 1 | To build: 2 | This example uses a public domain version of the ConcurrentHashMap 3 | that provides for weak (and other types of) references. That source 4 | is included; see src/extra166y/CustomConcurrentHashMap.java for 5 | license details. 6 | 7 | To run: 8 | java -jar jars/stringintern.jar self|jdk 9 | jdk -- use the standard string intern call in the jdk (tested with 10 | various StringTableSize arguments) 11 | self -- use a custom cannonical string representation 12 | -------------------------------------------------------------------------------- /FirstEdition/StringIntern/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/UncontendedSync/README: -------------------------------------------------------------------------------- 1 | To build: 2 | ant 3 | 4 | To run: 5 | java -jar jars/uncontended.jar 6 | -------------------------------------------------------------------------------- /FirstEdition/UncontendedSync/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /FirstEdition/api/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /FirstEdition/api/src/net/sdo/stock/StockOptionPrice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | 10 | public interface StockOptionPrice { 11 | public String getSymbol(); 12 | public Date getDate(); 13 | public int getExpirationPeriod(); 14 | public BigDecimal getPrice(); 15 | } 16 | -------------------------------------------------------------------------------- /FirstEdition/api/src/net/sdo/stock/StockPrice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Collection; 9 | import java.util.Date; 10 | 11 | public interface StockPrice { 12 | String getSymbol(); 13 | Date getDate(); 14 | BigDecimal getClosingPrice(); 15 | BigDecimal getHigh(); 16 | BigDecimal getLow(); 17 | BigDecimal getOpeningPrice(); 18 | boolean isYearHigh(); 19 | boolean isYearLow(); 20 | Collection getOptions(); 21 | } 22 | -------------------------------------------------------------------------------- /FirstEdition/api/src/net/sdo/stock/StockPriceHistory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.ArrayList; 9 | import java.util.Collection; 10 | import java.util.Date; 11 | import java.util.Map; 12 | 13 | public interface StockPriceHistory { 14 | public static final int STANDARD = 0; 15 | public static final int LOGGING = 1; 16 | public static final int COMPRESS = 2; 17 | public static final int COMPRESS_LAZY = 3; 18 | public static final int COMPRESS_SKIP = 4; // NOT USED 19 | public static final int COMPRESS_EAGER = 5; 20 | public static final int COMPRESS_EAGER_TRANSIENT = 6; 21 | 22 | StockPrice getPrice(Date d); 23 | Collection getPrices(Date startDate, Date endDate); 24 | Map getAllEntries(); 25 | Map> getHistogram(); 26 | BigDecimal getAveragePrice(); 27 | Date getFirstDate(); 28 | BigDecimal getHighPrice(); 29 | Date getLastDate(); 30 | BigDecimal getLowPrice(); 31 | BigDecimal getStdDev(); 32 | String getSymbol(); 33 | } 34 | -------------------------------------------------------------------------------- /FirstEdition/api/src/net/sdo/stock/StockPriceUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.io.StringWriter; 8 | import java.math.BigInteger; 9 | import java.util.Date; 10 | import java.util.Map; 11 | import java.util.concurrent.ThreadLocalRandom; 12 | 13 | public class StockPriceUtils { 14 | public static final int STANDARD = 0; 15 | public static final int LOGGER = 1; 16 | public static final int HALF = 2; 17 | public static final int ALTERNATE = 3; 18 | 19 | public static final int SYMBOL_LENGTH = 4; 20 | public static final int NSYMBOLS = 26 * SYMBOL_LENGTH; 21 | 22 | public static String makeSymbol(int symbolNumber) { 23 | String s = new BigInteger("" + symbolNumber).toString(26); 24 | StringBuilder sb = new StringBuilder(); 25 | int len = SYMBOL_LENGTH - s.length(); 26 | while (len-- > 0) { 27 | sb.append('A'); 28 | } 29 | for (int i = 0; i < s.length(); i++) { 30 | char c = Character.toUpperCase(s.charAt(i)); 31 | if (c >= '0' & c <= '9') { 32 | c = (char) ('A' + c - '0'); 33 | } else { 34 | c = (char) (c + 10); 35 | } 36 | sb.append(c); 37 | } 38 | return sb.toString(); 39 | } 40 | 41 | public static String getRandomSymbol() { 42 | return makeSymbol(ThreadLocalRandom.current().nextInt(NSYMBOLS)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /FirstEdition/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /FirstEdition/impl/README: -------------------------------------------------------------------------------- 1 | These classes provide various implementations of the StockPrice API. 2 | 3 | To compile these classes, you must have a JPA implementation. In your 4 | environment, please set JAVAX_PERSISTENCE to the path to the jar file 5 | containing the JPA API classes, e.g. 6 | export JAVA_PERSISTENCE=/path_to/javax.persistence.jar 7 | 8 | For examples that require Eclipselink-specific functionality, set 9 | ECLIPSELINK_JAR to the path of the eclipselink implementation, e.g. 10 | export ECLIPSELINK_JAR=/path_to/eclipselink.jar 11 | 12 | There are several implementations of the stock price API, discussed mostly 13 | in Chapter 11, as the implementation differences affect JPA. The choice of 14 | those implementations is selected via -Dimpl value passed to the ant 15 | comandline: 16 | -Dimpl=LazyLazyImpl (both relationships are annotated lazy) 17 | -Dimpl=EagerLazyImpl (the OneToMany stock->options relationship is eager) 18 | -Dimpl=EagerEagerImpl (both relationships are eager) 19 | -Dimpl=LazyEagerImpl (the ManyToOne options->stock relationship is eager) 20 | For tests with the mock entity manager, use LazyLazyImpl, which is the 21 | default. 22 | -------------------------------------------------------------------------------- /FirstEdition/impl/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.util.Date; 8 | import javax.persistence.Embeddable; 9 | import javax.persistence.Temporal; 10 | import javax.persistence.TemporalType; 11 | 12 | @Embeddable 13 | public class StockOptionPK { 14 | private String symbol; 15 | @Temporal(TemporalType.DATE) 16 | private Date priceDate; 17 | private int expirationPeriod; 18 | 19 | public StockOptionPK() { 20 | } 21 | 22 | public StockOptionPK(String s, Date d, int i) { 23 | symbol = s; 24 | priceDate = d; 25 | expirationPeriod = i; 26 | } 27 | 28 | public String getSymbol() { 29 | return symbol; 30 | } 31 | 32 | public Date getDate() { 33 | return priceDate; 34 | } 35 | 36 | public int getExpirationPeriod() { 37 | return expirationPeriod; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPriceEagerEagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerEagerImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.EAGER) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerEagerImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPriceEagerLazyImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerLazyImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.LAZY) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerLazyImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPriceEagerLazyQueryCacheImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerLazyQueryCacheImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.LAZY) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerLazyQueryCacheImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPriceLazyEagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceLazyEagerImpl implements StockOptionPrice { 22 | 23 | @EmbeddedId 24 | private StockOptionPK id; 25 | @Column(precision = 30, scale = 2) 26 | private BigDecimal price; 27 | @ManyToOne(fetch = FetchType.EAGER) 28 | @JoinColumns({ 29 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 30 | insertable = false, updatable = false), 31 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 32 | insertable = false, updatable = false) 33 | }) 34 | private StockPriceLazyEagerImpl stock; 35 | 36 | @Override 37 | public String getSymbol() { 38 | return id.getSymbol(); 39 | } 40 | 41 | @Override 42 | public Date getDate() { 43 | return id.getDate(); 44 | } 45 | 46 | @Override 47 | public int getExpirationPeriod() { 48 | return id.getExpirationPeriod(); 49 | } 50 | 51 | @Override 52 | public BigDecimal getPrice() { 53 | return price; 54 | } 55 | 56 | public void setId(StockOptionPK id) { 57 | this.id = id; 58 | } 59 | 60 | public void setPrice(BigDecimal price) { 61 | this.price = price; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockOptionPriceLazyLazyImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name="StockOptionPrice") 21 | public class StockOptionPriceLazyLazyImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | 25 | @Column(precision = 30, scale = 2) 26 | private BigDecimal price; 27 | 28 | @ManyToOne(fetch=FetchType.LAZY) 29 | @JoinColumns( 30 | {@JoinColumn(name="symbol", referencedColumnName="SYMBOL", 31 | insertable=false, updatable=false), 32 | @JoinColumn(name="priceDate", referencedColumnName="priceDate", 33 | insertable=false, updatable=false) 34 | }) 35 | private StockPriceLazyLazyImpl stock; 36 | 37 | @Override 38 | public String getSymbol() { 39 | return id.getSymbol(); 40 | } 41 | 42 | @Override 43 | public Date getDate() { 44 | return id.getDate(); 45 | } 46 | 47 | @Override 48 | public int getExpirationPeriod() { 49 | return id.getExpirationPeriod(); 50 | } 51 | 52 | @Override 53 | public BigDecimal getPrice() { 54 | return price; 55 | } 56 | 57 | public void setId(StockOptionPK id) { 58 | this.id = id; 59 | } 60 | 61 | public void setPrice(BigDecimal price) { 62 | this.price = price; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /FirstEdition/impl/src/net/sdo/stockimpl/StockPricePK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | import javax.persistence.Embeddable; 10 | import javax.persistence.Temporal; 11 | import javax.persistence.TemporalType; 12 | 13 | @Embeddable 14 | public class StockPricePK implements Serializable { 15 | private String symbol; 16 | 17 | @Temporal(TemporalType.DATE) 18 | private Date priceDate; 19 | 20 | public StockPricePK() { 21 | } 22 | 23 | public StockPricePK(String symbol, Date date) { 24 | this.symbol = symbol; 25 | this.priceDate = date; 26 | } 27 | 28 | public Date getDate() { 29 | return priceDate; 30 | } 31 | 32 | public String getSymbol() { 33 | return symbol; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FirstEdition/setenv.csh: -------------------------------------------------------------------------------- 1 | setenv JAVAX_PERSISTENCE /export/appservers/glassfish4/glassfish/modules/javax.persistence.jar 2 | setenv JAVAX_EJB /export/appservers/glassfish4/glassfish/modules/javax.ejb-api.jar 3 | setenv JAVAX_SERVLET /export/appservers/glassfish4/glassfish/modules/javax.servlet-api.jar 4 | setenv JSON_P /export/appservers/glassfish4/glassfish/modules/javax.json.jar 5 | setenv JAVAX_RS /export/appservers/glassfish4/glassfish/modules/javax.ws.rs-api.jar 6 | 7 | setenv SIZEOF /home/sdo/NetBeansProjects/sizeof/SizeOf.jar 8 | 9 | setenv JACKSON /home/sdo/NetBeansProjects/jackson/jackson-core-2.1.4.jar 10 | 11 | setenv FABAN_DRIVER /home/sdo/workload/utilities/faban/lib/fabandriver.jar 12 | 13 | setenv ECLIPSELINK_JAR /home/sdo/netbeans-7.4/java/modules/ext/eclipselink/eclipselink.jar 14 | setenv JDBC_JAR /u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar 15 | 16 | setenv JAVA_HOME /home/sdo/VMs/jdk1.8.0-b123 17 | 18 | set path = ( $JAVA_HOME/bin $path ) 19 | -------------------------------------------------------------------------------- /SecondEdition/AOT/.attach_pid21621: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScottOaks/JavaPerformanceTuning/cd2861b6b6659c57bce18a71ba067a7208bae289/SecondEdition/AOT/.attach_pid21621 -------------------------------------------------------------------------------- /SecondEdition/AOT/.attach_pid21907: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScottOaks/JavaPerformanceTuning/cd2861b6b6659c57bce18a71ba067a7208bae289/SecondEdition/AOT/.attach_pid21907 -------------------------------------------------------------------------------- /SecondEdition/AOT/clean.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | rm -f *.so *.class listDir 4 | -------------------------------------------------------------------------------- /SecondEdition/AOT/listdir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScottOaks/JavaPerformanceTuning/cd2861b6b6659c57bce18a71ba067a7208bae289/SecondEdition/AOT/listdir -------------------------------------------------------------------------------- /SecondEdition/AOT/run-list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TIMEFORMAT=%R 4 | JAVA_HOME=$HOME/VMs/jdk-11.0.5 5 | GRAAL_HOME=$HOME/VMs/graalvm-ee-19.2.1 6 | 7 | run() { 8 | i=0 9 | t=0 10 | while [ $i -lt 10 ]; 11 | do 12 | t=$(( $t + $( (time $* >& /dev/null) |& awk '{ print $1 * 1000 }') )) 13 | i=$(( $i + 1 )) 14 | done 15 | echo $*: $(( $t / 10 )) 16 | } 17 | 18 | compile() { 19 | echo Compiling ListDir.class 20 | $GRAAL_HOME/bin/javac ListDir.java 21 | 22 | echo Producing native image 23 | $GRAAL_HOME/bin/native-image ListDir 24 | } 25 | 26 | if [ ! -f listdir ]; then 27 | compile 28 | fi 29 | run $JAVA_HOME/bin/java ListDir $1 30 | run ./listdir $1 31 | -------------------------------------------------------------------------------- /SecondEdition/EpsilonGC/net/sdo/EpsilonTest.java: -------------------------------------------------------------------------------- 1 | package net.sdo; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class EpsilonTest { 6 | 7 | private static final ArrayList al = new ArrayList(); 8 | 9 | public static void main(String[] args) { 10 | System.out.println("Start"); 11 | for (int i = 0; i < 4096; i++) { 12 | al.add(new byte[1024*512]); 13 | } 14 | System.out.println("End"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /SecondEdition/JNI/jni-benchmark/Makefile: -------------------------------------------------------------------------------- 1 | default: lib/libRandomTestCLibrary.so 2 | 3 | src/main/h/net_sdo_JNIBenchmark.h: src/main/java/net/sdo/JNIBenchmark.java 4 | javah -classpath target/benchmarks.jar -d src/main/h net.sdo.JNIBenchmark 5 | 6 | bin/calc.o : src/main/c/calc.c src/main/h/net_sdo_JNIBenchmark.h 7 | -@mkdir bin 8 | gcc -c -O2 -Isrc/main/h -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -fPIC -o bin/calc.o src/main/c/calc.c 9 | 10 | lib/libRandomTestCLibrary.so : bin/calc.o 11 | -@mkdir lib 12 | ld -shared -o lib/libRandomTestCLibrary.so -fPIC bin/calc.o 13 | 14 | clean: 15 | -rm lib/* 16 | -rm bin/* 17 | -------------------------------------------------------------------------------- /SecondEdition/JNI/jni-benchmark/README: -------------------------------------------------------------------------------- 1 | 2 | mvn package 3 | make 4 | java (args) -jar target/benchmarks.jar (jmh args) --jvmArgs "-DLIBPATH=`pwd`/lib" 5 | -------------------------------------------------------------------------------- /SecondEdition/JNI/jni-benchmark/src/main/c/calc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | long calcInC(int nValues) { 9 | long d = 0; 10 | int j; 11 | for (j = 0; j < nValues; j++) { 12 | d += j; 13 | } 14 | long average = d / nValues; 15 | return average; 16 | } 17 | 18 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcCC 19 | (JNIEnv *env, jclass obj, jint nValues) { 20 | return calcInC(nValues); 21 | } 22 | 23 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcC 24 | (JNIEnv *env, jobject this, jint i) { 25 | return i * 3 + 15; 26 | } 27 | 28 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcCCC 29 | (JNIEnv *env, jclass obj, jint nTrials, jint nValues) { 30 | long l = 0; 31 | for (int i = 0; i < nTrials; i++) { 32 | long a = calcInC(nValues); 33 | l += 50 - a; 34 | } 35 | return l; 36 | } 37 | -------------------------------------------------------------------------------- /SecondEdition/JNI/jni-benchmark/src/main/h/net_sdo_JNIBenchmark.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class net_sdo_JNIBenchmark */ 4 | 5 | #ifndef _Included_net_sdo_JNIBenchmark 6 | #define _Included_net_sdo_JNIBenchmark 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: net_sdo_JNIBenchmark 12 | * Method: calcC 13 | * Signature: (I)J 14 | */ 15 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcC 16 | (JNIEnv *, jobject, jint); 17 | 18 | /* 19 | * Class: net_sdo_JNIBenchmark 20 | * Method: calcCC 21 | * Signature: (I)J 22 | */ 23 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcCC 24 | (JNIEnv *, jobject, jint); 25 | 26 | /* 27 | * Class: net_sdo_JNIBenchmark 28 | * Method: calcCCC 29 | * Signature: (II)J 30 | */ 31 | JNIEXPORT jlong JNICALL Java_net_sdo_JNIBenchmark_calcCCC 32 | (JNIEnv *, jobject, jint, jint); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | #endif 38 | -------------------------------------------------------------------------------- /SecondEdition/ObjectSerialization/serial-benchmark/.out.1.1.8.0_202.single-int.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ScottOaks/JavaPerformanceTuning/cd2861b6b6659c57bce18a71ba067a7208bae289/SecondEdition/ObjectSerialization/serial-benchmark/.out.1.1.8.0_202.single-int.swp -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/README: -------------------------------------------------------------------------------- 1 | 2 | Need to manaully copy src/main/resources/schema.xsd to /tmp for the 3 | (non-caching) SAX validation to happen. 4 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/AbstractParsingTest.java: -------------------------------------------------------------------------------- 1 | package net.sdo; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.InputStream; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | 9 | import org.openjdk.jmh.infra.Blackhole; 10 | 11 | public abstract class AbstractParsingTest { 12 | protected static String ID = "itemId"; 13 | protected ByteArrayInputStream inputStream; 14 | protected ByteArrayInputStream schemaInputStream; 15 | 16 | private int targetIdCount; 17 | private ArrayList ids; 18 | private boolean isDOM; 19 | 20 | protected boolean addId(String s) { 21 | ids.add(s); 22 | if (targetIdCount > 0 && ids.size() == targetIdCount) { 23 | return true; 24 | } 25 | return false; 26 | } 27 | 28 | protected abstract void testEngine(Blackhole bh) throws IOException; 29 | public void test(Blackhole bh) throws IOException { 30 | inputStream.reset(); 31 | ids = new ArrayList<>(); 32 | testEngine(bh); 33 | if (!isDOM) { 34 | if (System.getProperty("debugParse") != null) { 35 | System.out.println(ids); 36 | } 37 | if (targetIdCount > 0 && targetIdCount != ids.size()) { 38 | throw new IOException("Unexpected count in document, found " + ids.size()); 39 | } 40 | } 41 | } 42 | 43 | private ByteArrayInputStream readInputStream(String name) throws IOException { 44 | InputStream is = getClass().getClassLoader().getResourceAsStream(name); 45 | ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available()); 46 | byte[] buf = new byte[is.available()]; 47 | int n = is.read(buf); 48 | while (n > 0) { 49 | baos.write(buf, 0, n); 50 | n = is.read(buf); 51 | } 52 | byte[] data = baos.toByteArray(); 53 | is.close(); 54 | baos.close(); 55 | return new ByteArrayInputStream(data); 56 | } 57 | 58 | public void setup(String type, boolean isDOM, ParsingTest.Parameters p) throws IOException { 59 | this.isDOM = isDOM; 60 | targetIdCount = p.targetIdCount; 61 | 62 | inputStream = readInputStream(type + "/inputFile." + type); 63 | schemaInputStream = readInputStream("xsd/schema.xsd"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/DOMBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | 9 | import javax.xml.parsers.DocumentBuilder; 10 | import javax.xml.parsers.DocumentBuilderFactory; 11 | import javax.xml.parsers.ParserConfigurationException; 12 | 13 | import org.openjdk.jmh.annotations.Scope; 14 | import org.openjdk.jmh.annotations.Setup; 15 | import org.openjdk.jmh.annotations.State; 16 | import org.openjdk.jmh.infra.Blackhole; 17 | 18 | import org.w3c.dom.Document; 19 | import org.xml.sax.SAXException; 20 | 21 | @State(Scope.Benchmark) 22 | public class DOMBuilderTest extends AbstractParsingTest { 23 | protected DocumentBuilderFactory dbf; 24 | protected DocumentBuilder db; 25 | 26 | @Setup 27 | public void setup(ParsingTest.Parameters p) throws IOException { 28 | try { 29 | super.setup("xml", true, p); 30 | dbf = DocumentBuilderFactory.newInstance(); 31 | dbf.setNamespaceAware(true); 32 | db = dbf.newDocumentBuilder(); 33 | db.setErrorHandler(new SAXErrorHandler()); 34 | } catch (ParserConfigurationException pce) { 35 | throw new IOException("Can't setup DOMBuilder", pce); 36 | } 37 | } 38 | 39 | @Override 40 | public void testEngine(Blackhole bh) throws IOException { 41 | try { 42 | db.reset(); 43 | Document doc = db.parse(inputStream); 44 | bh.consume(doc); 45 | } catch (SAXException ex) { 46 | throw new IOException("Can't Parse" , ex); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JAXBParsingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | import javax.xml.bind.JAXBContext; 11 | import javax.xml.bind.JAXBException; 12 | import javax.xml.bind.Unmarshaller; 13 | 14 | import org.openjdk.jmh.annotations.Scope; 15 | import org.openjdk.jmh.annotations.Setup; 16 | import org.openjdk.jmh.annotations.State; 17 | import org.openjdk.jmh.infra.Blackhole; 18 | 19 | @State(Scope.Benchmark) 20 | public class JAXBParsingTest extends AbstractParsingTest { 21 | private JAXBContext jc; 22 | private Unmarshaller u; 23 | 24 | @Setup 25 | public void setup(ParsingTest.Parameters p) throws IOException { 26 | super.setup("xml", true, p); 27 | try { 28 | jc = JAXBContext.newInstance("net.sdo.jaxb"); 29 | u = jc.createUnmarshaller(); 30 | } catch (JAXBException ex) { 31 | throw new IOException("Can't create JAXB context", ex); 32 | } 33 | } 34 | 35 | @Override 36 | protected void testEngine(Blackhole bh) throws IOException { 37 | try { 38 | bh.consume(u.unmarshal(inputStream)); 39 | } catch (JAXBException ex) { 40 | throw new IOException("Can't create JAXB object", ex); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JAXBSerializingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.io.ByteArrayOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | import javax.xml.bind.JAXBContext; 13 | import javax.xml.bind.JAXBException; 14 | import javax.xml.bind.Marshaller; 15 | import javax.xml.bind.Unmarshaller; 16 | 17 | import org.openjdk.jmh.annotations.Scope; 18 | import org.openjdk.jmh.annotations.Setup; 19 | import org.openjdk.jmh.annotations.State; 20 | import org.openjdk.jmh.infra.Blackhole; 21 | 22 | @State(Scope.Benchmark) 23 | public class JAXBSerializingTest extends AbstractParsingTest { 24 | private JAXBContext jc; 25 | private Object responseIds; 26 | private ByteArrayOutputStream os; 27 | 28 | @Setup 29 | public void setup(ParsingTest.Parameters p) throws IOException { 30 | super.setup("xml", true, p); 31 | try { 32 | jc = JAXBContext.newInstance("net.sdo.jaxb"); 33 | Unmarshaller u = jc.createUnmarshaller(); 34 | responseIds = u.unmarshal(inputStream); 35 | os = new ByteArrayOutputStream(); 36 | } catch (JAXBException ex) { 37 | throw new IOException("Can't create JAXB context", ex); 38 | } 39 | } 40 | 41 | @Override 42 | protected void testEngine(Blackhole bh) throws IOException { 43 | os.reset(); 44 | try { 45 | Marshaller m = jc.createMarshaller(); 46 | m.marshal(responseIds,os); 47 | bh.consume(os); 48 | } catch (JAXBException ex) { 49 | throw new IOException("Can't create JAXB object", ex); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JSONB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | import javax.json.bind.Jsonb; 13 | import javax.json.bind.JsonbBuilder; 14 | 15 | import net.sdo.jsonb.FindItemsByKeywordsResponse; 16 | 17 | import org.openjdk.jmh.annotations.Scope; 18 | import org.openjdk.jmh.annotations.Setup; 19 | import org.openjdk.jmh.annotations.State; 20 | import org.openjdk.jmh.infra.Blackhole; 21 | 22 | @State(Scope.Benchmark) 23 | public class JSONB extends AbstractParsingTest { 24 | private InputStream in; 25 | private Jsonb jsonb; 26 | 27 | @Setup 28 | public void setup(ParsingTest.Parameters p) throws IOException { 29 | super.setup("json", true, p); 30 | jsonb = JsonbBuilder.create(); 31 | } 32 | 33 | @Override 34 | protected void testEngine(Blackhole bh) throws IOException { 35 | FindItemsByKeywordsResponse f = 36 | jsonb.fromJson(inputStream, FindItemsByKeywordsResponse.class); 37 | bh.consume(f); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JSONBJackson.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | import net.sdo.jsonb.FindItemsByKeywordsResponse; 13 | 14 | import org.openjdk.jmh.annotations.Scope; 15 | import org.openjdk.jmh.annotations.Setup; 16 | import org.openjdk.jmh.annotations.State; 17 | import org.openjdk.jmh.infra.Blackhole; 18 | 19 | @State(Scope.Benchmark) 20 | public class JSONBJackson extends AbstractParsingTest { 21 | private ObjectMapper mapper; 22 | 23 | @Setup 24 | public void setup(ParsingTest.Parameters p) throws IOException { 25 | super.setup("json", true, p); 26 | mapper = new ObjectMapper(); 27 | } 28 | 29 | @Override 30 | protected void testEngine(Blackhole bh) throws IOException { 31 | FindItemsByKeywordsResponse f = 32 | mapper.readValue(inputStream, FindItemsByKeywordsResponse.class); 33 | bh.consume(f); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JSONBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | import javax.json.Json; 11 | import javax.json.JsonReader; 12 | 13 | import org.openjdk.jmh.annotations.Scope; 14 | import org.openjdk.jmh.annotations.Setup; 15 | import org.openjdk.jmh.annotations.State; 16 | import org.openjdk.jmh.infra.Blackhole; 17 | 18 | @State(Scope.Benchmark) 19 | public class JSONBuilderTest extends AbstractParsingTest { 20 | JsonReader jr; 21 | 22 | @Setup 23 | public void setup(ParsingTest.Parameters p) throws IOException { 24 | super.setup("json", true, p); 25 | } 26 | 27 | @Override 28 | protected void testEngine(Blackhole bh) throws IOException { 29 | jr = Json.createReader(inputStream); 30 | bh.consume(jr.read()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JSONParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | import javax.json.Json; 11 | import javax.json.stream.JsonParser; 12 | import javax.json.stream.JsonParser.Event; 13 | import javax.json.stream.JsonParserFactory; 14 | 15 | import org.openjdk.jmh.annotations.Scope; 16 | import org.openjdk.jmh.annotations.Setup; 17 | import org.openjdk.jmh.annotations.State; 18 | import org.openjdk.jmh.infra.Blackhole; 19 | 20 | @State(Scope.Benchmark) 21 | public class JSONParserTest extends AbstractParsingTest { 22 | private JsonParserFactory factory; 23 | private JsonParser parser; 24 | 25 | @Setup 26 | public void setup(ParsingTest.Parameters p) throws IOException { 27 | super.setup("json", false, p); 28 | factory = Json.createParserFactory(null); 29 | } 30 | 31 | @Override 32 | protected void testEngine(Blackhole bh) throws IOException { 33 | boolean isID = false; 34 | 35 | parser = factory.createParser(inputStream); 36 | int idCount = 0; 37 | while (parser.hasNext()) { 38 | Event event = parser.next(); 39 | switch (event) { 40 | case KEY_NAME: 41 | String s = parser.getString(); 42 | if (ID.equals(s)) { 43 | isID = true; 44 | } 45 | break; 46 | case VALUE_STRING: 47 | if (isID) { 48 | if (addId(parser.getString())) { 49 | return; 50 | } 51 | isID = false; 52 | } 53 | continue; 54 | default: 55 | continue; 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/JsonJacksonParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.fasterxml.jackson.core.JsonFactory; 8 | import com.fasterxml.jackson.core.JsonParser; 9 | import com.fasterxml.jackson.core.JsonToken; 10 | 11 | import java.io.IOException; 12 | 13 | import org.openjdk.jmh.annotations.Scope; 14 | import org.openjdk.jmh.annotations.Setup; 15 | import org.openjdk.jmh.annotations.State; 16 | import org.openjdk.jmh.infra.Blackhole; 17 | 18 | @State(Scope.Benchmark) 19 | public class JsonJacksonParserTest extends AbstractParsingTest { 20 | private JsonFactory jsonFactory; 21 | private JsonParser parser; 22 | 23 | @Setup 24 | public void setup(ParsingTest.Parameters p) throws IOException { 25 | super.setup("json", false, p); 26 | jsonFactory = new JsonFactory(); 27 | } 28 | 29 | @Override 30 | protected void testEngine(Blackhole bh) throws IOException { 31 | parser = jsonFactory.createParser(inputStream); 32 | int idCount = 0; 33 | boolean isID = false; 34 | for (JsonToken token = parser.nextToken(); token != null; token = parser.nextToken()) { 35 | switch (token) { 36 | case FIELD_NAME: 37 | String s = parser.getCurrentName(); 38 | if (ID.equals(s)) { 39 | isID = true; 40 | } 41 | break; 42 | case VALUE_STRING: 43 | if (isID) { 44 | String s2 = parser.getText(); 45 | if (addId(s2)) { 46 | return; 47 | } 48 | isID = false; 49 | } 50 | break; 51 | default: 52 | break; 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/SAXErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import org.xml.sax.ErrorHandler; 8 | import org.xml.sax.SAXParseException; 9 | 10 | public class SAXErrorHandler implements ErrorHandler { 11 | 12 | public void warning(SAXParseException spe) throws SAXParseException { 13 | System.out.println("SAX Warning" + spe); 14 | throw spe; 15 | } 16 | 17 | public void error(SAXParseException spe) throws SAXParseException { 18 | System.out.println("SAX error" + spe); 19 | throw spe; 20 | } 21 | 22 | public void fatalError(SAXParseException spe) throws SAXParseException { 23 | System.out.println("SAX fatalError" + spe); 24 | throw spe; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/SAXNoReuseParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.sun.org.apache.xerces.internal.jaxp.JAXPConstants; 8 | 9 | import java.io.IOException; 10 | 11 | import javax.xml.XMLConstants; 12 | import javax.xml.parsers.ParserConfigurationException; 13 | import javax.xml.parsers.SAXParser; 14 | import javax.xml.parsers.SAXParserFactory; 15 | 16 | import org.openjdk.jmh.annotations.Scope; 17 | import org.openjdk.jmh.annotations.Setup; 18 | import org.openjdk.jmh.annotations.State; 19 | import org.openjdk.jmh.infra.Blackhole; 20 | 21 | import org.xml.sax.SAXException; 22 | import org.xml.sax.XMLReader; 23 | 24 | import org.xml.sax.SAXException; 25 | import org.xml.sax.XMLReader; 26 | 27 | @State(Scope.Benchmark) 28 | public class SAXNoReuseParserTest extends AbstractSAXParserTest { 29 | private SAXParserFactory spf; 30 | 31 | @Setup 32 | public void setup(ParsingTest.Parameters p) throws IOException { 33 | super.setup(p); 34 | try { 35 | spf = SAXParserFactory.newInstance(); 36 | } catch (Exception ex) { 37 | throw new IOException("Can't initialize parser", ex); 38 | } 39 | } 40 | 41 | @Override 42 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 43 | SAXParser parser = spf.newSAXParser(); 44 | parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 45 | XMLReader xr = parser.getXMLReader(); 46 | xr.setErrorHandler(new SAXErrorHandler()); 47 | return xr; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/SAXParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.sun.org.apache.xerces.internal.jaxp.JAXPConstants; 8 | 9 | import java.io.IOException; 10 | 11 | import javax.xml.XMLConstants; 12 | import javax.xml.parsers.ParserConfigurationException; 13 | import javax.xml.parsers.SAXParser; 14 | import javax.xml.parsers.SAXParserFactory; 15 | 16 | import org.openjdk.jmh.annotations.Scope; 17 | import org.openjdk.jmh.annotations.Setup; 18 | import org.openjdk.jmh.annotations.State; 19 | import org.openjdk.jmh.infra.Blackhole; 20 | 21 | import org.xml.sax.SAXException; 22 | import org.xml.sax.XMLReader; 23 | 24 | import org.xml.sax.SAXException; 25 | import org.xml.sax.XMLReader; 26 | 27 | @State(Scope.Benchmark) 28 | public class SAXParserTest extends AbstractSAXParserTest { 29 | private SAXParser parser; 30 | 31 | @Setup 32 | public void setup(ParsingTest.Parameters p) throws IOException { 33 | super.setup(p); 34 | try { 35 | SAXParserFactory spf = SAXParserFactory.newInstance(); 36 | parser = spf.newSAXParser(); 37 | } catch (Exception ex) { 38 | throw new IOException("Can't initialize parser", ex); 39 | } 40 | } 41 | 42 | @Override 43 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 44 | parser.reset(); 45 | parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 46 | XMLReader xr = parser.getXMLReader(); 47 | xr.setErrorHandler(new SAXErrorHandler()); 48 | return xr; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/SAXValidatingCachingParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.io.IOException; 8 | 9 | import javax.xml.XMLConstants; 10 | import javax.xml.parsers.ParserConfigurationException; 11 | import javax.xml.parsers.SAXParser; 12 | import javax.xml.parsers.SAXParserFactory; 13 | import javax.xml.transform.Source; 14 | import javax.xml.transform.stream.StreamSource; 15 | import javax.xml.validation.Schema; 16 | import javax.xml.validation.SchemaFactory; 17 | 18 | import org.openjdk.jmh.annotations.Scope; 19 | import org.openjdk.jmh.annotations.Setup; 20 | import org.openjdk.jmh.annotations.State; 21 | import org.openjdk.jmh.infra.Blackhole; 22 | 23 | import org.xml.sax.SAXException; 24 | import org.xml.sax.XMLReader; 25 | 26 | @State(Scope.Benchmark) 27 | public class SAXValidatingCachingParserTest extends AbstractSAXParserTest { 28 | private SAXParser parser; 29 | 30 | @Setup 31 | public void setup(ParsingTest.Parameters p) throws IOException { 32 | super.setup(p); 33 | try { 34 | SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 35 | StreamSource ss = new StreamSource(schemaInputStream); 36 | Schema schema = sf.newSchema(new Source[]{ss}); 37 | SAXParserFactory spf = SAXParserFactory.newInstance(); 38 | spf.setValidating(false); 39 | spf.setNamespaceAware(true); 40 | spf.setSchema(schema); 41 | parser = spf.newSAXParser(); 42 | } catch (Exception ex) { 43 | throw new IOException("Can't initialize parser", ex); 44 | } 45 | } 46 | 47 | @Override 48 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 49 | parser.reset(); 50 | XMLReader xr = parser.getXMLReader(); 51 | return xr; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /SecondEdition/ParsingTest/parsing-benchmark/src/main/java/net/sdo/SAXValidatingParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import com.sun.org.apache.xerces.internal.jaxp.JAXPConstants; 8 | 9 | import java.io.IOException; 10 | 11 | import javax.xml.XMLConstants; 12 | import javax.xml.parsers.ParserConfigurationException; 13 | import javax.xml.parsers.SAXParser; 14 | import javax.xml.parsers.SAXParserFactory; 15 | 16 | import org.openjdk.jmh.annotations.Scope; 17 | import org.openjdk.jmh.annotations.Setup; 18 | import org.openjdk.jmh.annotations.State; 19 | import org.openjdk.jmh.infra.Blackhole; 20 | 21 | import org.xml.sax.SAXException; 22 | import org.xml.sax.XMLReader; 23 | 24 | import org.xml.sax.SAXException; 25 | import org.xml.sax.XMLReader; 26 | 27 | @State(Scope.Benchmark) 28 | public class SAXValidatingParserTest extends AbstractSAXParserTest { 29 | private SAXParser parser; 30 | 31 | @Setup 32 | public void setup(ParsingTest.Parameters p) throws IOException { 33 | super.setup(p); 34 | try { 35 | SAXParserFactory spf = SAXParserFactory.newInstance(); 36 | spf.setValidating(true); 37 | spf.setNamespaceAware(true); 38 | parser = spf.newSAXParser(); 39 | } catch (Exception ex) { 40 | throw new IOException("Can't initialize parser", ex); 41 | } 42 | } 43 | 44 | @Override 45 | protected XMLReader getReader() throws ParserConfigurationException, SAXException { 46 | parser.reset(); 47 | parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 48 | XMLReader xr = parser.getXMLReader(); 49 | xr.setErrorHandler(new SAXErrorHandler()); 50 | return xr; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/.dockerignore: -------------------------------------------------------------------------------- 1 | target/* -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # 1st stage, build the app 18 | FROM maven:3.5.4-jdk-9 as build 19 | 20 | WORKDIR /helidon 21 | 22 | # Create a first layer to cache the "Maven World" in the local repository. 23 | # Incremental docker builds will always resume after that, unless you update 24 | # the pom 25 | ADD pom.xml . 26 | RUN mvn package -DskipTests 27 | 28 | # Do the Maven build! 29 | # Incremental docker builds will resume here when you change sources 30 | ADD src src 31 | RUN mvn package -DskipTests 32 | RUN echo "done!" 33 | 34 | # 2nd stage, build the runtime image 35 | FROM openjdk:8-jre-slim 36 | WORKDIR /helidon 37 | 38 | # Copy the binary built in the 1st stage 39 | COPY --from=build /helidon/target/stockrest.jar ./ 40 | COPY --from=build /helidon/target/libs ./libs 41 | 42 | CMD ["java", "-jar", "stockrest.jar"] 43 | 44 | EXPOSE 8080 45 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Helidon Quickstart MP Example 3 | 4 | This example implements a simple Hello World REST service using MicroProfile 5 | 6 | ## Prerequisites 7 | 8 | 1. Maven 3.5 or newer 9 | 2. Java SE 8 or newer 10 | 3. Docker 17 or newer (if you want to build and run docker images) 11 | 4. Kubernetes minikube v0.24 or newer (if you want to deploy to Kubernetes) 12 | or access to a Kubernetes 1.7.4 or newer cluster 13 | 5. Kubectl 1.7.4 or newer for deploying to Kubernetes 14 | 15 | Verify prerequisites 16 | ``` 17 | java -version 18 | mvn --version 19 | docker --version 20 | minikube version 21 | kubectl version --short 22 | ``` 23 | 24 | ## Build 25 | 26 | ``` 27 | mvn package 28 | ``` 29 | 30 | ## Start the application 31 | 32 | ``` 33 | java -jar target/stockrest.jar 34 | ``` 35 | 36 | ## Exercise the application 37 | 38 | ``` 39 | curl -X GET http://localhost:8080/greet 40 | {"message":"Hello World!"} 41 | 42 | curl -X GET http://localhost:8080/greet/Joe 43 | {"message":"Hello Joe!"} 44 | 45 | curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Hola"}' http://localhost:8080/greet/greeting 46 | 47 | curl -X GET http://localhost:8080/greet/Jose 48 | {"message":"Hola Jose!"} 49 | ``` 50 | 51 | ## Try health and metrics 52 | 53 | ``` 54 | curl -s -X GET http://localhost:8080/health 55 | {"outcome":"UP",... 56 | . . . 57 | 58 | # Prometheus Format 59 | curl -s -X GET http://localhost:8080/metrics 60 | # TYPE base:gc_g1_young_generation_count gauge 61 | . . . 62 | 63 | # JSON Format 64 | curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics 65 | {"base":... 66 | . . . 67 | 68 | ``` 69 | 70 | ## Build the Docker Image 71 | 72 | ``` 73 | docker build -t stockrest . 74 | ``` 75 | 76 | ## Start the application with Docker 77 | 78 | ``` 79 | docker run --rm -p 8080:8080 stockrest:latest 80 | ``` 81 | 82 | Exercise the application as described above 83 | 84 | ## Deploy the application to Kubernetes 85 | 86 | ``` 87 | kubectl cluster-info # Verify which cluster 88 | kubectl get pods # Verify connectivity to cluster 89 | kubectl create -f app.yaml # Deploy application 90 | kubectl get service stockrest # Verify deployed service 91 | ``` 92 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/app.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | kind: Service 18 | apiVersion: v1 19 | metadata: 20 | name: stockrest 21 | labels: 22 | app: stockrest 23 | spec: 24 | type: NodePort 25 | selector: 26 | app: stockrest 27 | ports: 28 | - port: 8080 29 | targetPort: 8080 30 | name: http 31 | --- 32 | kind: Deployment 33 | apiVersion: extensions/v1beta1 34 | metadata: 35 | name: stockrest 36 | spec: 37 | replicas: 1 38 | template: 39 | metadata: 40 | labels: 41 | app: stockrest 42 | version: v1 43 | spec: 44 | containers: 45 | - name: stockrest 46 | image: stockrest 47 | imagePullPolicy: IfNotPresent 48 | ports: 49 | - containerPort: 8080 50 | --- 51 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/java/net/sdo/stockrest/Compress.java: -------------------------------------------------------------------------------- 1 | package net.sdo.stockrest; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | import javax.ws.rs.NameBinding; 9 | 10 | @Target({ ElementType.TYPE, ElementType.METHOD }) 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @NameBinding 13 | public @interface Compress { } 14 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/java/net/sdo/stockrest/GZIPWriterInterceptor.java: -------------------------------------------------------------------------------- 1 | package net.sdo.stockrest; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.util.zip.GZIPOutputStream; 6 | 7 | import javax.ws.rs.core.Context; 8 | import javax.ws.rs.core.HttpHeaders; 9 | import javax.ws.rs.WebApplicationException; 10 | import javax.ws.rs.ext.WriterInterceptor; 11 | import javax.ws.rs.ext.WriterInterceptorContext; 12 | 13 | @Compress 14 | public class GZIPWriterInterceptor implements WriterInterceptor { 15 | @Context 16 | private HttpHeaders requestHeaders; 17 | 18 | @Override 19 | public void aroundWriteTo(WriterInterceptorContext context) 20 | throws IOException, WebApplicationException { 21 | String header = requestHeaders.getHeaderString("Accept-Encoding"); 22 | if (header == null || header.indexOf("gzip") < 0) { 23 | context.proceed(); 24 | return; 25 | } 26 | context.getHeaders().putSingle("Content-Encoding", "gzip"); 27 | OutputStream outputStream = context.getOutputStream(); 28 | context.setOutputStream(new GZIPOutputStream(outputStream)); 29 | context.proceed(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/java/net/sdo/stockrest/StockApplication.java: -------------------------------------------------------------------------------- 1 | package net.sdo.stockrest; 2 | 3 | import java.util.Set; 4 | 5 | import javax.enterprise.context.ApplicationScoped; 6 | import javax.ws.rs.ApplicationPath; 7 | import javax.ws.rs.core.Application; 8 | 9 | import io.helidon.common.CollectionsHelper; 10 | 11 | @ApplicationScoped 12 | @ApplicationPath("/") 13 | public class StockApplication extends Application { 14 | 15 | @Override 16 | public Set> getClasses() { 17 | return CollectionsHelper.setOf(StockResource.class, 18 | GZIPWriterInterceptor.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/java/net/sdo/stockrest/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Quickstart MicroProfile example. 19 | */ 20 | package net.sdo.stockrest; 21 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 26 | 27 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/resources/META-INF/microprofile-config.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Application properties. This is the default greeting 18 | app.greeting=Hello 19 | 20 | # Microprofile server properties 21 | server.port=8080 22 | server.host=0.0.0.0 23 | -------------------------------------------------------------------------------- /SecondEdition/Rest/stockrest/src/main/resources/logging.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Example Logging Configuration File 18 | # For more information see $JAVA_HOME/jre/lib/logging.properties 19 | 20 | # Send messages to the console 21 | handlers=java.util.logging.ConsoleHandler 22 | 23 | # Global default logging level. Can be overriden by specific handlers and loggers 24 | .level=INFO 25 | 26 | # Helidon Web Server has a custom log formatter that extends SimpleFormatter. 27 | # It replaces "!thread!" with the current thread name 28 | java.util.logging.ConsoleHandler.level=INFO 29 | java.util.logging.ConsoleHandler.formatter=io.helidon.webserver.WebServerLogFormatter 30 | java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n 31 | 32 | #Component specific log levels 33 | #io.helidon.webserver.level=INFO 34 | #io.helidon.config.level=INFO 35 | #io.helidon.security.level=INFO 36 | #io.helidon.microprofile.level=INFO 37 | #io.helidon.common.level=INFO 38 | #io.netty.level=INFO 39 | #org.glassfish.jersey.level=INFO 40 | #org.jboss.weld=INFO 41 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/README: -------------------------------------------------------------------------------- 1 | This is the API (interface + helper) for various examples based on the stock 2 | price simulations. 3 | 4 | mvn package 5 | will build the stock-api.jar 6 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | net.sdo.stock 4 | stock-api 5 | 1.0-SNAPSHOT 6 | 7 | UTF-8 8 | stock-api 9 | 1.8 10 | 1.8 11 | 12 | 13 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/src/main/java/net/sdo/stock/StockOptionPrice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | 10 | public interface StockOptionPrice { 11 | public String getSymbol(); 12 | public Date getDate(); 13 | public int getExpirationPeriod(); 14 | public BigDecimal getPrice(); 15 | } 16 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/src/main/java/net/sdo/stock/StockPrice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Collection; 9 | import java.util.Date; 10 | 11 | public interface StockPrice { 12 | String getSymbol(); 13 | Date getDate(); 14 | BigDecimal getClosingPrice(); 15 | BigDecimal getHigh(); 16 | BigDecimal getLow(); 17 | BigDecimal getOpeningPrice(); 18 | boolean isYearHigh(); 19 | boolean isYearLow(); 20 | Collection getOptions(); 21 | } 22 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/src/main/java/net/sdo/stock/StockPriceHistory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.ArrayList; 9 | import java.util.Collection; 10 | import java.util.Date; 11 | import java.util.Map; 12 | 13 | public interface StockPriceHistory { 14 | public static final int STANDARD = 0; 15 | public static final int LOGGING = 1; 16 | public static final int COMPRESS = 2; 17 | public static final int COMPRESS_LAZY = 3; 18 | public static final int COMPRESS_SKIP = 4; // NOT USED 19 | public static final int COMPRESS_EAGER = 5; 20 | public static final int COMPRESS_EAGER_TRANSIENT = 6; 21 | 22 | StockPrice getPrice(Date d); 23 | Collection getPrices(Date startDate, Date endDate); 24 | Map getAllEntries(); 25 | Map> getHistogram(); 26 | BigDecimal getAveragePrice(); 27 | Date getFirstDate(); 28 | BigDecimal getHighPrice(); 29 | Date getLastDate(); 30 | BigDecimal getLowPrice(); 31 | BigDecimal getStdDev(); 32 | String getSymbol(); 33 | } 34 | -------------------------------------------------------------------------------- /SecondEdition/StockAPI/src/main/java/net/sdo/stock/StockPriceUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stock; 6 | 7 | import java.io.StringWriter; 8 | import java.math.BigInteger; 9 | import java.util.Date; 10 | import java.util.Map; 11 | import java.util.concurrent.ThreadLocalRandom; 12 | 13 | public class StockPriceUtils { 14 | public static final int STANDARD = 0; 15 | public static final int LOGGER = 1; 16 | public static final int HALF = 2; 17 | public static final int ALTERNATE = 3; 18 | 19 | public static final int SYMBOL_LENGTH = 4; 20 | public static int NSYMBOLS; 21 | static { 22 | NSYMBOLS = 1; 23 | for (int i = 0; i < SYMBOL_LENGTH; i++) { 24 | NSYMBOLS = 26 * NSYMBOLS; 25 | } 26 | } 27 | 28 | public static String makeSymbol(int symbolNumber) { 29 | String s = new BigInteger("" + symbolNumber).toString(26); 30 | StringBuilder sb = new StringBuilder(); 31 | int len = SYMBOL_LENGTH - s.length(); 32 | while (len-- > 0) { 33 | sb.append('A'); 34 | } 35 | for (int i = 0; i < s.length(); i++) { 36 | char c = Character.toUpperCase(s.charAt(i)); 37 | if (c >= '0' & c <= '9') { 38 | c = (char) ('A' + c - '0'); 39 | } else { 40 | c = (char) (c + 10); 41 | } 42 | sb.append(c); 43 | } 44 | return sb.toString(); 45 | } 46 | 47 | public static String getRandomSymbol() { 48 | return makeSymbol(ThreadLocalRandom.current().nextInt(NSYMBOLS)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /SecondEdition/StockAutoPool/stockauto-benchmark/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in 1 2 4 8 16 4 | do 5 | java -jar target/benchmarks.jar --jvmArgs -Djava.util.concurrent.ForkJoinPool.common.parallelism=${i} -p corePoolSize=${i} |& tee out.${i} 6 | done 7 | -------------------------------------------------------------------------------- /SecondEdition/StockBatching/README: -------------------------------------------------------------------------------- 1 | To build: 2 | Make sure you have built StockAPI and StockImpl 3 | Then 4 | mvn package 5 | java -jar StockBatching/target/stock-batching-1.0-SNAPSHOT-shaded.jar 6 | or 7 | mvn install 8 | java -jar /net/sdo/stock/stock-batching/1.0-SNAPSHOT/stock-batching-1.0-SNAPSHOT-shaded.jar 9 | 10 | To run: 11 | In general, to use a MockEntityManager, run 12 | 13 | java -DMockEntityManager=MockEntityManager \ 14 | -jar StockBatching/target/stock-batching-1.0-SNAPSHOT-shaded.jar \ 15 | [-n num_stocks] \ 16 | [-s startDate] [-e endDate] [-l] [-m memoryUse] \ 17 | [-t nThreads] 18 | 19 | where 20 | -n num_stocks -- optional, default 10000 21 | -s startDate -- optional, default 01/01/20 22 | -e endDate -- optional, default 12/31/20 23 | -l -- optional; if present, use logging implementation 24 | -m memoryUse -- optional, number of entries to save (put pressure on heap) 25 | -t nThreads -- optiona, number of threads, default 1 26 | 27 | Note that the number of stocks are memory use are per thread: -n 1000 -t 5 28 | will calculate the mock histories of 5000 stocks (1000 per thread); -m 100 29 | will save the past 100 entries per thread. 30 | 31 | Notes on Examples: 32 | Example 4-2: 33 | To limit the impact of writing to System.out, redirect to /dev/null 34 | 35 | time java -client -DMockEntityManager=MockEntityManager \ 36 | -jar jars/StockBatcher.jar 37 | time java -server -DMockEntityManager=MockEntityManager \ 38 | -jar jars/StockBatcher.jar 39 | time java -XX:+TieredCompilation -DMockEntityManager=MockEntityManager \ 40 | -jar jars/StockBatcher.jar 41 | 42 | Each command is run with an argument of 1, 10, 100, and so on to produce 43 | the results in the table. 44 | 45 | Example 5-1: 46 | To run 100K stocks for 1 thread in a 1G heap for 3 years, saving 1000 47 | entries/thread for GC pressure: 48 | time java -Xmx1g -Xms1g \ 49 | -XX:+UseG1 \ 50 | -DMockEntityManager=MockEntityManager \ 51 | -jar StockBatching/target/stock-batching-1.0-SNAPSHOT-shaded.jar \ 52 | -n 100000 -s 1/1/19 -e 12/31/21 -m 1000 53 | 54 | Example 7-4: 55 | Run with this argument: 56 | -DMockEntityManager=StdRandomMockEntityManager 57 | -------------------------------------------------------------------------------- /SecondEdition/StockBatching/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | net.sdo.stock 4 | stock-batching 5 | 1.0-SNAPSHOT 6 | 7 | UTF-8 8 | 1.8 9 | 1.8 10 | 11 | 12 | 13 | net.sdo.stock 14 | stock-api 15 | 1.0-SNAPSHOT 16 | 17 | 18 | net.sdo.stock 19 | stock-impl 20 | 1.0-SNAPSHOT 21 | 22 | 23 | org.eclipse.persistence 24 | eclipselink 25 | 2.7.4 26 | 27 | 28 | 29 | 30 | 31 | org.apache.maven.plugins 32 | maven-shade-plugin 33 | 3.2.1 34 | 35 | 36 | 37 | shade 38 | 39 | 40 | true 41 | 42 | 43 | net.sdo.StockPriceHistoryBatcher 44 | 45 | 46 | 47 | 48 | *:* 49 | 50 | META-INF/*.SF 51 | META-INF/*.DSA 52 | META-INF/*.RSA 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-ear/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | net.sdo.stock 5 | StockEar 6 | 1.0-SNAPSHOT 7 | 8 | StockEar-ear 9 | ear 10 | StockEar-Ear 11 | 12 | 13 | UTF-8 14 | 15 | 16 | 17 | 18 | net.sdo.stock 19 | StockEar-ejb 20 | 1.0-SNAPSHOT 21 | jar 22 | optional 23 | 24 | 25 | net.sdo.stock 26 | StockEar-war 27 | 1.0-SNAPSHOT 28 | war 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.apache.maven.plugins 36 | maven-compiler-plugin 37 | 3.1 38 | 39 | 1.8 40 | 1.8 41 | 42 | 43 | 44 | org.apache.maven.plugins 45 | maven-ear-plugin 46 | 2.8 47 | 48 | 6 49 | lib 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-ejb/src/main/java/net/sdo/StockSessionBeanLocal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.util.Date; 8 | import javax.ejb.Local; 9 | import net.sdo.stock.StockPriceHistory; 10 | 11 | @Local 12 | public interface StockSessionBeanLocal { 13 | StockPriceHistory getHistory(String symbol, Date startDate, 14 | Date endDate, boolean doMock, int impl); 15 | } 16 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-ejb/src/main/java/net/sdo/StockSessionBeanRemote.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo; 6 | 7 | import java.util.Date; 8 | import javax.ejb.Remote; 9 | import net.sdo.stock.StockPriceHistory; 10 | 11 | @Remote 12 | public interface StockSessionBeanRemote { 13 | StockPriceHistory getHistory(String symbol, Date startDate, 14 | Date endDate, boolean doMock, int impl); 15 | } 16 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-ejb/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.eclipse.persistence.jpa.PersistenceProvider 5 | StockDB 6 | stock-impl-1.0-SNAPSHOT.jar 7 | false 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-war/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | StockServlet 5 | net.sdo.StockServlet 6 | 7 | 8 | StockServlet 9 | /StockServlet 10 | 11 | 12 | 13 | 30 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-war/src/main/webapp/error.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="java.io.PrintWriter"%> 6 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 7 | 8 | 9 | 10 | 11 | 12 | Error 13 | 14 | 15 |

Error from StockPrice Servlet!

16 | The following error was returned from the StockPrice servlet 17 |
18 |             <% 
19 |                 PrintWriter pw = new PrintWriter(out);
20 |                 exception.printStackTrace(pw);
21 |             %>
22 |         
23 | 24 | 25 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-war/src/main/webapp/history.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 6 | 7 | 8 | 9 | 10 | 11 | Stock Price History 12 | 13 | 14 |

Stock Price History for <%=history.getSymbol()%>

15 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 16 |
17 |
High: <%=history.getHighPrice()%> 18 |
Low: <%=history.getLowPrice()%> 19 |
Average: <%=history.getAveragePrice()%> 20 |
Standard Deviation: <%=history.getStdDev()%> 21 | 22 | 23 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-war/src/main/webapp/longhistory.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="java.math.BigDecimal"%> 6 | <%@page import="java.util.Date"%> 7 | <%@page import="net.sdo.stock.StockPrice"%> 8 | <%@page import="java.util.Collection"%> 9 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 10 | 11 | 12 | 13 | 14 | 15 | Stock History Page 16 | 17 | 18 |

Stock Price History for <%=history.getSymbol()%>

19 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 20 |
DateOpenHighLowClose
<%=price%>
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | <% 29 | Collection prices = history.getPrices( 30 | history.getFirstDate(), history.getLastDate()); 31 | for (StockPrice sp : prices) { 32 | Date date = sp.getDate(); 33 | BigDecimal highPrice = sp.getHigh(); 34 | BigDecimal lowPrice = sp.getLow(); 35 | BigDecimal closePrice = sp.getClosingPrice(); 36 | BigDecimal openPrice = sp.getOpeningPrice(); 37 | %> 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | <% 46 | } 47 | %> 48 | 49 | 50 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/StockEar-war/src/main/webapp/option.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Copyright (c) 2013,2014 Scott Oaks. All rights reserved. 3 | --%> 4 | 5 | <%@page import="net.sdo.stock.StockOptionPrice"%> 6 | <%@page import="java.math.BigDecimal"%> 7 | <%@page import="java.util.Date"%> 8 | <%@page import="net.sdo.stock.StockPrice"%> 9 | <%@page import="java.util.Collection"%> 10 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 11 | 12 | 13 | 14 | 15 | 16 | Stock History Page 17 | 18 | 19 |

Stock Option Price History for <%=history.getSymbol()%>

20 | For period <%=history.getFirstDate()%> to <%=history.getLastDate()%> 21 |
DateOpenHighLowClose
<%=date%><%=openPrice%><%=highPrice%><%=lowPrice%><%=closePrice%>
22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | <% 30 | Collection prices = history.getPrices( 31 | history.getFirstDate(), history.getLastDate()); 32 | for (StockPrice sp : prices) { 33 | %> 34 | 35 | <% 36 | Collection optionPrices = sp.getOptions(); 37 | for (StockOptionPrice sop : optionPrices) { 38 | BigDecimal price = sop.getPrice(); 39 | %> 40 | 41 | <% 42 | } 43 | %> 44 | 45 | <% 46 | } 47 | %> 48 | 49 | 50 | -------------------------------------------------------------------------------- /SecondEdition/StockEar/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | net.sdo.stock 4 | StockEar 5 | 1.0-SNAPSHOT 6 | pom 7 | StockBatchingEar 8 | 9 | StockEar-ear 10 | StockEar-ejb 11 | StockEar-war 12 | 13 | 14 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/README: -------------------------------------------------------------------------------- 1 | This is the implementation archive of various examples based on the stock 2 | price simulations. 3 | 4 | mvn package 5 | will build the stock-impl.jar 6 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | net.sdo.stock 4 | stock-impl 5 | 1.0-SNAPSHOT 6 | 7 | UTF-8 8 | stock-impl 9 | 1.8 10 | 1.8 11 | 12 | 13 | 14 | net.sdo.stock 15 | stock-api 16 | 1.0-SNAPSHOT 17 | 18 | 19 | javax.persistence 20 | javax.persistence-api 21 | 2.2 22 | 23 | 24 | org.eclipse.persistence 25 | eclipselink 26 | 2.7.4 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.util.Date; 8 | import javax.persistence.Embeddable; 9 | import javax.persistence.Temporal; 10 | import javax.persistence.TemporalType; 11 | 12 | @Embeddable 13 | public class StockOptionPK { 14 | private String symbol; 15 | @Temporal(TemporalType.DATE) 16 | private Date priceDate; 17 | private int expirationPeriod; 18 | 19 | public StockOptionPK() { 20 | } 21 | 22 | public StockOptionPK(String s, Date d, int i) { 23 | symbol = s; 24 | priceDate = d; 25 | expirationPeriod = i; 26 | } 27 | 28 | public String getSymbol() { 29 | return symbol; 30 | } 31 | 32 | public Date getDate() { 33 | return priceDate; 34 | } 35 | 36 | public int getExpirationPeriod() { 37 | return expirationPeriod; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPriceEagerEagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerEagerImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.EAGER) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerEagerImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPriceEagerLazyImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerLazyImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.LAZY) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerLazyImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPriceEagerLazyQueryCacheImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceEagerLazyQueryCacheImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | @Column(precision = 30, scale = 2) 25 | private BigDecimal price; 26 | @ManyToOne(fetch = FetchType.LAZY) 27 | @JoinColumns({ 28 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 29 | insertable = false, updatable = false), 30 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 31 | insertable = false, updatable = false) 32 | }) 33 | private StockPriceEagerLazyQueryCacheImpl stock; 34 | 35 | @Override 36 | public String getSymbol() { 37 | return id.getSymbol(); 38 | } 39 | 40 | @Override 41 | public Date getDate() { 42 | return id.getDate(); 43 | } 44 | 45 | @Override 46 | public int getExpirationPeriod() { 47 | return id.getExpirationPeriod(); 48 | } 49 | 50 | @Override 51 | public BigDecimal getPrice() { 52 | return price; 53 | } 54 | 55 | public void setId(StockOptionPK id) { 56 | this.id = id; 57 | } 58 | 59 | public void setPrice(BigDecimal price) { 60 | this.price = price; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPriceLazyEagerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name = "StockOptionPrice") 21 | public class StockOptionPriceLazyEagerImpl implements StockOptionPrice { 22 | 23 | @EmbeddedId 24 | private StockOptionPK id; 25 | @Column(precision = 30, scale = 2) 26 | private BigDecimal price; 27 | @ManyToOne(fetch = FetchType.EAGER) 28 | @JoinColumns({ 29 | @JoinColumn(name = "symbol", referencedColumnName = "SYMBOL", 30 | insertable = false, updatable = false), 31 | @JoinColumn(name = "priceDate", referencedColumnName = "priceDate", 32 | insertable = false, updatable = false) 33 | }) 34 | private StockPriceLazyEagerImpl stock; 35 | 36 | @Override 37 | public String getSymbol() { 38 | return id.getSymbol(); 39 | } 40 | 41 | @Override 42 | public Date getDate() { 43 | return id.getDate(); 44 | } 45 | 46 | @Override 47 | public int getExpirationPeriod() { 48 | return id.getExpirationPeriod(); 49 | } 50 | 51 | @Override 52 | public BigDecimal getPrice() { 53 | return price; 54 | } 55 | 56 | public void setId(StockOptionPK id) { 57 | this.id = id; 58 | } 59 | 60 | public void setPrice(BigDecimal price) { 61 | this.price = price; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockOptionPriceLazyLazyImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.math.BigDecimal; 8 | import java.util.Date; 9 | import javax.persistence.Column; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.FetchType; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinColumns; 15 | import javax.persistence.ManyToOne; 16 | import javax.persistence.Table; 17 | import net.sdo.stock.StockOptionPrice; 18 | 19 | @Entity 20 | @Table(name="StockOptionPrice") 21 | public class StockOptionPriceLazyLazyImpl implements StockOptionPrice { 22 | @EmbeddedId 23 | private StockOptionPK id; 24 | 25 | @Column(precision = 30, scale = 2) 26 | private BigDecimal price; 27 | 28 | @ManyToOne(fetch=FetchType.LAZY) 29 | @JoinColumns( 30 | {@JoinColumn(name="symbol", referencedColumnName="SYMBOL", 31 | insertable=false, updatable=false), 32 | @JoinColumn(name="priceDate", referencedColumnName="priceDate", 33 | insertable=false, updatable=false) 34 | }) 35 | private StockPriceLazyLazyImpl stock; 36 | 37 | @Override 38 | public String getSymbol() { 39 | return id.getSymbol(); 40 | } 41 | 42 | @Override 43 | public Date getDate() { 44 | return id.getDate(); 45 | } 46 | 47 | @Override 48 | public int getExpirationPeriod() { 49 | return id.getExpirationPeriod(); 50 | } 51 | 52 | @Override 53 | public BigDecimal getPrice() { 54 | return price; 55 | } 56 | 57 | public void setId(StockOptionPK id) { 58 | this.id = id; 59 | } 60 | 61 | public void setPrice(BigDecimal price) { 62 | this.price = price; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /SecondEdition/StockImpl/src/main/java/net/sdo/stockimpl/StockPricePK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2019 Scott Oaks. All rights reserved. 3 | */ 4 | 5 | package net.sdo.stockimpl; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | import javax.persistence.Embeddable; 10 | import javax.persistence.Temporal; 11 | import javax.persistence.TemporalType; 12 | 13 | @Embeddable 14 | public class StockPricePK implements Serializable { 15 | private String symbol; 16 | 17 | @Temporal(TemporalType.DATE) 18 | private Date priceDate; 19 | 20 | public StockPricePK() { 21 | } 22 | 23 | public StockPricePK(String symbol, Date date) { 24 | this.symbol = symbol; 25 | this.priceDate = date; 26 | } 27 | 28 | public Date getDate() { 29 | return priceDate; 30 | } 31 | 32 | public String getSymbol() { 33 | return symbol; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/.dockerignore: -------------------------------------------------------------------------------- 1 | target/* -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # 1st stage, build the app 18 | FROM maven:3.5.4-jdk-9 as build 19 | 20 | WORKDIR /helidon 21 | 22 | # Create a first layer to cache the "Maven World" in the local repository. 23 | # Incremental docker builds will always resume after that, unless you update 24 | # the pom 25 | ADD pom.xml . 26 | RUN mvn package -DskipTests 27 | 28 | # Do the Maven build! 29 | # Incremental docker builds will resume here when you change sources 30 | ADD src src 31 | RUN mvn package -DskipTests 32 | RUN echo "done!" 33 | 34 | # 2nd stage, build the runtime image 35 | FROM openjdk:8-jre-slim 36 | WORKDIR /helidon 37 | 38 | # Copy the binary built in the 1st stage 39 | COPY --from=build /helidon/target/stockrest.jar ./ 40 | COPY --from=build /helidon/target/libs ./libs 41 | 42 | CMD ["java", "-jar", "stockrest.jar"] 43 | 44 | EXPOSE 8080 45 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/app.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | kind: Service 18 | apiVersion: v1 19 | metadata: 20 | name: stockrest 21 | labels: 22 | app: stockrest 23 | spec: 24 | type: NodePort 25 | selector: 26 | app: stockrest 27 | ports: 28 | - port: 8080 29 | targetPort: 8080 30 | name: http 31 | --- 32 | kind: Deployment 33 | apiVersion: extensions/v1beta1 34 | metadata: 35 | name: stockrest 36 | spec: 37 | replicas: 1 38 | template: 39 | metadata: 40 | labels: 41 | app: stockrest 42 | version: v1 43 | spec: 44 | containers: 45 | - name: stockrest 46 | image: stockrest 47 | imagePullPolicy: IfNotPresent 48 | ports: 49 | - containerPort: 8080 50 | --- 51 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/etc/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/main/java/net/sdo/stockrest/StockApplication.java: -------------------------------------------------------------------------------- 1 | package net.sdo.stockrest; 2 | 3 | import java.util.Set; 4 | 5 | import javax.enterprise.context.ApplicationScoped; 6 | import javax.ws.rs.ApplicationPath; 7 | import javax.ws.rs.core.Application; 8 | 9 | import io.helidon.common.CollectionsHelper; 10 | 11 | @ApplicationScoped 12 | @ApplicationPath("/") 13 | public class StockApplication extends Application { 14 | 15 | @Override 16 | public Set> getClasses() { 17 | return CollectionsHelper.setOf(StockResource.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/main/java/net/sdo/stockrest/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Quickstart MicroProfile example. 19 | */ 20 | package net.sdo.stockrest; 21 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 26 | 27 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/main/resources/META-INF/microprofile-config.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Microprofile server properties 18 | server.port=8080 19 | server.host=0.0.0.0 20 | 21 | #server.executor-service.core-pool-size=16 22 | #server.executor-service.max-pool-size=16 23 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/main/resources/logging.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Example Logging Configuration File 18 | # For more information see $JAVA_HOME/jre/lib/logging.properties 19 | 20 | # Send messages to the console 21 | handlers=java.util.logging.ConsoleHandler 22 | 23 | # Global default logging level. Can be overriden by specific handlers and loggers 24 | .level=INFO 25 | 26 | # Helidon Web Server has a custom log formatter that extends SimpleFormatter. 27 | # It replaces "!thread!" with the current thread name 28 | java.util.logging.ConsoleHandler.level=INFO 29 | java.util.logging.ConsoleHandler.formatter=io.helidon.webserver.WebServerLogFormatter 30 | java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n 31 | 32 | #Component specific log levels 33 | #io.helidon.webserver.level=INFO 34 | #io.helidon.config.level=INFO 35 | #io.helidon.security.level=INFO 36 | #io.helidon.microprofile.level=INFO 37 | #io.helidon.common.level=INFO 38 | #io.netty.level=INFO 39 | #org.glassfish.jersey.level=INFO 40 | #org.jboss.weld=INFO 41 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/test/java/net/sdo/stockrest/MainTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.sdo.stockrest; 18 | 19 | import javax.enterprise.inject.se.SeContainer; 20 | import javax.enterprise.inject.spi.CDI; 21 | import javax.json.JsonObject; 22 | import javax.ws.rs.client.Client; 23 | import javax.ws.rs.client.ClientBuilder; 24 | import javax.ws.rs.client.Entity; 25 | import javax.ws.rs.core.MediaType; 26 | import javax.ws.rs.core.Response; 27 | 28 | import io.helidon.microprofile.server.Server; 29 | import org.junit.jupiter.api.AfterAll; 30 | import org.junit.jupiter.api.Assertions; 31 | import org.junit.jupiter.api.BeforeAll; 32 | import org.junit.jupiter.api.Test; 33 | 34 | class MainTest { 35 | private static Server server; 36 | 37 | @BeforeAll 38 | public static void startTheServer() throws Exception { 39 | server = Main.startServer(); 40 | } 41 | 42 | @Test 43 | void testHelloWorld() { 44 | 45 | Client client = ClientBuilder.newClient(); 46 | 47 | JsonObject jsonObject = client 48 | .target(getConnectionString("/stock")) 49 | .request() 50 | .get(JsonObject.class); 51 | } 52 | 53 | @AfterAll 54 | static void destroyClass() { 55 | CDI current = CDI.current(); 56 | ((SeContainer) current).close(); 57 | } 58 | 59 | private String getConnectionString(String path) { 60 | return "http://localhost:" + server.port() + path; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /SecondEdition/StockRest/stockrest/src/test/resources/META-INF/microprofile-config.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | 18 | # Override configuration in main source branch, so we do not use 8080 port for tests 19 | config_ordinal=1000 20 | # Microprofile server properties 21 | server.port=-1 22 | server.host=0.0.0.0 23 | --------------------------------------------------------------------------------
DateOpenHighLowClose
<%=price%>