Skip to content

Commit

Permalink
Moved utils from U into Cls, UTILS and Bufs utility classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jan 24, 2015
1 parent 4b89abc commit bb51eda
Show file tree
Hide file tree
Showing 35 changed files with 311 additions and 308 deletions.
Expand Up @@ -570,7 +570,7 @@ private boolean invariant() {
}

private void dumpBuffers() {
U.print(">> BUFFER " + name + " HAS " + bufN + " PARTS:");
System.out.println(">> BUFFER " + name + " HAS " + bufN + " PARTS:");

for (int i = 0; i < bufN - 1; i++) {
ByteBuffer buf = bufs[i];
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.rapidoid.buffer.Buf;
import org.rapidoid.bytes.BytesUtil;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

public class KeyValueRanges {

Expand Down Expand Up @@ -118,10 +119,10 @@ public Map<String, String> toMap(Buf src, boolean urlDecodeKeys, boolean urlDeco
String val = values[i].str(src.bytes());

if (urlDecodeKeys) {
key = U.urlDecode(key);
key = UTILS.urlDecode(key);
}
if (urlDecodeVals) {
val = U.urlDecode(val);
val = UTILS.urlDecode(val);
}

map.put(key, val);
Expand All @@ -138,7 +139,7 @@ public Map<String, byte[]> toBinaryMap(Buf src, boolean urlDecodeKeys) {
byte[] val = values[i].bytes(src);

if (urlDecodeKeys) {
key = U.urlDecode(key);
key = UTILS.urlDecode(key);
}

map.put(key, val);
Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.rapidoid.config.Conf;
import org.rapidoid.db.model.Person;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.testng.annotations.Test;

public class DbClassPersistenceTest extends DbTestCommons {
Expand All @@ -37,9 +37,9 @@ public void testPersistence() {

System.out.println("inserting...");

U.startMeasure();
UTILS.startMeasure();

U.benchmarkMT(Conf.cpus(), "insert", count, new Runnable() {
UTILS.benchmarkMT(Conf.cpus(), "insert", count, new Runnable() {
@Override
public void run() {
DB.insert(new Person("abc", -1));
Expand All @@ -50,7 +50,7 @@ public void run() {

final CountDownLatch latch = new CountDownLatch(count);

U.benchmarkMT(10, "update", count, latch, new Runnable() {
UTILS.benchmarkMT(10, "update", count, latch, new Runnable() {
@Override
public void run() {
int id = Rnd.rnd(count) + 1;
Expand All @@ -68,7 +68,7 @@ public void run() {
}

private void checkDb(final int count) {
U.endMeasure("total");
UTILS.endMeasure("total");

eq(DB.size(), count);

Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.rapidoid.config.Conf;
import org.rapidoid.db.model.IPerson;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.testng.annotations.Test;

public class DbInterfacePersistenceTest extends DbTestCommons {
Expand All @@ -35,9 +35,9 @@ public void testPersistence() {

System.out.println("inserting...");

U.startMeasure();
UTILS.startMeasure();

U.benchmarkMT(Conf.cpus(), "insert", count, new Runnable() {
UTILS.benchmarkMT(Conf.cpus(), "insert", count, new Runnable() {
@Override
public void run() {
DB.insert(DB.create(IPerson.class, "name", "abc", "age", -1));
Expand All @@ -46,7 +46,7 @@ public void run() {

System.out.println("updating...");

U.benchmarkMT(10, "update", count, new Runnable() {
UTILS.benchmarkMT(10, "update", count, new Runnable() {
@Override
public void run() {
int id = Rnd.rnd(count) + 1;
Expand All @@ -63,7 +63,7 @@ public void run() {
}

private void checkDb(final int count) {
U.endMeasure("total");
UTILS.endMeasure("total");

eq(DB.size(), count);

Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.rapidoid.log.Log;
import org.rapidoid.log.LogLevel;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.testng.annotations.Test;

public class DbTransactionTest extends DbTestCommons {
Expand All @@ -36,7 +37,7 @@ public void testMultiThreadedTransactionAtomicity() {

final AtomicInteger n = new AtomicInteger();

U.benchmarkMT(10, "tx", 10000, new Runnable() {
UTILS.benchmarkMT(10, "tx", 10000, new Runnable() {
@Override
public void run() {
DB.transaction(new Runnable() {
Expand Down
2 changes: 1 addition & 1 deletion rapidoid-db/src/main/java/org/rapidoid/db/DB.java
Expand Up @@ -44,7 +44,7 @@ public class DB {
static {
String dbImplName = Conf.option("db", IMPL_NAME);

DB_IMPL_CLASS = U.getClassIfExists(IMPL_NAME);
DB_IMPL_CLASS = Cls.getClassIfExists(IMPL_NAME);

U.must(DB_IMPL_CLASS != null, "Cannot find Db implementation (%s)!", dbImplName);
U.must(Database.class.isAssignableFrom(DB_IMPL_CLASS), "%s must implement %s!", dbImplName,
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.rapidoid.compile.Compile;
import org.rapidoid.config.Conf;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.rapidoid.wrap.Int;

public class CompilerBenchmark {
Expand All @@ -39,7 +40,7 @@ public static void main(String[] args) throws Throwable {

final Int n = new Int();

U.benchmarkMT(threads, "compile", count, new Runnable() {
UTILS.benchmarkMT(threads, "compile", count, new Runnable() {
@Override
public void run() {
int x = n.value++;
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.rapidoid.config.Conf;
import org.rapidoid.db.DB;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

public class DbCRUDBenchmark {

Expand All @@ -32,7 +33,7 @@ public static void main(String[] args) {

int size = Conf.option("size", 10000);

U.benchmarkMT(Conf.cpus(), "insert+read", size, new Runnable() {
UTILS.benchmarkMT(Conf.cpus(), "insert+read", size, new Runnable() {
@Override
public void run() {
String name = "Niko";
Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.rapidoid.config.Conf;
import org.rapidoid.db.DB;
import org.rapidoid.util.Rnd;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

public class DbPersistenceBenchmark {

Expand All @@ -35,9 +35,9 @@ public static void main(String[] args) {

System.out.println("inserting...");

U.startMeasure();
UTILS.startMeasure();

U.benchmarkMT(Conf.cpus(), "insert", size, new Runnable() {
UTILS.benchmarkMT(Conf.cpus(), "insert", size, new Runnable() {
@Override
public void run() {
DB.insert(new Person("abc", 10));
Expand All @@ -46,7 +46,7 @@ public void run() {

System.out.println("updating...");

U.benchmarkMT(Conf.cpus(), "update", size, new Runnable() {
UTILS.benchmarkMT(Conf.cpus(), "update", size, new Runnable() {
@Override
public void run() {
DB.update(Rnd.rnd(size) + 1, new Person("xyz", 10));
Expand All @@ -57,7 +57,7 @@ public void run() {

DB.shutdown();

U.endMeasure("total");
UTILS.endMeasure("total");
}

}
Expand Up @@ -27,7 +27,7 @@
import org.rapidoid.db.impl.inmem.DbEntityConstructor;
import org.rapidoid.db.impl.inmem.JacksonEntitySerializer;
import org.rapidoid.inmem.InMem;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

public class DbSerializationBenchmark {

Expand All @@ -48,7 +48,7 @@ public static void main(String[] args) {

System.out.println("measuring...");

U.benchmark("save " + size + " records", loops, new Runnable() {
UTILS.benchmark("save " + size + " records", loops, new Runnable() {
@Override
public void run() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.rapidoid.security.annotation.Manager;
import org.rapidoid.security.annotation.Role;
import org.rapidoid.security.annotation.Roles;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

@Admin
@Manager
Expand All @@ -40,7 +40,7 @@ public void onShutdown() {
DB.shutdown();

Log.warn("Shutting down the application...");
U.schedule(new Runnable() {
UTILS.schedule(new Runnable() {
@Override
public void run() {
Log.warn("Exit application");
Expand Down
Expand Up @@ -263,7 +263,7 @@ private void parseMultiParts(Buf src, Range body, KeyValueRanges data, KeyValueR
try {

while ((pos2 = BytesUtil.find(src.bytes(), start, limit, helper.bytes, 0, sepLen, true)) >= 0) {
// U.print("****** PARSE MULTI *** pos2=" + pos2);
// System.out.println("****** PARSE MULTI *** pos2=" + pos2);

if (pos1 >= 0 && pos2 >= 0) {
int from = pos1 + sepLen + 2;
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.io.FileUtils;
import org.rapidoid.util.IO;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.testng.annotations.Test;

public class HttpMultipartFormTest extends HttpTestCommons {
Expand All @@ -33,9 +34,9 @@ public class HttpMultipartFormTest extends HttpTestCommons {
public void shouldHandleUploads() throws Throwable {
defaultServerSetup();

String hash1 = U.md5(FileUtils.readFileToByteArray(new File(IO.resource("test1.txt").toURI())));
String hash2 = U.md5(FileUtils.readFileToByteArray(new File(IO.resource("test2.txt").toURI())));
String hash3 = U.md5("");
String hash1 = UTILS.md5(FileUtils.readFileToByteArray(new File(IO.resource("test1.txt").toURI())));
String hash2 = UTILS.md5(FileUtils.readFileToByteArray(new File(IO.resource("test2.txt").toURI())));
String hash3 = UTILS.md5("");

eq(upload("/upload", U.map("a", "bb"), U.map("f1", "test1.txt", "f2", "test2.txt")),
"bar:a:bb:2:" + U.join(":", hash1, hash2, hash3));
Expand All @@ -47,9 +48,9 @@ public void shouldHandleUploads() throws Throwable {
public void shouldHandleBigUploads() throws Throwable {
defaultServerSetup();

String hash1 = U.md5(FileUtils.readFileToByteArray(new File(IO.resource("test1.txt").toURI())));
String hash2 = U.md5(FileUtils.readFileToByteArray(new File(IO.resource("test2.txt").toURI())));
String hash3 = U.md5(FileUtils.readFileToByteArray(new File(IO.resource("rabbit.jpg").toURI())));
String hash1 = UTILS.md5(FileUtils.readFileToByteArray(new File(IO.resource("test1.txt").toURI())));
String hash2 = UTILS.md5(FileUtils.readFileToByteArray(new File(IO.resource("test2.txt").toURI())));
String hash3 = UTILS.md5(FileUtils.readFileToByteArray(new File(IO.resource("rabbit.jpg").toURI())));

eq(upload("/upload", U.map("a", "d"), U.map("f1", "test1.txt", "f2", "test2.txt", "f3", "rabbit.jpg")),
"bar:a:d:3:" + U.join(":", hash1, hash2, hash3));
Expand Down
Expand Up @@ -24,7 +24,7 @@
import org.rapidoid.buffer.BufGroup;
import org.rapidoid.data.Ranges;
import org.rapidoid.net.impl.RapidoidHelper;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

public class HttpParserPerfTest {

Expand All @@ -46,7 +46,7 @@ public static void main(String[] args) {
final HttpExchangeImpl req = new HttpExchangeImpl();

for (int i = 0; i < 10; i++) {
U.benchmark("parse", 3000000, new Runnable() {
UTILS.benchmark("parse", 3000000, new Runnable() {
int n;

@Override
Expand All @@ -61,12 +61,12 @@ public void run() {
});
}

U.print(BUFS.instances() + " buffer instances.");
System.out.println(BUFS.instances() + " buffer instances.");
}

private static Buf r(String req) {
req = req.replaceAll("\\|", "\r\n");
U.print("Request size: " + req.length());
System.out.println("Request size: " + req.length());
return BUFS.from(req, "");
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.rapidoid.net.impl.ConnState;
import org.rapidoid.net.impl.Protocol;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.rapidoid.wrap.Bool;
import org.rapidoid.wrap.Int;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void process(final Channel ctx) {
}).build().start();

int sec = 5;
U.sleep(sec * 1000);
UTILS.sleep(sec * 1000);
shutdown();

isFalse(err.value);
Expand Down

0 comments on commit bb51eda

Please sign in to comment.