s/benchmarks/micro/BenchmarkLocking.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 12 Feb 2020 15:09:57 +0000
changeset 318 1b735d3747d8
parent 317 a462cd3ed353
permissions -rw-r--r--
Use launcher script to run smalltalk ...rather than binary. This makes it work in both, in-tree builds and (new) out-of-tree builds.

"{ Package: 'jv:calipel/s/benchmarks/micro' }"

"{ NameSpace: Smalltalk }"

Benchmark subclass:#BenchmarkLocking
	instanceVariableNames:'nesting threads lock'
	classVariableNames:''
	poolDictionaries:''
	category:'CalipeL-S-Benchmarks-Micro'
!

!BenchmarkLocking class methodsFor:'queries'!

isAbstract
    "Return if this class is an abstract class.
     True is returned here for myself only; false for subclasses.
     Abstract subclasses must redefine this again."

    ^ self == BenchmarkLocking.
! !

!BenchmarkLocking methodsFor:'benchmarks'!

benchmarkBase
    <benchmark: 'workload (no locks)'>

    | blocker iterations body |

    nesting isNil ifTrue:[ nesting := 1 ].
    iterations := 10000000.
    blocker := Semaphore new: (threads - 1) negated.
    body := [
                iterations timesRepeat:[ self workloadBase: nesting ].
                blocker signal.
            ].
    1 to: threads do:[:i |
        | t |

        t := body newProcess.
        t name: 'Benchmark ''', self class name, ' >> benchmarkBase, thread ', i printString.
        t resume.
    ].
    blocker wait.

    "
    BenchmarkRecursionLock run: #benchmarkCritical.
    BenchmarkSemaphore run: #benchmarkCritical
    "

    "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"

!

benchmarkCritical
    <benchmark: '#critical:'>

    | blocker iterations body |

    nesting isNil ifTrue:[ nesting := 1 ].
    iterations := 10000000.
    blocker := Semaphore new: (threads - 1) negated.
    body := [
                iterations timesRepeat:[ self workloadCritical: nesting ].
                blocker signal.
            ].
    1 to: threads do:[:i |
        | t |

        t := body newProcess.
        t name: 'Benchmark ''', self class name, ' >> #critical:'', thread ', i printString.
        t resume.
    ].
    blocker wait.

    "
    BenchmarkRecursionLock run: #benchmarkCritical.
    BenchmarkSemaphore run: #benchmarkCritical
    "

    "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"

!

benchmarkCriticalN
    <benchmark: '#critical: (no work)'>

    | blocker iterations body |

    nesting isNil ifTrue:[ nesting := 1 ].
    iterations := 10000000.
    blocker := Semaphore new: (threads - 1) negated.
    body := [
                iterations timesRepeat:[ self justpureCritical: nesting ].
                blocker signal.
            ].
    1 to: threads do:[:i |
        | t |

        t := body newProcess.
        t name: 'Benchmark ''', self class name, ' >> #critical:'', thread ', i printString.
        t resume.
    ].
    blocker wait.

    "
    BenchmarkRecursionLock run: #benchmarkCritical.
    BenchmarkSemaphore run: #benchmarkCritical
    "

    "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"

! !

!BenchmarkLocking methodsFor:'parameters'!

threads: anInteger
    <parameter: #threads type: #Integer values: #(1 2 5)>
    threads := anInteger.

    "Created: / 25-08-2017 / 08:52:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BenchmarkLocking methodsFor:'private'!

doSomeWork
    1 perform: #+ with: 1

    "Created: / 25-08-2017 / 09:04:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:11:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

justpureCritical: n
    lock critical:[
        n > 1 ifTrue:[
            self justpureCritical: n - 1.
        ].
    ].

    "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

workloadBase: n
    self doSomeWork.
    n > 1 ifTrue:[
        self workloadBase: n - 1.
    ].

    "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"

!

workloadCritical: n
    lock critical:[
        self doSomeWork.
        n > 1 ifTrue:[
            self workloadCritical: n - 1.
        ].
    ].

    "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !