Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php usleep with simultaneous requests

Tags:

php

I've been playing around with the ApacheBench (ab) command line utility but I'm not sure if it's the right tool for what I'm trying to do.

I was running 10 concurrent and 10 requests doing an INSERT into a MySQL table and looking at round(microtime(1) * 10000) to see if I was getting the same values.

I would like to know if using delayed execution usleep() is enough to prevent two simultaneous requests getting the same value on the same box, or is time_sleep_until better.

I know I could set more entropy with uniqid().

So I'm just wondering if using delayed execution will be enough to prevent two exact time stamps.

like image 740
david Avatar asked Mar 19 '26 05:03

david


1 Answers

So you want to know if round(microtime(1) * 10000) can give duplicate values? Well of course it can, I don't think you need to test for this.

uniqid is also not guaranteed to be unique, even with more entropy.

If uniqueness is really important you could always use MySQL with a UNIQUE index to enforce uniqueness.

like image 193
Halcyon Avatar answered Mar 21 '26 19:03

Halcyon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!