1 /*
2   Copyright (c) 2010, 2021, Oracle and/or its affiliates.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License, version 2.0, for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 package com.mysql.cluster.crund;
26 
27 import com.mysql.cluster.crund.CrundDriver.XMode;
28 
29 abstract class CrundSLoad extends Load {
30     // resources
31     protected final CrundDriver driver;
32 
CrundSLoad(CrundDriver driver)33     public CrundSLoad(CrundDriver driver) {
34         this.driver = driver;
35         driver.addLoad(this);
36     }
37 
38     // ----------------------------------------------------------------------
39     // intializers/finalizers
40     // ----------------------------------------------------------------------
41 
initProperties()42     abstract protected void initProperties();
printProperties()43     abstract protected void printProperties();
44 
init()45     public void init() throws Exception {
46         initProperties();
47         printProperties();
48     }
49 
close()50     public void close() throws Exception {}
51 
52     // ----------------------------------------------------------------------
53     // datastore operations
54     // ----------------------------------------------------------------------
55 
initConnection()56     abstract public void initConnection() throws Exception;
closeConnection()57     abstract public void closeConnection() throws Exception;
clearData()58     abstract public void clearData() throws Exception;
59 
60     // ----------------------------------------------------------------------
61     // benchmark operations
62     // ----------------------------------------------------------------------
63 
clearPersistenceContext()64     abstract protected void clearPersistenceContext();
runInsert(XMode mode, int[] id)65     abstract protected void runInsert(XMode mode, int[] id) throws Exception;
runLookup(XMode mode, int[] id)66     abstract protected void runLookup(XMode mode, int[] id) throws Exception;
runUpdate(XMode mode, int[] id)67     abstract protected void runUpdate(XMode mode, int[] id) throws Exception;
runDelete(XMode mode, int[] id)68     abstract protected void runDelete(XMode mode, int[] id) throws Exception;
69 
70     // runs a sequence of benchmark operations
runOperations(int nOps)71     public void runOperations(int nOps) throws Exception {
72         final int[] id = new int[nOps];
73         for (int i = 0; i < nOps; i++)
74             id[i] = i * 2;
75 
76         for (XMode m : driver.xModes) {
77             clearPersistenceContext();
78             runInsert(m, id);
79             clearPersistenceContext();
80             runLookup(m, id);
81             clearPersistenceContext();
82             runUpdate(m, id);
83             clearPersistenceContext();
84             runDelete(m, id);
85         }
86         // XXX failing fast, not yet using driver.failOnError, driver.logError()
87         //driver.abortIfErrors();
88     }
89 
90     // ----------------------------------------------------------------------
91     // helpers
92     // ----------------------------------------------------------------------
93 
verify(boolean cond)94     static protected final void verify(boolean cond) {
95         if (!cond)
96             throw new RuntimeException("data verification failed.");
97     }
98 
verify(int exp, int act)99     static protected final void verify(int exp, int act) {
100         if (exp != act)
101             throw new RuntimeException("data verification failed:"
102                                        + " expected = " + exp
103                                        + ", actual = " + act);
104     }
105 
verify(String exp, String act)106     static protected final void verify(String exp, String act) {
107         if (exp == null ? act != null : !exp.equals(act))
108             throw new RuntimeException("data verification failed:"
109                                        + " expected = '" + exp + "'"
110                                        + ", actual = '" + act + "'");
111     }
112 }
113