Recently engaged in a high-concurrency service center, need to write data to MySQL, the results of the test found that the maximum TPS is 4 K, after discussion decided to first write the received data locally, and then synchronized to the synchronization thread to MySQL.
Initial local storage selected Sqllite, the results test found that sqllite support concurrency problems, and select BerkeleyDB, tested to find berkeleydb meet the needs.
The BerkeleyDB test code is as follows:
Note: The code also has a place to change, such as the Initcheck method to remove the synchronization, to initialize the synchronization, please modify the project itself
package test.berkelydb;import java.io.file;import java.util.date;import java.util.timer; import java.util.timertask;import java.util.concurrent.executorservice;import java.util.concurrent.executors;import java.util.concurrent.timeunit;import java.util.concurrent.atomic.atomicinteger;import org.msgpack.messagepack;import Com.sleepycat.je.cursor;import com.sleepycat.je.cursorconfig;import com.sleepycat.je.database;import com.sleepycat.je.DatabaseConfig;import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.environment;import com.sleepycat.je.environmentconfig;import com.sleepycat.je.lockmode;import com.sleepycat.je.operationstatus;public class testberkely { public environment env; public database db; private static final String dbName = "JSF"; &nBsp; public synchronized void initandcheck () throws Exception { if (Env != null && env.isvalid ()) { return; } environmentconfig envconfig = new environmentconfig (); envconfig.setallowcreate ( true); envconfig.setcachesize (10*1024 * 1024); try { env = new environment (New file ("E:\\test"), envConfig); } catch (exception e) { e.printstacktrace (); } } public void open () { if (db != null) { return; } Databaseconfig dbconfig = new databaseconfig (); dbconfig.setsortedduplicates (True); Dbconfig.setallowcreate (True); try { db = env.opendatabase (null, dbName, Dbconfig); } catch (Exception e) { e.printstacktrace (); } } public void close () { if (db != null) { try { db.close (); } catch (Exception e) { E.printstacktrace (); } } if (env != null) { try { &nBsp; env.close (); } catch (exception e) { e.printstacktrace (); } } } public object get (String key) throws exception { databaseentry querykey = new databaseentry (); databaseentry value = new databaseentry (); querykey.setdata (Key.getbytes ("UTF-8")); operationstatus status = db.get (Null, queryKey, value, lockmode.defAult); if (status == operationstatus.success) { return new string ( Value.getdata ()); } return null; } public boolean put (String key, byte values[]) throws Exception { byte[] thekey = key.getbytes ("UTF-8"); operationstatus status = db.put (Null, new databaseentry (TheKey), new databaseentry ( Values)); if (status == operationstatus.success) { return true; } return false; } public boolean del (String key) throws Exception{ byte[] thekey = key.getbytes ("UTF-8"); operationstatus status = db.delete (NULL, new databaseentry (Thekey)); if ( status == operationstatus.success) { return true; } return false; } public&nbSp;static void main (String[] args) throws Exception { final long len = 10000000; final testberkely tb = new testberkely (); final atomicinteger counter = new atomicinteger (1); tb.initandcheck (); tb.open ( ); timer timer = new timer (); client client = new client (); client.setalias ("[email protected]"); client.setapppath ("E:\\workspace\\myproject\\bin"); Client.setcreatetime (New datE ()); client.setid (100000); client.setinskey (TestBerkely.class.getCanonicalName () + ":: [email protected]"); client.setinterfaceid (10092389); client.setip ("192.168.229.39"); client.setpid (2398); client.setprotocol (1); client.setsafver (+); client.setsrctype (1); client.setstarttime (System.currenttimemillis ()); client.setstatus (1); Client.setuniqkey ("Uniqkey"); client.setupdatetime (new Date ()); &NBSP;&NBSP;&NBSP;&NBSp; client.seturldesc ("89uf92438yq29384yf"); messagepack mp = new messagepack (); mp.register (Client.class); final byte data[] = mp.write (client); ExecutorService exePool = Executors.newfixedthreadpool ( final long start); = system.currenttimemillis (); //Statistics TPS Thread timer.schedule (New timertask () { @Override &Nbsp; public void run () { long end = System.currenttimemillis (); long time = (End-start)/1000; if (time == 0) { return; } int current = counter.get (); System.out.println ("***********-----------------> + (current*100/time/100f); } }, 1000, 2000); for (int i=0; i<10; i++) { exepool.execute (new runnable () { @Override public void run () { int num = counter.getandIncrement (); String key = "Key" + num; try { while (True) { tb.put (Key, data); if (Counter.get () < len) { &nbsP; num = counter.getandincrement (); key = "Key" + num; continue; } break; } } catch (exception e) { e.printstacktrace (); } } }); } exepool.shutdown (); try { while (!exepool.Awaittermination (1, timeunit.seconds)) { } } catch (interruptedexception e) { e.printstacktrace (); } long end = System.currenttimemillis (); long tps = len/((End - start)/1000); system.out.println ("TPs------------------>" + tps); timer.cancel (); &nbSp; tb.env.sync (); tb.env.cleanlog (); counter.set (0); int errornum = 0; //Check Write Data while (Counter.get () < 100000) { if (Tb.get ("Key" + counter.getandincrement ()) == NULL) { errornum ++; } } system.out.println ("Error data is -----------> + errornum); tb.del ("Key") + 5000); &Nbsp; tb.env.sync (); Tb.close (); }}
Berkeley DB TPS Test Code