An open API service indexing awesome lists of open source software.

https://github.com/mostlygeek/go-sqlite3-locking

Test sqlite3 locking vs golang sync.Mutex
https://github.com/mostlygeek/go-sqlite3-locking

Last synced: 3 months ago
JSON representation

Test sqlite3 locking vs golang sync.Mutex

Awesome Lists containing this project

README

        

# Test sqlite3 locking situations

Testing performance and behaviour of doing locking in go's runtime vs sqlite's runtime.
The test sets up parallel readers and writers. Each reader will try to read from the database as
fast as possible. Writers will clear a work queue of updates to do. Print some interesting
ASCII art of the access patterns.

So far the fastest configuration after [adding db.SetMaxConns(1)](https://github.com/mostlygeek/go-sqlite3-locking/commit/f35ab6ca464b0fe0b3e2a71e76037bf9ebc551ee) is: `./test-sqlite -wal -type none`.

## Installing dependencies:

This will fetch and prebuild go-sqlit3:

`$ go install github.com/mattn/go-sqlite3`

## Running it:

```
# building it
$ go build . -o test-sqlite

# cli help
$ ./test-sqlite -h
Usage of ./test-sqlite:
-readers int
Number of parallel readers (default 2)
-rows int
Number of total DB rows, lower number = more contention (default 10)
-type string
Locking type: [none, mutex, rwmutex] (default "none")
-updates int
How many UPDATE dml operations to perform over numRows (default 500)
-wal
Use WAL mode for database
-writers int
Number of parallel writers (default 2)

# (default) no locking, retries
$ ./test-sqlite

# using sync.Mutex
$ ./test-sqlite -type mutex

# using sync.RWMutex
$ ./test-sqlite -type rwmutex
```

## Output

A lot of fun ASCII symbols will be printed for the test, one for each retry, write and read. This makes it easier to visualize what's happening.

```
$ ./test-sqlite -type rwmutex -updates 25
Legend
---------------------------
Write : .
Write Retry : |
Read : -
Read Retry : |

Running sync.RWMutex test
--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--

Duration: 4.106226ms
```

## Try it with:

```
# more writers than readers
$ ./test-sqlite -updates 500 -writers 4 -readers 3 -type mutex
$ ./test-sqlite -updates 500 -writers 4 -readers 3 -type rwmutex

# more readers than writers
$ ./test-sqlite -updates 500 -writers 4 -readers 5 -type mutex
$ ./test-sqlite -updates 500 -writers 4 -readers 5 -type rwmutex
```