是否可以使用JMeter生成UUID v1?

时间:2017-07-28 08:14:30

标签: jmeter uuid

我阅读了JMeter的手册,发现JMeter有__uuid()函数。它允许为JMeter测试生成UUID类型4。是否可以在JMeter中生成UUIDv1,或者可能存在一些插件。

3 个答案:

答案 0 :(得分:1)

在jmeter中你可以添加JSR 223 Sampler选择Java语言并为UUID版本1执行java代码:

 String timeuuid = com.datastax.driver.core.utils.UUIDs.timeBased().toString();

然后将其添加到Jmeter变量:

 vars.put("myUUID", timeuuid);

答案 1 :(得分:1)

我建议采取以下步骤:

  1. 下载Jug library(例如从here下载)并将.jar放到某处JMeter Classpath
  2. 重新启动JMeter以选择.jar up
  3. 完成后,您应该能够使用JSR223 Test ElementsGroovy language生成UUIDv1,如:

    import com.fasterxml.uuid.EthernetAddress
    import com.fasterxml.uuid.Generators
    import com.fasterxml.uuid.impl.TimeBasedGenerator
    
    def addr = EthernetAddress.fromInterface()
    def gen = Generators.timeBasedGenerator(addr)
    def v1uuid = gen.generate()
    
    log.info(v1uuid.toString())
    
  4. 演示:

    JMeter Generate UUIDv1

    参考文献:

答案 2 :(得分:0)

首先,我们将生成64个最低和最高有效位作为长值:

private static long get64LeastSignificantBitsForVersion1() {
    Random random = new Random();
    long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
    long variant3BitFlag = 0x8000000000000000L;
    return random63BitLong + variant3BitFlag;
}
 
private static long get64MostSignificantBitsForVersion1() {
    LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
    Duration duration = Duration.between(start, LocalDateTime.now());
    long seconds = duration.getSeconds();
    long nanos = duration.getNano();
    long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
    long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
    long version = 1 << 12;
    return 
      (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
}

然后我们可以将这两个值传递给UUID的构造函数:

public static UUID generateType1UUID() {
 
    long most64SigBits = get64MostSignificantBitsForVersion1();
    long least64SigBits = get64LeastSignificantBitsForVersion1();
 
    return new UUID(most64SigBits, least64SigBits);
}