提交 1a3b634e 编写于 作者: oldratlee's avatar oldratlee 🔥

convert Utils.java -> Utils.kt

上级 234d2402
package com.alibaba.ttl.perf;
import java.util.Random;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
public class Utils {
private static Random random = new Random();
private static String bytes2Hex(byte[] bytes) {
StringBuilder sb = new StringBuilder(1024);
for (byte b : bytes) {
String s = Integer.toHexString(b & 0xFF);
sb.append((s.length() == 1) ? "0" + s : s);
}
return sb.toString();
}
private static byte[] getRandomBytes() {
byte[] bytes = new byte[1024];
random.nextBytes(bytes);
return bytes;
}
public static String getRandomString() {
return bytes2Hex(getRandomBytes());
}
private Utils() {
}
}
package com.alibaba.ttl.perf
import java.util.*
private val random = Random()
internal fun bytes2Hex(bytes: ByteArray): String {
val sb = StringBuilder(1024)
for (b in bytes) {
val s = Integer.toHexString(b.toInt() and 0xFF)
sb.append(if (s.length == 1) "0$s" else s)
}
return sb.toString()
}
internal fun getRandomBytes(): ByteArray {
val bytes = ByteArray(1024)
random.nextBytes(bytes)
return bytes
}
internal fun getRandomString(): String {
return bytes2Hex(getRandomBytes())
}
......@@ -2,7 +2,7 @@
package com.alibaba.ttl.perf.memoryleak
import com.alibaba.ttl.perf.Utils
import com.alibaba.ttl.perf.getRandomString
/**
* @author Jerry Lee (oldratlee at gmail dot com)
......@@ -11,7 +11,7 @@ fun main(args: Array<String>) {
var counter: Long = 0
while (true) {
val threadLocal = ThreadLocal<String>()
threadLocal.set(Utils.getRandomString())
threadLocal.set(getRandomString())
if (counter % 1000 == 0L)
System.out.printf("%05dK%n", counter / 1000)
......
......@@ -3,7 +3,7 @@
package com.alibaba.ttl.perf.memoryleak
import com.alibaba.ttl.TransmittableThreadLocal
import com.alibaba.ttl.perf.Utils
import com.alibaba.ttl.perf.getRandomString
/**
* @author Jerry Lee (oldratlee at gmail dot com)
......@@ -12,7 +12,7 @@ fun main(args: Array<String>) {
var counter: Long = 0
while (true) {
val threadLocal = TransmittableThreadLocal<String>()
threadLocal.set(Utils.getRandomString())
threadLocal.set(getRandomString())
if (counter % 1000 == 0L)
System.out.printf("%05dK%n", counter / 1000)
......
......@@ -2,7 +2,7 @@
package com.alibaba.ttl.perf.tps
import com.alibaba.ttl.perf.Utils
import com.alibaba.ttl.perf.getRandomString
/**
* @author Jerry Lee (oldratlee at gmail dot com)
......@@ -12,7 +12,7 @@ fun main(args: Array<String>) {
tpsCounter.setAction(Runnable {
val threadLocal = ThreadLocal<String>()
threadLocal.set(Utils.getRandomString())
threadLocal.set(getRandomString())
})
while (true) {
......
......@@ -3,7 +3,7 @@
package com.alibaba.ttl.perf.tps
import com.alibaba.ttl.TransmittableThreadLocal
import com.alibaba.ttl.perf.Utils
import com.alibaba.ttl.perf.getRandomString
/**
* @author Jerry Lee (oldratlee at gmail dot com)
......@@ -13,7 +13,7 @@ fun main(args: Array<String>) {
tpsCounter.setAction(Runnable {
val threadLocal = TransmittableThreadLocal<String>()
threadLocal.set(Utils.getRandomString())
threadLocal.set(getRandomString())
})
while (true) {
......
package com.alibaba.ttl.perf.tps
import org.junit.Assert.fail
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
import org.junit.Assert.fail
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
......
......@@ -108,6 +108,7 @@ class TransmittableThreadLocal_Transmitter_UserTest {
}
@AfterClass
@Suppress("unused")
fun afterClass() {
executorService.shutdown()
executorService.awaitTermination(100, TimeUnit.MILLISECONDS)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册